- Introduction
- Abstraction and Encapsulation
- An Interactive Hello World! Program
- Summary
- Review Questions
- Programming Exercises
Programming Exercises
In the following exercises, you are meant to change and add parts to the program in Listing 3.1 to make it perform the suggested actions.
-
Instead of printing "Bye Bye!" as the last text on the command console before the program finishes, change the source code so that the program writes "Bye Bye. Have a good day!"
-
Instead of typing a y to have the program print "Hello World!", have the user type Yes. Have the program inform the user about this by changing line 10.
-
Instead of using the variable name answer to hold the input from the user, change the name to userInput.
-
Let the program print out an additional line under "Bye Bye. Have a good day!" saying "The program is terminating."
-
Declare another variable of type string called userName. Before the program prints "Do you want me to write the famous words?", have the program request the user to type in his or her name. After the user has entered his or her name, have the program read this name and store it in the userName variable. Then have the program print "Hello" followed by the content of the username. Tip: the last printout can be accomplished by typing
System.Console.WriteLine("Hello" + userName);
A typical execution of the program should result in the following output:
Please type your name Deborah<enter> Hello Deborah Do you want me to write the famous words? Type Yes for yes; n for no. Then <enter>. Yes<enter> Hello World! Bye Bye. Have a good day! The program is terminating.