Question
1.1 It is often the case that two or more classes share a common set of methods. For programming purposes we might wish to treat
1.1 It is often the case that two or more classes share a common set of methods. For programming purposes we might wish to treat the objects of those classes in a similar way by invoking some of their common routines. For example, the Dog and Cat classes listed below agree on the void method speak. Because Dog and Cat objects have the ability to speak, it is natural to think of putting both types of objects in an ArrayList and invoking speak on every object in the list. Is this possible? Certainly we could create an ArrayList of Dog that would hold all the Dog objects, but can we then add a Cat object to an ArrayList of Dog? Try running the main program below as it is written. Run it a second time after uncommenting the line that instantiates a Cat object and tries to add it to the ArrayList. import java.util.*; public class AnimalRunner { public static void main(String[] args) { ArrayList dogcatList = new ArrayList(); dogcatList.add(new Dog("Fred")); // dogcatList.add(new Cat("Wanda")); } } ------------------- public class Dog { private String name; public Dog(String name) { this.name = name; } public void speak() { System.out.println("Woof! Woof!"); } public String toString() { return "Dog: " + name; } } ------------------- public class Cat { private String name; public Cat(String name) { this.name = name; } public void speak() { System.out.println("Meow! Meow!"); } public String toString() { return "Cat: " + name; } } Our experiment to add Cat objects to an ArrayList of Dog objects failed. Perhaps we should try using the original Java ArrayList without generics? Try running the code below as it is written along with the Dog and Cat classes defined above. Run it a second time after uncommenting the line that invokes speak. import java.util.*; public class AnimalRunner { public static void main(String[] args) { ArrayList dogcatList = new ArrayList(); dogcatList.add(new Dog("Fred")); // dogList.add(new Cat("Wanda")); for (Object obj : dogcatList) { // obj.speak(); } } } The experiment shows that we are now able to add Dog and Cat objects to the ArrayList, but there is a compile error on the line obj.speak because obj is an Object reference variable and the class Object doesnt contain a speak method. We need a reference variable that can refer to Dog and Cat objects and which also allows us to invoke speak. The solution to the problem uses interfaces. First create an interface called Speakable that contains a void speak() method signature. Be sure to modify the Dog and Cat classes to indicate that they implement the Speakable interface. For example, in the case of the Dog class, we will code public class Dog implements Speakable. Be sure to make a similar change in the declaration of the Cat class. The term Speakable can be used to create Speakable references. Using generics, create an ArrayList of Speakable objects in the main method. Modify the for loop so that it iterates over Speakable objects. Try adding the Dog and Cat objects and invoking the speak method on each object. Does this work? 1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();. public class AnimalRunner { public static void main(String[] args) { Dog d1 = new Dog("Fred"); d1.speak(); Object obj = new Dog("Connie"); // obj.speak(); } } The uncommented line causes a compile error because obj is an Object reference variable and class Object doesnt contain a speak() method. This is the case, in spite of the fact that obj refers to a Dog object and that class Dog has a speak() method. For safety, the Java compiler limits the methods that can be called using a reference variable to only those methods that belong to the class that was used to create the reference variable. So obj can only invoke Object methods. Use the following method to make Connie speak: Create a second Dog reference variable d2. Cast obj to a Dog and assign the new reference to d2. Now invoke speak on d2. What happens if you create a Cat object and try to cast it to a Dog? 2.1. The Comparable interface is a commonly used interface in Java. Look up the Comparable interface in the API documentation. If you wanted to modify the Dog class so that it implements the Comparable interface, what method(s) do you need to implement? Give the method signature(s), that is, the return type(s), the method name(s), and the method parameter(s) needed. 2.2. The compareTo method compares two parameters, the implicit and explicit parameter. The call a.compareTo(b)returns a positive integer if a is larger than b a negative integer if a is smaller than b 0 if a and b are the same Make your Dog class implement Comparable. Write the compareTo method of the Dog class so that it compares the names of the dogs. Some of the code has been provided for you: public class Dog implements Comparable { /** Compares two Dogs. @param other the other Dog @return 1 if this Dogs name comes after the other one in alphabetic order -1 if this Dog comes before, and 0 if both Dogs have the same name */ public int compareTo(Dog other) { . . . } } 2.3. The sort method of the Collections class can sort a list of objects whose classes implement the Comparable interface. Here is the outline of the required code. import java.util.ArrayList; import java.util.Collections; . . . // Put Dogs into a list ArrayList list = new ArrayList(); list.add(new Dog("Fido"); list.add(new Dog("Bub"); list.add(new Dog("Zeus); // Call the library sort method Collections.sort(list); // Print out the sorted list for (int i = 0; i < list.size(); i++) { Dog d = list.get(i); System.out.println(d); } Using this outline, write a test program that sorts a list of five dogs. 2.4. Change your compareTo method by switching the positive and negative return values. Recompile and run the test program again. What is the outcome of executing your test program? Explain the changed output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started