-1

Need some homework help and having some weird output on my console.

Here's the homework question:

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.

Here's my code:

Horse.java

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 String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getBirthYear() {
        return birthYear;
    }
    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }


}

RaceHorse.java

public class RaceHorse extends Horse {

    private int races;

    public int getRaces() {
        return races;
    }

    public void setRaces(int races) {
        this.races = races;
    }


}

DemoHorse.java

import java.util.Scanner;
public class DemoHorses {

    public static void main(String[] args) {

        Horse aHorse = new Horse();
        RaceHorse aRaceHorse = new RaceHorse();

        String DemoName;
        String DemoColor;
        String RaceDemoName;
        String RaceDemoColor;
        int DemoYear;
        int RaceDemoYear;
        int DemoRace;

        Scanner input = new Scanner(System.in); // Horse
        System.out.print("Enter the name of the horse >> ");
        DemoName = input.nextLine();
        aHorse.setColor(DemoName);
        System.out.print("Enter the color of the horse >> ");
        DemoColor = input.nextLine();
        aHorse.setColor(DemoColor);
        System.out.print("Enter the birth year of the horse >> ");
        DemoYear = input.nextInt();
        aHorse.setBirthYear(DemoYear);

        System.out.print("Enter the name of the racehorse >> "); //Racehorse
        RaceDemoName = input.nextLine();
        aRaceHorse.setName(RaceDemoName);
        System.out.print("Enter the color of the racehorse >> ");
        RaceDemoColor = input.nextLine();
        aRaceHorse.setColor(RaceDemoColor);
        System.out.print("Enter the birth year of the racehorse >> ");
        RaceDemoYear = input.nextInt();
        aRaceHorse.setBirthYear(RaceDemoYear);
        System.out.print("Enter the number of races the racehorse has completed >> ");
        DemoRace = input.nextInt();
        aRaceHorse.setRaces(DemoRace);

        System.out.println(aHorse.getName() + " is " + aHorse.getColor() + " and was born in " + aHorse.getBirthYear());
        System.out.println(aRaceHorse.getName() + " is " + aRaceHorse.getColor() + " and was born in " + aRaceHorse.getRaces());

        input.close();

    }

}

Here's the output I get:

Enter the name of the horse >> Old Paint
Enter the color of the horse >> black
Enter the birth year of the horse >> 2005
Enter the name of the racehorse >> Enter the color of the racehorse >> black
Enter the birth year of the racehorse >> 2222
Enter the number of races the racehorse has completed >> 12
null is black and was born in 2005
 is black and was born in 12

I don't know why it shows up as Enter the name of the racehorse >> Enter the color of the racehorse >> on my console, and I don't think the data the user is inputting is saving correctly. Anyone know how I can fix this?

Avaka
  • 3
  • 1

1 Answers1

0

Add

input.nextLine();

after

RaceDemoYear = input.nextInt();
aRaceHorse.setBirthYear(RaceDemoYear);
Ankit Sharma
  • 1,035
  • 11
  • 15