In this lab, we will use inheritance to create several subclasses that inherit f
ID: 3700684 • 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.
Explanation / Answer
Class Dog
=============================
public 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 which Inherits the Dog Class
=============================================
public class Poodle extends Dog {
public Poodle(String name) {
super(name);
}
@Override
public String speak() {
return "yip yip yip";
}
}
PitBull Class Which Inherits the Dog Class
==================================================
public class PitBull extends Dog {
public PitBull(String name) {
super(name);
}
@Override
public String speak() {
return "grrr";
}
}
Hound Class Which Inherits the Dog Class
======================================
public class Hound extends Dog {
public Hound(String name) {
super(name);
}
@Override
public String speak() {
return "ooooo";
}
}
The DogShow Class Which Contains the main Method and Implements working of all the classes
==================================================================================
import java.util.ArrayList;
public class DogShow {
public static int judgeDog(Dog dog) {
return (int)(Math.random()*100) + 1;
}
public static void chooseBestInShow(Dog[] dog) {
for(int a =0; a < dog.length; a++) {
for(int b =1; b < dog.length-a; b++) {
if (judgeDog(dog[b - 1]) < judgeDog(dog[b])) { //Simple Comparison made on the value return by judgeDog method
Object temp = dog[b - 1];
dog[b - 1] = dog[b];
dog[b] = (Dog)temp;
}
}
}
System.out.println(dog[0].getName()+" wins best in show!");
System.out.println(dog[0].speak());
}
//Overloaded method of chooseBestInShow which accept ArrayList as a parameter
public static void chooseBestInShow(ArrayList<Dog> dog) {
for(int a =0; a < dog.size(); a++) {
for(int b =1; b < dog.size()-a; b++) {
if (judgeDog(dog.get(b - 1)) < judgeDog(dog.get(b))) {//Simple Comparison made on the value return by judgeDog method
Object temp = dog.get(b - 1);
dog.set(b - 1, dog.get(b));
dog.set(b, (Dog) temp);
}
}
}
System.out.println(dog.get(0).getName()+" wins best in show!");
System.out.println(dog.get(0).speak());
}
public static void main(String[] args) {
Dog[] dog = {new Poodle("Alex"), new PitBull("Blake"), new Hound("Carl")};
ArrayList<Dog> dogArrayList = new ArrayList<>();
dogArrayList.add(new Poodle("Alex"));
dogArrayList.add(new PitBull("Blake"));
dogArrayList.add(new Hound("Carl"));
chooseBestInShow(dog);
chooseBestInShow(dogArrayList);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.