Answer to Question #229451 in Java | JSP | JSF for DOEBOY*

Question #229451

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.


1
Expert's answer
2021-08-25T17:28:20-0400
public class Horse {
    private String name;
    private String color;
    private int birthYear;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}


public class RaceHorse extends Horse{
    private int numOfRaces;

    public int getNumOfRaces() {
        return numOfRaces;
    }

    public void setNumOfRaces(int numOfRaces) {
        this.numOfRaces = numOfRaces;
    }
}


public class DemoHorses {
    public static void main(String[] args) {
        Horse horse = new Horse();
        horse.setName("Kaze");
        horse.setBirthYear(2010);
        horse.setColor("Brown");

        RaceHorse raceHorse = new RaceHorse();
        raceHorse.setName("Kage");
        raceHorse.setBirthYear(2019);
        raceHorse.setColor("White");
        raceHorse.setNumOfRaces(10);

        System.out.println(horse.getName()+" "+horse.getBirthYear()+" year, "+horse.getColor());
        System.out.println(raceHorse.getName()+" "+raceHorse.getBirthYear()+" year, "
                +raceHorse.getColor()+", races: "+raceHorse.getNumOfRaces());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS