Question
Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type
Java file
Name Dog Classes and Methods
Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a cattle herding dog. A Shiba Inu would be a hunting dog.
Create the setTopTrick() mutator method
Dog is parent class
Corgi and Driver are subclasses
Complete the Corgi class:
- Using the UML Class diagram, declare the instance variables.
- Create the two mutator methods for the instance variables.
Driver is subclass
then create the code:
- There should be no instance variables.
- The main() method will be the only method in the class.
- Write three lines of code in the main() method:
- Instantiate a corgi object using the below syntax:
className objectName = new className(input parameters) |
- TIP: Refer to the constructors in the Dog and Corgi classes to ensure the input parameters are correct.
- Use the objectName.setTopTrick() method to set a top trick for the dog you created.
- Embed the objectName.toString() method in a statement that outputs to the console window.
Sample output:
DOG DATA
Java is a Pembroke Welsh Corgi, a cattle herding dog.
The top trick is: ringing the bell to go outside.
The Corgi is 5 years old and weighs 38 pounds.
Dog |
type: string breed: string name: string topTrick: string |
setTopTrick(trick:string) toString() |
Corgi |
weight:int age:int |
setWeight(pounds:int) setAge(years:int) toString() |
Arrow pointing from table labeled Corgi to table labeled Dog
1st program : Parent Dog
public class Dog {
// instance variables
// constructor
// methods
// method used to print Dog information
public String toString() {
String temp = " DOG DATA " + name + " is a " + breed +
", a " + type + " dog. The top trick is : " +
topTrick + ".";
return temp;
}
}
2nd program Corgi
public class Corgi extends Dog {
// additional instance variables
// constructor
public Corgi(String type, String breed, String name, int pounds, int years) {
// invoke Dog class (super class) constructor
super(type, breed, name);
weight = pounds;
age = years;
}
// mutator methods
// override toString() method to include additional dog information
@Override
public String toString() {
return (super.toString() + " The Corgi is " + age +
" years old and weighs " + weight + " pounds.");
}
}
3rd program Driver (CockerSpaniel)
public class Driver extends Dog {
// additional instance variables
// constructor
public Driver(String type, String breed, String name, int pounds, int years) {
// invoke Dog class (super class) constructor
super(type, breed, name);
weight = pounds;
age = years;
}
// mutator methods
// override toString() method to include additional dog information
@Override
public String toString() {
return (super.toString() + " The Cocker Spaniel is " + age +
" years old and weighs " + weight + " pounds.");
}
}
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