Question

In: Computer Science

Create a class named Horse that contains the following data fields: name - of type String...

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

    {

       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.");

    }

}

----------------------------------------------

Solutions

Expert Solution

class Horse {
        // fileds for Horse class
        private String name;
        private String color;
        private int birthYear;

        
        //setters and getters
        public String getName() {
                return name;
        }

        public String getColor() {
                return color;
        }

        public int getBirthYear() {
                return birthYear;
        }

        public void setName(String aName) {
                name = aName;
        }

        public void setColor(String aColor) {
                color = aColor;
        }

        public void setBirthYear(int aBirthYear) {
                birthYear = aBirthYear;
        }

}
// RaceHorse extending the Horse
class RaceHorse extends Horse {
        private int races;

        //setters and getters
        public int getRaces() {
                return races;
        }

        public void setRaces(int aRaces) {
                races = aRaces;
        }

}

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.");

        }

}


Related Solutions

Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the 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. Run the provided DemoHorses application that demonstrates using objects of each class. DemoHorses.java public class DemoHorses { public...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT