Answer:
The code to this question can be given as:
Code:
import javax.swing.JOptionPane; //import package.
public class Horse //define class horse.
{
//define variable as private.
private String name;
private String color;
private String birthYear;
public void setName() //define function (setName)
{
name = JOptionPane.showInputDialog(null,"Please enter name of the horse:");
}
public void setColor() //define function (setColor)
{
color = JOptionPane.showInputDialog(null,"Please enter color of the horse:");
}
public void setYear() //define function (setColor)
{
birthYear = JOptionPane.showInputDialog(null,"Please enter birth year of the horse:");
}
public String getName() //define function (getName)
{
return name;
}
public String getColor() //define function (getColor
{
return color;
}
public String getYear() //define function (getYear)
{
return birthYear;
}
}
public class RaceHorse extends Horse //define class RaceHorse that inherit Horse
{
private String raceNum; //define variable as private
public void setRace() //define function (setRace)
{
raceNum = JOptionPane.showInputDialog(null,"Please enter number of the horse's races:");
}
public String getRace() //define function (getRace)
{
return raceNum;
}
}
public class DemoHorses //define class DemoHorses
{
public static void main(String[] args) //main method.
{
Horse ob1 = new Horse(); //creating object of Horse
RaceHorse ob2 = new RaceHorse(); //creating object of RaceHorse
ob1.setName(); //calling functions
ob1.setColor();
ob1.setYear();
ob2.setRace();
//show messsage.
JOptionPane.showMessageDialog(null,"The name of the horse is " + ob1.getName() +
". The horse's color is " + ob1.getColor()+ ". It was born in " + ob1.getYear() +
". " + ob1.getName() + " has taken part in " + ob2.getRace() + " races");
}
}
Step-by-step explanation:
In this code firstly we import the package that is used to create a message dialog with the given title and messageType. It is used to create a dialog with the options Yes, No and Cancel. This is used to show a question-message dialog and request for input from the user. Then we define 3 classes that are Horse.java, RaceHorse .java, and DemoHorses.java which name is already given in the question. In the first class i.e Horse. we declare variable as private and we use the get and set function. The set function sets the values and gets the function to get the values. Then we define second class i.e RaceHorse. In this class, we first inherit the Horse class. Then we define a variable as private and use the set and get function. Then we define another class i.e DemoHorse. In this class, we define the main method and call the above functions by creating their objects.