In this lab, we will use inheritance to create several subclasses that inherit f
ID: 3700629 • Letter: I
Question
In this lab, we will use inheritance to create several subclasses that inherit functionality from a superclass. We will also use an array or an ArrayList to pass a set of objects of the same type to a method for processing.
This lab will simulate a dog show. We will create several classes representing different species of dogs. Each of these classes will extend the Dog class, provided as an attachment.
Create the following three classes, each of which extends the Dog class. For each class, override the speak() method to return the specified String:
Poodle: speak() must return "yip yip yip"
PitBull: speak() must return "grrr"
Hound: speak() must return "ooooo"
Also, create a static method in the provided DogShow class that evaluates some number of Dogs and chooses one as best in show -- the winning dog of the dog show. The method should be named chooseBestInShow, and it should accept a number of Dogs formatted as an array, an ArrayList, or a variable length argument. Call the provided judgeDog() method to get a score for each Dog; the winner will be the Dog with the highest score. If there is a tie, choose any of the Dogs with the highest score.
Finally, create a main method in DogShow to simulate a dog show. Create the following dogs:
a Poodle named Alex
a PitBull named Blake
a Hound named Carl
Use the chooseBestInShow method to find a winning dog. Print the winning dog's name, and print the output of the dog's speak() method. For example, if Alex the Poodle is the winner, the program should print the following:
Upload the Poodle, PitBull, Hound, and updated DogShow source files to Blackboard. For 15 points extra credit, overload three versions of the chooseBestInShow() method: one that accepts an array, one that accepts an ArrayList, and one that accepts a variable length argument.
Using
and using
Explanation / Answer
class DogShow {
public static void main (String[] args)
{
Dog[] d = new Dog[3];
d[0] = new Poodle("Alex");
d[1] = new PitBull("Blake");
d[2] = new Hound("Carl");
chooseBestInShow(d);
}
public static int judgeDog(Dog dog) {
// generate a random value between 1 and 100
return (int)(Math.random()*100) + 1;
}
public static void chooseBestInShow(Dog[] d)
{
int highest = 0;
String name = " ";
for(int i=0;i<d.length;i++)
{
if(highest <judgeDog(d[i])) // find highest score and the name of the dog
{
highest = judgeDog(d[i]);
name = d[i].getName();
}
}
System.out.println("The dog " + name +" has the highest score : "+ highest + " and is the winner ");
}
}
class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String speak() {
return "woof";
}
}
class Poodle extends Dog
{
public Poodle(String name)
{
super(name);
}
public String speak()
{
return "yip yip yip";
}
}
class PitBull extends Dog
{
public PitBull(String name)
{
super(name);
}
public String speak()
{
return "grrr";
}
}
class Hound extends Dog
{
public Hound(String name)
{
super(name);
}
public String speak()
{
return "ooooo";
}
}
output:
The dog Carl has the highest score : 76 and is the winner
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.