Solution for Programmming Exercise 2.3
This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.
Exercise 2.3:
Write a program that asks the user's name, and then greets the user by name. Before outputting the user's name, convert it to upper case letters. For example, if the user's name is Fred, then the program should respond "Hello, FRED, nice to meet you!".
In order to read the name typed in by the user, this program uses one of the input routines from the non-standard TextIO class. So, this program can only be run if that class is available to the program. For consistency, I will also use TextIO for output, although I could just as easily use System.out.
A name is a sequence of characters, so it is a value of type String. The program must declare a variable of type String to hold the user's name. I declare another variable to hold the user's name with all the letters in the name converted to upper case. The conversion might be difficult, except that String objects have a function that does the conversion. If usersName is the variable that refers to the name that the user enters, then the function call usersName.toUpperCase() returns the string obtained by replacing any lower case letters in the name with upper case letters.
There are several functions in TextIO class that can be used for reading Strings: getWord(), getlnWord(), and getln(). The first two routines only read a single word, so if the user entered "David J. Eck", they would only read the first name, "David". The getln() routine will read the entire line, and so would get the whole name. For this program, I use getln(), but you might refer to use just the first name.
(For this program, by the way, getWord() and getlnWord() would be equivalent. They return the same value. The second version of the routine, getlnWord(), would then discard the rest of the user's line of input. However, since this program is only doing one input operation, it doesn't matter whether it's discarded. It would only matter when it came time to read a second value from input.)
public class Greeting { /* This program asks the user's name and then greets the user by name. This program depends on the non-standard class, TextIO. */ public static void main(String[] args) { String usersName; // The user's name, as entered by the user. String upperCaseName; // The user's name, converted to upper case letters. TextIO.put("Please enter your name: "); usersName = TextIO.getln(); upperCaseName = usersName.toUpperCase(); TextIO.putln("Hello, " + upperCaseName + ", nice to meet you!"); } // end main() } // end class