Problem

PetMouse program:The program below creates a linked list of objects. The class reference v...

PetMouse program:

The program below creates a linked list of objects. The class reference variable, pets, refers to the first object in the list, and each subsequent object contains an instance reference variable that refers to the next object, except the instance reference variable in the last object refers to null. Notice that the individual pets are anonymous objects, in that they do not have separate names. The class reference variable, pets, actually refers to the first object in the list, but conceptually it refers to all the objects in the list. Notice that we use the same word, next, for a local variable in a class method and an instance variable, because both variables are really talking about the same things, and it would be unnatural to use different terms.

1 /**************************************************************2 * PetMouseDriver.java3 * Dean&Dean4 *5 * This creates&displays a linked list of simple objects.78 public class PetMouseDriver9 {10 public static void main(String[] args)11 {12 new PetMouse();13 new PetMouse();14 new PetMouse();15 PetMouse.list();16 } // end main17 } // end class PetMouseDriver

1 /**************************************************************2 * PetMouse.java3 * Dean&Dean4 *5 * This creates&displays a linked list of simple objects.78 import java.util.Scanner;910 public class PetMouse11 {12 private static PetMouse pets; // points to list of pets1314 private String name;15 private PetMouse next;1617 //******************************************************1819 // Insert each new object at beginning of existing list.2021 public PetMouse()22 {23 Scanner stdIn = new Scanner(System.in);2425 this.next = pets;26 System.out.print("Enter name: ");27 this.name = stdIn.nextLine();28 pets = this;29 } // end constructor3031 //******************************************************3233 public static void list()34 {35 PetMouse next = pets;3637 while (next != null)38 {39 System.out.print(next.name + " ");40 next = next.next;41 }42 System.out.println();43 } // end list44 } // end class PetMouse

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

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 9
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT