Create a class named Horse that contains data fields for the

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. Program 1 Submission Template Fields: Strategy for coding the program (Provide informal explanation as to how you approached the problem. What were your given information (input) and expected output? What Java libraries did you need to use): ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________

Sample output (Provide screenshots of it running):

Source code (Provide all Java code you have written.):

Solution

public class Horse {

   private String name;
   private String color;
   private int birthYear;

   public Horse() {
       // TODO Auto-generated constructor stub

   }

   /**
   * @param name
   * @param color
   * @param birthYear
   */
   public Horse(String name, String color, int birthYear) {
       this.name = name;
       this.color = color;
       this.birthYear = birthYear;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the color
   */
   public String getColor() {
       return color;
   }

   /**
   * @param color
   * the color to set
   */
   public void setColor(String color) {
       this.color = color;
   }

   /**
   * @return the birthYear
   */
   public int getBirthYear() {
       return birthYear;
   }

   /**
   * @param birthYear
   * the birthYear to set
   */
   public void setBirthYear(int birthYear) {
       this.birthYear = birthYear;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Horse [name=\" + name + \", color=\" + color + \", birthYear=\"
               + birthYear + \"]\";
   }

}

public class RaceHorse extends Horse {

   private int numberOfRaces;

   /**
   *
   */
   public RaceHorse() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param name
   * @param color
   * @param birthYear
   */
   public RaceHorse(String name, String color, int birthYear, int numberOfRaces) {
       super(name, color, birthYear);
       // TODO Auto-generated constructor stub
       this.numberOfRaces = numberOfRaces;
   }

   /**
   * @return the numberOfRaces
   */
   public int getNumberOfRaces() {
       return numberOfRaces;
   }

   /**
   * @param numberOfRaces
   * the numberOfRaces to set
   */
   public void setNumberOfRaces(int numberOfRaces) {
       this.numberOfRaces = numberOfRaces;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"RaceHorse [numberOfRaces=\" + numberOfRaces + \", toString()=\"
               + super.toString() + \"]\";
   }

}

public class DemoHorses {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       RaceHorse horse1 = new RaceHorse(\"Horse name\", \"Black\", 2000, 6);

       System.out.println(horse1);

   }

}

OUTPUT:

RaceHorse [numberOfRaces=6, toString()=Horse [name=Horse name, color=Black, birthYear=2000]]

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
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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site