Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Help. New to Java.... Create a class named Horse that contains data field

ID: 3602286 • Letter: P

Question

Please Help. New to Java....

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.

//Horse.java
public class Horse
{
//instance variables
private String name;
private String color;
private int birthYear;
//construcor to set name, color and birth year
public Horse(String name, String color, int birthYear)
{
this.name=name;
this.color=color;
this.birthYear=birthYear;
}
//set name of horse class
public void setName(String name)
{
this.name=name;
}
//set color
public void setColor(String color)
{
this.color=color;
}
//set birth year
public void setBirthYear(int birthYear)
{
this.birthYear=birthYear;
}
//returns name
public String getName()
{
return name;
}
//returns color
public String getColor()
{
return color;
}
//returns birth year
public int getBirthYear()
{
return birthYear;
}
}

//RaceHorse.java
public class RaceHorse extends Horse
{
//number of races
private int races;
/*
*contructor that calls parent class Horse
*constructor with super keyword
*/
public RaceHorse(String name, String color, int birthYear, int races)
{
//calling Horse class constructor
super(name, color, birthYear);
//set races
this.races=races;
}
//set number of races
public void setRaces(int races)
{
this.races=races;
}
//returns races
public int getRaces()
{
return races;
}
}

//DemoHorse.java
public class DemoHorse
{
public static void main(String[]args)
{
//create an instance of class horse
Horse horse = new Horse("Jockey","black", 1990);
System.out.println("Horse object");
System.out.println("Name:"+horse.getName());
System.out.println("Color:"+horse.getColor());
System.out.println("Date of Birth:"+horse.getBirthYear());
//create an instance of class RaceHorse
RaceHorse racehorse=new RaceHorse("Jockey" + "Brother","black",1988,50);
System.out.println("RaceHorse object");
System.out.println("Name:"+RaceHorse.getName());
System.out.println("Color:"+RaceHorse.getColor());
System.out.println("Date of Birth:"+RaceHorse.getBirthYear());
System.out.println("Number of races:"+RaceHorse.getRaces());
}
}

Explanation / Answer

Everything is asked in Question is done

You have Horse,java with getters and setters as well as related fields


/Horse.java
public class Horse
{
//instance variables
private String name;
private String color;
private int birthYear;
//construcor to set name, color and birth year
public Horse(String name, String color, int birthYear)
{
this.name=name;
this.color=color;
this.birthYear=birthYear;
}
//set name of horse class
public void setName(String name)
{
this.name=name;
}
//set color
public void setColor(String color)
{
this.color=color;
}
//set birth year
public void setBirthYear(int birthYear)
{
this.birthYear=birthYear;
}
//returns name
public String getName()
{
return name;
}
//returns color
public String getColor()
{
return color;
}
//returns birth year
public int getBirthYear()
{
return birthYear;
}
}

============================================================================
You have RaceHorse.java that extends Horse and you have DemoHorse.java that calls object as Test class
Everything is there