Create a class named Horse that contains data fields for the name, color, and bi
ID: 3568465 • Letter: C
Question
Create a class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, create a 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. Write an application that demonstrates using objects of each class. Save the files as Horse.java, RaceHorse.java, and DemoHorses.java.
The answer that are already provided do not compile correctly without errors. I am looking for a perfectly compiled answer.
Explanation / Answer
//Three classes are provided.
//Horse.java
public class Horse
{
//instance variable of class
private String name;
private String color;
private int birthYear;
//constructor to set name ,color and birth year
public Horse(String name,String color,int birthYear)
{
this.name=name;
this.color=color;
this.birthYear=birthYear;
}
//getter metods for name,color and birth year
public int getBirthYear() {
return birthYear;
}
public String getColor()
{
return color;
}
public String getName() {
return name;
}
//setter metods for name,color and birht year
public void setBirthYear(int birthYear)
{
this.birthYear=birthYear;
}
public void setColor(String color)
{
this.color=color;
}
public void setName(String name) {
this.name=name;
}
//String description of Horse class object
@Override
public String toString() {
return "Name : "+getName()+
" Color : "+getColor()+
" Birth Year : "+getBirthYear();
}
}
---------------------------------------------------------------------------------------
//The class RaceHorse extends from the super class Horse
//and create a constructor that calls the super class
//constructor to set the horse fields
//RaceHorse.java
public class RaceHorse extends Horse
{
private int numRacesCompeted;
//constructor to set the name,color ,birt year and number of races competed
public RaceHorse(String name,String color,int birthYear,int numRacesCompeted)
{
super(name, color, birthYear);
this.numRacesCompeted=numRacesCompeted;
}
//setter method for number of races
public void setRacesCompeted(int numRacesCompeted)
{
this.numRacesCompeted=numRacesCompeted;
}
//getter method retuerns the number of races
public int getRacesCompeted()
{
return numRacesCompeted;
}
//override the toString metod
@Override
public String toString() {
return super.toString()+" Races competed : "+getRacesCompeted();
}
}
----------------------------------------------------------------------------------------------------
//Demo class
//The java program demonstrates the classes Horse and
//RaceHorse and display the objects data.
//DemoHorses.java
public class DemoHorses
{
public static void main(String[] args)
{
//create an instace of the RaceHorse1 subcalss
RaceHorse raceHorse1=new RaceHorse("Bond-Horse", "Black", 2000, 25);
//call the toString metod on raceHorse object
System.out.println(raceHorse1.toString());
//create an instace of the RaceHorse2 subcalss
RaceHorse raceHorse2=new RaceHorse("Super-Horse", "White", 2001, 20);
//call the toString metod on raceHorse object
System.out.println(raceHorse2.toString());
}
}
------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Name : Bond-Horse
Color : Black
Birth Year : 2000
Races competed : 25
Name : Super-Horse
Color : White
Birth Year : 2001
Races competed : 20
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.