1. Create a new Java Project. 2. Create a class named Horse that contains data f
ID: 3721560 • Letter: 1
Question
1. Create a new Java Project.
2. Create a class named Horse that contains data fields for the name, color, and year born. Include get and set methods for these fields.
3. 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.
4. Create an application named TestHorse that demonstrates using objects of each class. All data is in TestHorse.
Sample Output:
Old Paint is brown and was born in 2009.
Champion is black and was born in 2011.
Champion has been in 4 races.
Explanation / Answer
Please find my implementation.
public class Horse {
private String name;
private String color;
private int birthYear;
public void setName(String name) {
this.name = name;
} // end setName()
public String getName() {
return name;
} // end getName()
public void setColor(String color) {
this.color = color;
} // end setColor()
public String getColor() {
return color;
} // end getColor()
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
} // end setBirthYear()
public int getBirthYear() {
return birthYear;
} // end getBirthYear()
} // end class Horse
I think you need something like this for your RaceHorse class:
public class RaceHorse extends Horse {
private String name;
private String color;
private int birthYear;
private int races;
public void setRaces(int races) {
this.races = races;
} // end setNumberRaces()
public int getRaces() {
return races;
} // end getNumberRaces()
} // end class RaceHorse
And then you need your application:
public class HorseDemo {
public static void main(String[] args) {
Horse henrietta = new Horse();
RaceHorse rosetta = new RaceHorse();
henrietta.setName("Henrietta");
henrietta.setColor("Bay");
henrietta.setBirthYear(2004);
rosetta.setName("Rosetta");
rosetta.setColor("Tan");
rosetta.setBirthYear(2007);
rosetta.setRaces(10);
System.out.println( " Horse:");
System.out.println( "Name: " + henrietta.getName());
System.out.println( "Color: " + henrietta.getColor());
System.out.println( "Birth Year: " + henrietta.getBirthYear());
System.out.println( " RaceHorse:");
System.out.println( "Name: " + rosetta.getName());
System.out.println( "Color: " + rosetta.getColor());
System.out.println( "Birth Year: " + rosetta.getBirthYear());
System.out.println( "Races: " + rosetta.getRaces());
} // end main()
} // end class HorseDemo
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.