Need Help ?

Home / Expert Answers / Other / exploring-inheritance-file-dog-java-contains-a-declaration-for-a-dog-class-save-this-file-to-your-di

(Answered): Exploring Inheritance File Dog java contains a declaration for a Dog class. Save this file to your ...




Exploring Inheritance File Dog java contains a declaration for a Dog class. Save this file to your directory and study itnoti17 77 Dog.java 11 1/ A class that holds a dogs name and can make it speak. 77 +++++ public class Dog protected string name;H1 Labrador.java 11 A class derived from Dog that holds information about I a labrador retriever. Overrides Dog speak method// // Yorkshire.java // // A class derived from Dog that holds information about // a Yorkshire terrier. Overrides Dog speak
Exploring Inheritance File Dog java contains a declaration for a Dog class. Save this file to your directory and study itnotice what instance variables and methods are provided. Files Labrador java and Yorkshire java contain declarations for classes that extend Dog. Save and study these files as well. File Dog Test.java contains a simple driver program that creates a dog and makes it speak. Study Dog Test.java, save it to your directory, and compile and run it to see what it does. Now modify these files as follows: 1. Add statements in DogTestjava after you create and print the dog to create and print a Yorkshire and a Labrador. Note that the Labrador constructor takes two parameters: the name and color of the labrador, both strings. Don't change any files besides Dog Test.java. Now recompile DogTest.java; you should get an error saying something like / Labrador.java:18: Dog (java.lang.String) in Dog cannot be applied to () 1 error If you look at line 18 of Labrador java it's just a f, and the constructor the compiler can't find (Dog() isn't called anywhere in this file What's going on? (Hint: What call must be made in the constructor of a subclass?) 2 b. Fix the problem (which really is in Labrador) so that Dog Test.java creates and makes the Dog. Labrador, and Yorkshire all speak 2. Add code to Dog Testjava to print the average breed weight for both your Labrador and your Yorkshire Use the avgBreedWeight() method for both. What error do you get? Why? Fix the problem by adding the needed code to the Yorkshire class. Add an abstract intag BreedWeight() method to the Dog class. Remember that this means that the word abstract appears in the method header after public, and that the method does not have a body (just a semicolon after the parameter list). It makes sense for this to be abstract since Dog has no idea what breed it is. Now any subclass of Dog must have an avgreedWeight method, since both Yorkshire and Laborador do, you should be all set Save these changes and recompile Dog Testjava You should get an error in Dog java (unless you made more changes than described above). Figure out what's wrong and fix this error, then recompile Dog Testjava. You should get another error this time in Dogfest java. Read the error message carefully, it tells you exactly what the problem is. Fix this by changing Dog Test (which will mean taking some things out) 17 77 Dog.java 11 1/ A class that holds a dog's name and can make it speak. 77 +++++ public class Dog protected string name; // // Constructor store name public Dog (String name) this.name = name; - 17 7/ Returns the dog's name // public String getName() return name 1 17 // Returns a string with the dog's comments puble String speak return "Woof H1 Labrador.java 11 A class derived from Dog that holds information about I a labrador retriever. Overrides Dog speak method and includes // information about avg weight for this breed. 77 77 **** public class Labrador extends Dog private String color; //black, yellow, or chocolate? private int breedweight = 75, public Labrador (String name, String color) this.color = color: ) 1 Big bark overrides speak method in Dog public String speak() return *WOOF: 11 Returns weight public static int avgereedWeight) return breedweight: // // Yorkshire.java // // A class derived from Dog that holds information about // a Yorkshire terrier. Overrides Dog speak method. public class Yorkshire extends Dog public Yorkshire (String name) super (name); ) 77 // Small bark -- overrides speak method in Dog 17 public String speak() return "woof"; ( // // Dog'Test.java 17 // A simple test class that creates a Dog and makes it speak. 17 public class DogTest public static void main(String[] args) Dog dog = new Dog("Spike"): System.out.println(dog.getName() says + dog. speak (1):


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer to Exploring Inheritance File Dog java contains a declaration for a Dog class. Save this file to your directory and study i...

The Problem has Answer!

We have detailed solutions for you for more understanding.

View Answer