Question: Hi, i need help in java program. Implement the 5 classes with a name data field and toString(): String method. The return value of the
Hi, i need help in java program.
Implement the 5 classes with a name data field and toString(): String method. The return value of the toString() method must contain the class name, and the value of the name data field.
Add method getApplePieRecipe(): String to the Apple class.
Add method getOrangeJuiceRecipe(): String to the Orange class.
Write a FruitClient class where you design a few statements to demonstrate 1) polymorphisms, 2) dynamic binding, and 3) down-casting and up-casting. For each, write a comment explain how the statements you wrote demonstrate it. Be sure to use instanceof when doing down-casting
i will post program i have so far
Fruit class:
public class Fruit {
//data field
String name;
//toString() method.
public String toString() {
return "Fruit [name=" + name + "]";
}
}
Applce class:
public class Apple extends Fruit{
//data field
String name;
//toString() method.
@Override
public String toString() {
return "Apple [name=" + super.name + "]";
}
//getApplePieRecipe(): String
String getApplePieRecipe()
{
return super.name;
}
}
Orange class:
public class Orange extends Fruit{
//data field
String name;
//toString() method.
@Override
public String toString() {
return "Orange [name=" + super.name + "]";
}
//getOrangeJuiceRecipe() method
String getOrangeJuiceRecipe()
{
return super.name;
}
}
GoldenDelicious class:
public class GoldenDelicious extends Apple{
//data field
String name;
//toString() method.
@Override
public String toString() {
return "GoldenDelicious [name=" + super.name + "]";
}
}
McIntosh class:
public class McIntosh extends Apple{
//data field
String name;
//toString() method.
@Override
public String toString() {
return "McIntosh [name=" + super.name + "]";
}
}
FruitClient classs:
//FruitClient class
public class FruitClient {
//main method-start of program
public static void main(String[]arg)
{
//When type of the object is determined at run-time, it is known as dynamic binding.
//Creating object of Fruit class
Fruit fruit = new Fruit();
//assigning name to fruit class
fruit.name="Cherry";
//printing fruit class information using toString() method of Fruit class
System.out.println(fruit);
//1) polymorphisms Starts
//Creating object of Apple class and assigning it to Fruit class reference
Fruit apple = new Apple(); //4)Upcasting
//assigning name to Apple class
apple.name="Green Apple";
/*printing Apple class information using overriden method
of toString() method of Fruit class*/
System.out.println(apple);
System.out.println("Apple Pie Recipe:"+((Apple) apple).getApplePieRecipe());
//Creating object of Orange class and assigning it to Fruit class reference
Fruit orange= new Orange();//4)Upcasting
//assigning name to Apple class
orange.name="Orange";
/*printing Orange class information using overriden method
of toString() method of Fruit class*/
System.out.println(orange);
System.out.println("Orange Juice Recipe:"+((Orange) orange).getOrangeJuiceRecipe());
//Creating object of GoldenDelicious class and assigning it to Apple class reference
Apple gd = new GoldenDelicious();//4)Upcasting
//assigning name to GoldenDelicious class
gd.name="Golden Delicious Fruit";
//printinf information of GoldenDelicious class
System.out.println(gd);
//Creating object of McIntosh class and assigning it to Apple class reference
Apple mi = new McIntosh(); //4)Upcasting
//assigning name to McIntosh class
mi.name="McIntosh Fruit";
////printinf information of McIntosh class
System.out.println(mi);
//down casting
if(gd instanceof Apple)
{
//down casting
GoldenDelicious newGD = (GoldenDelicious)gd;
//and setting new name to GoldenDelicious object
newGD.name="New Golden Delicious Fruit";
System.out.println(" "+newGD.name);
}
if(mi instanceof Apple)
{
//down casting
McIntosh newMI = (McIntosh)mi;
//and setting new name to McIntosh object
newMI.name="New McIntosh Fruit";
System.out.println(" "+newMI.name);
}
}
}
all this is done. but need help in bellow questions
Add an equal method and a hashCode method to each class, and make sure the contract of the hashCode method is maintained.
Revise the GoldenDelicious and McInosh classes so that they cannot be subclassed.
In your client class, create an AraryList of fruits of various kind.
Print the list of fruits
Thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
