Part 3. Create a test class "TestPolymorphism.java" in the same package. Inside its main() method do the following steps. Some steps ask to provide explanantions for questions. Provide the answers in a separate PDF file. a) Create two Fruit objects "fruit1" and "fruit2" using the two constructors respectively. b) Display the total number of fruits using the getNumberOfFruits() method. c) Create a new Apple object "apple1" using the no-arg constructor and assign it to the object reference variable of type Fruit. (i.e., Apple as a subtype of Fruit)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please complete and check my code in Java (eclipse). Complete the instructions (please see images). Please add comments. 

 

package HW5;
enum Color {
    YELLOW, ORANGE, RED, PURPLE;
}

  public class Fruit{
  private Color color;
  private int calories;
  private double pricePerPound;
  protected static int numberOfFruits;
//Constructor a default fruit object 
  Fruit(){
      numberOfFruits++;
  }
 Fruit(Color color, int calories, double  pricePerPound){
     this.color=color;
     this.calories=calories;
     this.pricePerPound= pricePerPound;
     numberOfFruits++;
 }
 
/////generate getters & setters
public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}
public int getCalories() {
    return calories;
}
public void setCalories(int calories) {
    this.calories = calories;
}
public double getPricePerPound() {
    return pricePerPound;
}
public void setPricePerPound(double pricePerPound) {
    this.pricePerPound = pricePerPound;
}
public static int getNumberOfFruits() {
    return numberOfFruits;
}
public static void setNumberOfFruits(int numberOfFruits) {
    Fruit.numberOfFruits = numberOfFruits;
}
@Override
public String toString() {
    return "\ncolor: "+ color + "Calories: "+ calories + "pricePerPound: "+ pricePerPound;
}

package HW5;
//Child class #1
public class Apple extends Fruit {
    private String variety;
    private String texture;
//default constructor 
    Apple(){
        super();
    }    
  Apple(String variety, String texture, Color color, int calories, double pricePerPound){
      super (color,calories, pricePerPound);
      this.variety=variety;
      this.texture=texture;
}
/////generate getters & setters
public String getVariety() {
    return variety;
}

public void setVariety(String variety) {
    this.variety = variety;
}

public String getTexture() {
    return texture;
}

public void setTexture(String texture) {
    this.texture = texture;
}
@Override
public String toString() {
    return "\ncolor: "+ getColor() + "Calories: "+ getCalories() + "pricePerPound: "+ getPricePerPound()+ 
            "\nvariety: "+ getVariety() +"\ntexture: "+ getTexture();
            
}
}

package HW5;

//Child class #2
public class Grapes extends Fruit {
    private String taste;
    private boolean hasSeed;
//default constructor
  Grapes(){  
      super();
  }
  Grapes( String taste, boolean hasSeed, Color color, int calories, double pricePerPound){  
  super (color,calories, pricePerPound);
  this.taste=taste;
  this.hasSeed=hasSeed;
  }
 ////////generate getters & setters
public String getTaste() {
    return taste;
}
public void setTaste(String taste) {
    this.taste = taste;
}
public boolean isHasSeed() {
    return hasSeed;
}
public void setHasSeed(boolean hasSeed) {
    this.hasSeed = hasSeed;
}
@Override
public String toString() {
    return "\ntaste: "+ getTaste() +"\ncolor: "+ getColor() + 
            "Calories: "+ getCalories() + "pricePerPound: "+ 
             getPricePerPound();
}
}

 

 

Part 3. Create a test class “TestPolymorphism.java” in the same package. Inside its main() method do the following steps. Some steps ask to provide explanations for questions. Provide the answers in a separate PDF file.

a) Create two Fruit objects “fruit1” and “fruit2” using the two constructors respectively.

b) Display the total number of fruits using the getNumberOfFruits() method.

c) Create a new Apple object “apple1” using the no-arg constructor and assign it to the object reference variable of type Fruit. (i.e., Apple is a subtype of Fruit)

d) Create a new Apple object “apple2” using the overloaded constructor that has arguments and assign it to the object reference variable of type Fruit. (i.e., Apple is a subtype of Fruit)

e) Repeat step (c) and step (d) for “Grape” class. Create two Grape objects “grape1” and “grape2” and assign them to the object reference variable of type Fruit. (i.e., Grape as a subtype of Fruit).

f) Display the total number of fruits using the getNumberOfFruits() method.

g) Display the actual class of “fruit1”, “fruit2”, “apple1” and “apple2” objects. Refer to the following example.
   
   ```java
   System.out.println("The actual class is " + fruit1.getClass());
   ```

h) Display the result to test whether “fruit1” and “apple1” objects are equal. Refer to the following example. Provide an answer explaining the output.

   ```java
   System.out.println("Is objects fruit1 and apple1 equal? " + (fruit1).equals(apple1));
   ```

i) Display the result to test whether “fruit2” and “grape2” objects are equal. (example in step g))

j) Answer the question: Can you display the texture of the apple object “apple2” by calling getTexture() method as below? Why or Why not?

   ```java
   System.out.println(apple2.getTexture());
   ```

k) Answer the question: Can you display the taste of the grape object “grape2” by calling getTaste() method?

   ```java
   System.out.println(grape2.getTaste());
   ```

m) Change the color of “apple2” to
Transcribed Image Text:Part 3. Create a test class “TestPolymorphism.java” in the same package. Inside its main() method do the following steps. Some steps ask to provide explanations for questions. Provide the answers in a separate PDF file. a) Create two Fruit objects “fruit1” and “fruit2” using the two constructors respectively. b) Display the total number of fruits using the getNumberOfFruits() method. c) Create a new Apple object “apple1” using the no-arg constructor and assign it to the object reference variable of type Fruit. (i.e., Apple is a subtype of Fruit) d) Create a new Apple object “apple2” using the overloaded constructor that has arguments and assign it to the object reference variable of type Fruit. (i.e., Apple is a subtype of Fruit) e) Repeat step (c) and step (d) for “Grape” class. Create two Grape objects “grape1” and “grape2” and assign them to the object reference variable of type Fruit. (i.e., Grape as a subtype of Fruit). f) Display the total number of fruits using the getNumberOfFruits() method. g) Display the actual class of “fruit1”, “fruit2”, “apple1” and “apple2” objects. Refer to the following example. ```java System.out.println("The actual class is " + fruit1.getClass()); ``` h) Display the result to test whether “fruit1” and “apple1” objects are equal. Refer to the following example. Provide an answer explaining the output. ```java System.out.println("Is objects fruit1 and apple1 equal? " + (fruit1).equals(apple1)); ``` i) Display the result to test whether “fruit2” and “grape2” objects are equal. (example in step g)) j) Answer the question: Can you display the texture of the apple object “apple2” by calling getTexture() method as below? Why or Why not? ```java System.out.println(apple2.getTexture()); ``` k) Answer the question: Can you display the taste of the grape object “grape2” by calling getTaste() method? ```java System.out.println(grape2.getTaste()); ``` m) Change the color of “apple2” to
p) Create a new method `displayObject()` that takes one argument. The argument type is an object for the "Object" class. Method header:
```java
public static void displayObject(Object obj)
```

q) This method will display the contents of "obj" only if this object belongs to either the Fruit or Apple or Grape class. You have to use the `toString()` method and `instanceof` operator in this method implementation. Do not use any casting technique inside this method. (Hint: Use if - else if conditions.) NOTE: The output displayed from your objects should be relevant and appropriate based on its class methods.

r) Call the method `displayObject(<param>)` by passing each object (created in all the previous steps) as a parameter in separate function calls.
Transcribed Image Text:p) Create a new method `displayObject()` that takes one argument. The argument type is an object for the "Object" class. Method header: ```java public static void displayObject(Object obj) ``` q) This method will display the contents of "obj" only if this object belongs to either the Fruit or Apple or Grape class. You have to use the `toString()` method and `instanceof` operator in this method implementation. Do not use any casting technique inside this method. (Hint: Use if - else if conditions.) NOTE: The output displayed from your objects should be relevant and appropriate based on its class methods. r) Call the method `displayObject(<param>)` by passing each object (created in all the previous steps) as a parameter in separate function calls.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education