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. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
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...
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...
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....
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT