In: Computer Science
Introduction to Inheritance (Exercise 1)
Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field.
Run the provided DemoHorses application that demonstrates using objects of each class.
DemoHorses.java
public class DemoHorses
{
public static void main(String args[])
{
Horse horse1 = new Horse();
RaceHorse horse2 = new RaceHorse();
horse1.setName("Old Paint");
horse1.setColor("brown");
horse1.setBirthYear(2009);
horse2.setName("Champion");
horse2.setColor("black");
horse2.setBirthYear(2011);
horse2.setRaces(4);
System.out.println(horse1.getName() + " is " +
horse1.getColor() + " and was born in " + horse1.getBirthYear() +
".");
System.out.println(horse2.getName() + " is " +
horse2.getColor() + " and was born in " + horse2.getBirthYear() +
".");
System.out.println(horse2.getName() + " has been in " +
horse2.getRaces() + " races.");
}
}
Horse.java
public class Horse
{
// add private variables here
public String getName()
{
// write method code here
}
public String getColor()
{
// write method code here
}
public int getBirthYear()
{
// write method code here
}
public void setName(String n)
{
// write method code here
}
public void setColor(String c)
{
// write method code here
}
public void setBirthYear(int y)
{
// write method code here
}
}
RaceHorse.java
// Extend the Horse class as RaceHorse here
{
// add private variables here
public int getRaces()
{
// write method code here
}
public void setRaces(int r)
{
// write method code here
}
}
public class DemoHorses {
public static void main(String args[]) {
Horse horse1 = new Horse();
RaceHorse horse2 = new
RaceHorse();
horse1.setName("Old Paint");
horse1.setColor("brown");
horse1.setBirthYear(2009);
horse2.setName("Champion");
horse2.setColor("black");
horse2.setBirthYear(2011);
horse2.setRaces(4);
System.out.println(
horse1.getName() + " is " + horse1.getColor() +
" and was born in " + horse1.getBirthYear() + ".");
System.out.println(
horse2.getName() + " is " + horse2.getColor() +
" and was born in " + horse2.getBirthYear() + ".");
System.out.println(horse2.getName()
+ " has been in " + horse2.getRaces() + " races.");
}
}
class Horse {
private String name;
private String color;
private int birthYear;
// add private variables here
public String getName() {
return name;
// write method code here
}
public String getColor() {
return color;
// write method code here
}
public int getBirthYear() {
return birthYear;
// write method code here
}
public void setName(String n) {
name = n;
// write method code here
}
public void setColor(String c) {
color = c;
// write method code here
}
public void setBirthYear(int y) {
birthYear = y;
// write method code here
}
}
class RaceHorse extends Horse
// Extend the Horse class as RaceHorse here
{
// add private variables here
int races;
public int getRaces() {
return races;
// write method code here
}
public void setRaces(int r) {
// write method code here
races = r;
}
}