Java Reference
In-Depth Information
Display 4.16
Using Constructors and Set Methods (part 2 of 2)
6
Pet usersPet = new Pet("Jane Doe");
7
System.out.println("My records on your pet are incomplete.");
8
System.out.println("Here is what they currently say:");
9
System.out.println(usersPet);
10
Scanner keyboard = new Scanner(System.in);
11
System.out.println("Please enter the pet's name:");
12
String name = keyboard.nextLine();
13
System.out.println("Please enter the pet's age:");
14
int age = keyboard.nextInt();
15
System.out.println("Please enter the pet's weight:");
16
double weight = keyboard.nextDouble();
17
usersPet.set(name, age, weight);
18
System.out.println("My records now say:");
19
System.out.println(usersPet);
20
}
This is equivalent to
System.out.println(usersPet.toString());
21 }
Sample Dialogue
My records on your pet are incomplete.
Here is what they currently say:
Name: Jane Doe Age: 0 years
Weight: 0.0 pounds
Please enter the pet's name:
Fang Junior
Please enter the pet's age:
5
Please enter the pet's weight:
87.5
My records now say:
Name: Fang Junior Age: 5 years
Weight: 87.5 pounds
The StringTokenizer Class
The StringTokenizer class is used to recover the words in a multiword string. It is
often used when reading input. However, when we covered input in Chapter 2, we
could not cover the StringTokenizer class because use of the StringTokenizer class
normally involves knowledge of loops and constructors, two topics that we had not yet
covered. We now have covered enough material to explain the StringTokenizer class.
 
 
Search WWH ::




Custom Search