Java assigment ( please include inheritance for Animal Using Horse ) the lesson
ID: 3803573 • Letter: J
Question
Java assigment ( please include inheritance for Animal Using Horse ) the lesson is on inheritance and i am confused on that aspect as well as overloading methods and overriden methods?
You are to use the theme provided for a new Parent class. You will use the theme
Animal
For Animal, you will create a subclass of Animal called Horse
Once you pick your theme, you must use the “HAS A” question to determine the fields that you are going to include in your classes.
For example, an Alarm Clock HAS A:
Set Hours Button Time
Set Minutes Button Time
Set Hours Button Alarm
Set Minutes Button Alarm
Alarm On-Off Button
Snooze Button
You will create UMLs for all classes.
You will WRITE JAVA code to create the class from above. In the classes you are creating you will include at least three fields or attributes (See sample alarm clock above)
You will have overloaded methods, and overridden methods. You will label each. You will have a Testclass that creates at least 3 objects of each class.
Your parent and child classes need to have all necessary and required methods.
Explanation / Answer
/*
* The java class Animal that represents
* the basic properties of animal class
* */
//Animal.java
public class Animal
{
private String name;
private int age;
private String color;
//default constructor
public Animal()
{
//default values
}
//parameterized constructor
public Animal(String name, int age, String color)
{
this.name=name;
this.age=age;
this.color=color;
}
public String getName()
{
return name;
}
public int getage()
{
return age;
}
public String getColor()
{
return color;
}
public String toString()
{
return String.format("Name : %s,age = %d, color : %s", name,age,color);
}
//sound method that will override in child dclass
//Horse
public String sound(){return "";}
}
--------------------------------------------------------------------------------------------
//Horse.java
/**
* The java class Horse that extends the Animal
* class overrides the toString method.
* The class overloads constructor of class.
* */
public class Horse extends Animal
{
//private data member
private int speed;
//default constructor
public Horse()
{
//default values
}
public Horse(String name, int age, String color,int speed)
{
//calling base class constructor
super(name, age, color);
this.speed=speed;
}
//Override sound method
public String sound() {
return "neigh";
}
//Overriding toString method of Animal class
public String toString()
{
return super.toString()+",Speed :"+speed+",sounds "+sound();
}
}
--------------------------------------------------------------------------------------------
/*
* The java program Driver that tests the classes
* Animal and Horse classes .
* */
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//Create an instance of Animal class
Animal animal=new Animal("Cow", 5, "white");
//call toString on animal object
System.out.println(animal.toString());
//Create an instance of Horse class
Animal horse=new Horse("Horse", 10, "black", 35);
//call toString on horse object
System.out.println(horse.toString());
}
}
--------------------------------------------------------------------------------------------
Sample output:
Name : Cow,age = 5, color : white
Name : Horse,age = 10, color : black,Speed :35,sounds neigh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.