Question

In: Computer Science

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

Horse.java

public class Horse
{
// add private variables here

public String getName()
{
// write method code here
}
public String getColor()
{
// write method code here
}
public int getBirthYear()
{
// write method code here
}
public void setName(String n)
{
// write method code here
}
public void setColor(String c)
{
// write method code here
}
public void setBirthYear(int y)
{
// write method code here
}
}


RaceHorse.java

// Extend the Horse class as RaceHorse here
{
// add private variables here

public int getRaces()
{
// write method code here
}
public void setRaces(int r)
{
// write method code here
}
}

Solutions

Expert Solution

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

class Horse {
   private String name;
   private String color;
   private int birthYear;
   // add private variables here

   public String getName() {
       return name;
       // write method code here
   }

   public String getColor() {
       return color;
       // write method code here
   }

   public int getBirthYear() {
       return birthYear;

       // write method code here
   }

   public void setName(String n) {
       name = n;
       // write method code here
   }

   public void setColor(String c) {
       color = c;
       // write method code here
   }

   public void setBirthYear(int y) {
       birthYear = y;
       // write method code here
   }
}

class RaceHorse extends Horse
// Extend the Horse class as RaceHorse here
{
   // add private variables here
   int races;

   public int getRaces() {
       return races;
       // write method code here
   }

   public void setRaces(int r) {
       // write method code here
       races = r;
   }
}


Related Solutions

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...
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. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
Define the circle2d class that contains: Two double data fields named x and y that specify...
Define the circle2d class that contains: Two double data fields named x and y that specify the center of the circle with getter methods A data field radius with a getter method A no arg constructor that creates a default circle with 0,0 for x,y and 1 for the radius A constructor that creates a circle with the specified x,y of the the circle A method getArea() that returns the area of the circle A method Contains(Double x, Double y)...
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...
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...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
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 int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT