Page 402 - Introduction to Programming with Java: A Problem Solving Approach
P. 402
368
Chapter 9 Classes with Class Members
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
private static PetMouse pets; // points to list of pets
private String name;
private PetMouse next;
//******************************************************
// Insert each new object at beginning of existing list.
public PetMouse()
}
// end class PetMouse
{
Scanner stdIn = new Scanner(System.in);
this.next = pets;
System.out.print("Enter name: ");
this.name = stdIn.nextLine();
pets = this;
// end constructor
//******************************************************
}
public static void list()
{
}
// end list
PetMouse next = pets;
Apago PDF Enhancer
while (next != null)
{
}
System.out.print(next.name + " ");
next = next.next;
System.out.println();
Use the following trace setup to trace the PetMouse program. Note how the pets class variable is underneath the PetMouse header, but separate from the three objects. We’ve shown pets’s initial value, null.
input cutie sugar fluffy
Driver
PetMouse
line#
line#
static
pets
list
next
name
obj1
next
name
obj2
next
name
obj3
next
output
null