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

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...
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...
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...
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...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - 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....
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
Write a Data Element Class named Property that has fields tohold the property name, the...
Write a Data Element Class named Property that has fields to hold the property name, the city where the property is located, the rent amount, the owner's name, and the Plot to be occupied by the property, along with getters and setters to access and set these fields. Write a parameterized constructor (i.e., takes values for the fields as parameters) and a copy constructor (takes a Property object as the parameter). Follow the Javadoc file provided.Write a Data Element Class...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT