This is the question I am struggling on -
Create a class named Horse that contains the following data fields:
- name - of type String
- color - of type String
- birthYear - of type int
Include get and set methods for these fields.
Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field.
This is the code I have but it will not accept it at all -
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.");
}
}
----------------------------------
public class Horse
{
private String name;
private String color;
private int birthYear;
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setColor(String c)
{
color = c;
}
public String getColor()
{
return color;
}
public void setBirthYear(int yr)
{
birthYear = yr;
}
public int getBirthYear()
{
return birthYear;
}
}
----------------------------------------
public class RaceHorse
{
// Define the RaceHorse class here
private int races;
public void setRaces(int numR)
{
races = numR;
}
public int getRaces()
{
return races;
}
}
Here is what it is telling me -
Build Output
DemoHorses.java:10: error: cannot find symbol horse2.setName("Champion"); ^
symbol: method setName(String)
location: variable horse2 of type RaceHorse DemoHorses.java:11: error: cannot find symbol horse2.setColor("black"); ^
symbol: method setColor(String)
location: variable horse2 of type RaceHorse DemoHorses.java:12: error: cannot find symbol horse2.setBirthYear(2011); ^ symbol: method setBirthYear(int) location: variable horse2 of type RaceHorse DemoHorses.java:15: error: cannot find symbol System.out.println(horse2.getName() + " is " + horse2.getColor() + " and was born in " + horse2.getBirthYear() + "."); ^ symbol: method getName() location: variable horse2 of type RaceHorse DemoHorses.java:15: error: cannot find symbol System.out.println(horse2.getName() + " is " + horse2.getColor() + " and was born in " + horse2.getBirthYear() + "."); ^ symbol: method getColor() location: variable horse2 of type RaceHorse DemoHorses.java:15: error: cannot find symbol System.out.println(horse2.getName() + " is " + horse2.getColor() + " and was born in " + horse2.getBirthYear() + "."); ^ symbol: method getBirthYear() location: variable horse2 of type RaceHorse DemoHorses.java:16: error: cannot find symbol System.out.println(horse2.getName() + " has been in " + horse2.getRaces() + " races."); ^ symbol: method getName() location: variable horse2 of type RaceHorse
7 errors
Test Contents@Test public void unitTest() { Horse horse = new Horse(); horse.setName("Old Paint"); assertEquals(horse.getName(), "Old Paint"); horse.setName("Champion"); assertEquals(horse.getName(), "Champion"); }