Question
1. Draw a UML class diagram for the exercise (code is blow) 2. Given an existing class X and you would like to reuse it
1. Draw a UML class diagram for the exercise (code is blow)
2. Given an existing class X and you would like to reuse it to create a new class, when do you utilize inheritance instead of aggregation?
public class DogTest
{
public static void main(String[] args)
{
Dog objLabrador = new Labrador("Jackson", "yellow");
Dog objYorkshire = new Yorkshire("Layla", "Black and Gold");
System.out.println(objLabrador.getName() + " says " + objLabrador.speak());
System.out.println("The average weight of a Labrador is " + objLabrador.avgBreedWeight());
System.out.println();
System.out.println(objYorkshire.getName() + " says " + objYorkshire.speak());
System.out.println("The average weight of a Yorkshire is " + objYorkshire.avgBreedWeight());
}
}
public abstract class Dog
{
protected String name;
public Dog(String name)
{
this.name = name;
}
// ------------------------------------------------------------
// Returns the dog's name
// ------------------------------------------------------------
public String getName()
{
return name;
}
// ------------------------------------------------------------
// Returns a string with the dog's comments
// ------------------------------------------------------------
public String speak()
{
return "Woof";
}
public abstract int avgBreedWeight();
}
public class Labrador extends Dog
{
private String color; //black, yellow, or chocolate?
private int breedWeight = 75;
public Labrador(String name, String color)
{
super(name);
this.color = color;
}
public String speak()
{
return "WOOF";
}
public int avgBreedWeight()
{
return breedWeight;
}
}
public class Yorkshire extends Dog
{
private String color;
private int breedWeight = 13;
public Yorkshire(String name, String color)
{
super(name);
this.color = color;
}
public String speak()
{
return "woof";
}
public int avgBreedWeight()
{
return breedWeight;
}
}
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