Question
Please Help. New to Java.... Create a class named Horse that contains data fields for the name, color, and birth year. Include get and set
Please Help. New to Java....
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.
//Horse.java public class Horse { //instance variables private String name; private String color; private int birthYear; //construcor to set name, color and birth year public Horse(String name, String color, int birthYear) { this.name=name; this.color=color; this.birthYear=birthYear; } //set name of horse class public void setName(String name) { this.name=name; } //set color public void setColor(String color) { this.color=color; } //set birth year public void setBirthYear(int birthYear) { this.birthYear=birthYear; } //returns name public String getName() { return name; } //returns color public String getColor() { return color; } //returns birth year public int getBirthYear() { return birthYear; } }
//RaceHorse.java public class RaceHorse extends Horse { //number of races private int races; /* *contructor that calls parent class Horse *constructor with super keyword */ public RaceHorse(String name, String color, int birthYear, int races) { //calling Horse class constructor super(name, color, birthYear); //set races this.races=races; } //set number of races public void setRaces(int races) { this.races=races; } //returns races public int getRaces() { return races; } }
//DemoHorse.java public class DemoHorse { public static void main(String[]args) { //create an instance of class horse Horse horse = new Horse("Jockey","black", 1990); System.out.println("Horse object"); System.out.println("Name:"+horse.getName()); System.out.println("Color:"+horse.getColor()); System.out.println("Date of Birth:"+horse.getBirthYear()); //create an instance of class RaceHorse RaceHorse racehorse=new RaceHorse("Jockey" + "Brother","black",1988,50); System.out.println("RaceHorse object"); System.out.println("Name:"+RaceHorse.getName()); System.out.println("Color:"+RaceHorse.getColor()); System.out.println("Date of Birth:"+RaceHorse.getBirthYear()); System.out.println("Number of races:"+RaceHorse.getRaces()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started