Question

Create a class named Horse that contains data fields for the name, color, and birth year....

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.):

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Create a class named Horse that contains data fields for the name, color, and birth year....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

    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. DemoHorses.java public class DemoHorses { public static void main(String args[])...

  • Create a class named Book that contains data fields for the title and number of pages....

    Create a class named Book that contains data fields for the title and number of pages. Include get and set methods for these fields. Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class. Save the files as Book.java, Textbook.java, and DemoBook.java.

  • Create a class named Poem that contains the following fields: title - the name of the...

    Create a class named Poem that contains the following fields: title - the name of the poem (of type String) lines - the number of lines in the poem (of type int) Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. The constructor for each subclass requires only a title; the lines field is set using a constant value. A couplet has two lines, a...

  • a) Create an abstract class Student. The class contains fields for student Id number, name, and...

    a) Create an abstract class Student. The class contains fields for student Id number, name, and tuition. Includes a constructor that requires parameters for the ID number and name. Include get and set method for each field; setTuition() method is abstract. b) The Student class should throw an exception named InvalidNameException when it receives an invalid student name (student name is invalid if it contains digits). c) Create three student subclasses named UndergradStudent, GradeStudent, and StudentAtLarge, each with a unique...

  • Mick’s Wicks makes candles in various sizes. Create a class for the business named Candle that...

    Mick’s Wicks makes candles in various sizes. Create a class for the business named Candle that contains the following data fields: color - of type String height - of type int price - of type double Create get methods for all three fields. Create set methods for color and height, but not for price. Instead, when height is set, determine the price as $2 per inch. Create a child class named ScentedCandle that contains an additional data field named scent...

  • 1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and...

    1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and onion) and price. include a constructor fhay requires arguments for borh fields ans a method to diplay the data. Create a subclass names DelicedPizza that inherits from Pizza bit adds a delivery fee and a delicery address. The description, price, and delicery address are required as arguments to the constructor. The delivery fee is $3 if the pizza ordered cost more that $15; otherwise...

  • Create a class named BaseballGame that contains data fields for two team names and scores for...

    Create a class named BaseballGame that contains data fields for two team names and scores for each team in each of nine innings. names should be an array of two strings and scores should be a two-dimensional array of type int; the first dimension indexes the team (0 or 1) and the second dimension indexes the inning. Create get and set methods for each field. The get and set methods for the scores should require a parameter that indicates which...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

    FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT