File Dog.java contains a declaration for a Dog class. Study it—notice what instance variables and methods are provided. Files Labrador.java and Yorkshire.java contain declarations for classes that extend Dog. Study these files as well.

File DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java and compile and run it to see what it does. Now modify these files as follows:

1. In the Labrador.java, you should find following error in the constructor method Implicit super constructor Dog() is undefined. Must explicitly invoke another constructor

Fix the problem (which really is in Labrador) so that constructor of Labrador.java is error free.

Hint: Every constructor calls its superclass constructor. An implied super() is therefore included in each constructor which invokes a constructor of the super class. The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor. The explicit super allows parameter values to be passed to the constructor of its superclass and must have matching parameter types. A super(variable inputs) call in the constructor of a subclass will result in the call of the relevant constructor from the superclass, based on the signature of the call. This is called constructor chaining.

2. Add statements in the Labrador constructor method, setImage of the Labrador using fileName provided during object instantiation and setBreed as Labrador

3. In the Yorkshire.java, you should find undefined errors in the constructor method. Yorkshire program is supposed to extend Dog class, but extend statement is missing in the program. Undefined error in the constructor is caused due to missing extends statement. Add extends statement to fix these errors.

3. Add avgBreedWeight() methods for both Labrador and Yorkshire. For Labrador method should return value of 75 and for Yorkshire return value of 6. Add appropriate statements in the DogTest.java to print the average breed weight for both your Labrador and your Yorkshire.

4. Add statements in DogTest.java to create a Yorkshire and a Labrador objects. Add statements to add JLabel that displays average weight for Labrador and Yorkshire.

Note that the Labrador constructor takes three parameters: the name, color, and file name of the picture of the Labrador; while the Yorkshire constructor takes two parameters: name and file name of the picture of Yorkshire.

Sample Screen shot of the program See image.

//********************************************************************
//DogTest.java
//A simple test class that creates a Dog and displays a funny picture.
//********************************************************************

import javax.swing.*;

public class DogTest
{
public static void main(String[] args)
{
//Create a panel to which different Dog class objects can be added
JPanel panel = new JPanel();
panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS));

//Create and add Dog object to panel
Dog dog = new Dog("SpikeD");
panel.add(dog);

//Create and add Labrador object to panel

//Create and add JLabel that displays weight of Labrador to the panel

//Create and add Yorkshire object to panel

//Create and add JLabel that displays weight of Yorkshire to the panel

//Create JFrame object and add panel to it
JFrame frame = new JFrame ("Exploring Inheritance");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

}
}

//****************************************************************
//Dog.java
//A class that holds a dog's name, picture, and breed name.
//****************************************************************

import javax.swing.*;

public class Dog extends JPanel
{
protected String name;
private ImageIcon image;
private JLabel namelabel;
private JLabel imglabel;
private JLabel breedlabel;
private JLabel speaklabel;

// ------------------------------------------------------------
// Constructor for Dog
// ------------------------------------------------------------
public Dog(String name)
{
//sets breed name and displays it
breedlabel = new JLabel("Sample Dog");
add(breedlabel);

JLabel dash = new JLabel (" - ");
add(dash);

//sets user provided value to name and displays it
this.name = name;
namelabel = new JLabel(name);
add(namelabel);

//sets Dog image and displays it
image = new ImageIcon ("dog1.gif");
imglabel = new JLabel(image);
add(imglabel);

//sets speaks JLabel
speaklabel = new JLabel(speak());
add(speaklabel);
}

// ------------------------------------------------------------
// Returns the dog's name
// ------------------------------------------------------------
public String getName()
{
return name;
}

// ------------------------------------------------------------
// Returns a string with the dog's comments
// ------------------------------------------------------------
public String speak()
{
return "Woof";
}

// ------------------------------------------------------------
// Set Dog's Image to user provided Image
// ------------------------------------------------------------
public void setImage(String fileName)
{
image = new ImageIcon (fileName);
imglabel.setIcon(image);
}

// ------------------------------------------------------------
// Set Dog's Breed to user provided name
// ------------------------------------------------------------
public void setBreed(String breedName)
{
breedlabel.setText(breedName);
}

}

//****************************************************************
//Labrador.java
//A class derived from Dog that holds information about
//a labrador retriever. Overrides Dog speak method and includes
//information about avg weight for this breed.
//****************************************************************

public class Labrador extends Dog
{
private String color; //black, yellow, or chocolate?


public Labrador(String name, String color, String fileName)
{
this.color = color;
}

// ------------------------------------------------------------
// Big bark -- overrides speak method in Dog
// ------------------------------------------------------------
public String speak()
{
return "WOOF";
}

// ------------------------------------------------------------
// Returns weight
// ------------------------------------------------------------


}


//****************************************************************
//Yorkshire.java
//A class derived from Dog that holds information about
//a Yorkshire terrier. Overrides Dog speak method.
//****************************************************************

public class Yorkshire
{

public Yorkshire(String name, String fileName)
{
super(name);
setImage(fileName);
setBreed("Yorkshire");
}

// ------------------------------------------------------------
// Small bark -- overrides speak method in Dog
// ------------------------------------------------------------


// ------------------------------------------------------------
// Returns weight
// ------------------------------------------------------------

}

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.