Question

object oriented programming java homework question about abstracting and interfaces

2. Definition of a hierarchy of fruits is given below. • Fruit contains an abstract method getVitamin() that returns String.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* Driver program that demonstrates the classes ,Fruit.java, TreeFruit.java, GroudFruit.java
* Apple.java
* Banana.java
* StrawBerry.java
* BlackBerry.java
* */
//Driver.java
import java.util.ArrayList;
public class Driver
{
   public static void main(String[] args)
   {
       ArrayList<Fruit> fruits=new ArrayList<Fruit>();
       fruits.add(new Apple());
       fruits.add(new Banana());
       fruits.add(new StrawBerry());
       fruits.add(new BlackBerry());
       //calling method ,prepareFruits
       prepareFruits(fruits);

   }
/*The method prepareFruits takes ArrayList of Fruit type
   * and then call the peel for the Apple and Banana
   * or pick for straw berry and blue berry */

   private static void prepareFruits(ArrayList<Fruit> fruits)
   {
       for (Fruit fruit : fruits)
       {
           //check if fruit is an instance of Apple then call
           //peel method
           if(fruit instanceof Apple)
               ((Apple) fruit).peel();
           //check if fruit is an instance of Apple then call
           //peel method
           else if(fruit instanceof Banana)
               ((Banana) fruit).peel();
           //check if fruit is an instance of StrawBerry then call
           //pick method
           if(fruit instanceof StrawBerry)
               ((StrawBerry) fruit).pick();
           //check if fruit is an instance of BlackBerry then call
           //pick method
           else if(fruit instanceof BlackBerry)
               ((BlackBerry) fruit).pick();
           System.out.println("Vitamins : "+fruit.getVitamin());      
       }
   }
}
------------------------------------------------------------------------------------------------------------------------------------------------------

//Fruit.java
public abstract class Fruit
{
   public String color;
   public abstract String getVitamin();
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//TreeFruit.java
public interface TreeFruit
{
   public void peel();
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//GroundFruit.java
public interface GroundFruit
{
   public void pick();
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//Apple.java
public class Apple extends Fruit implements TreeFruit
{
   //Override peel method of the TreeFruit interface
   public void peel() {
       System.out.println("Peeling an apple");      
   }
   //Override getVitamin method of the Fruit abstract class
   public String getVitamin() {
       return "A B12";
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//Banana.java
public class Banana extends Fruit implements TreeFruit
{
   //Override peel method of the TreeFruit interface
   public void peel() {
       System.out.println("Peeling a banana");      
   }
   //Override getVitamin method of the Fruit abstract class
   public String getVitamin() {
       return "C D";
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//StrawBerry.java
public class StrawBerry extends Fruit implements GroundFruit
{
   //Override getVitamin method of the Fruit abstract class
   public String getVitamin() {
       return "B5 E";
   }
   //Override
   public void pick() {
       System.out.println("Picking a strawberry");      
      
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------

//BlackBerry.java
public class BlackBerry extends Fruit implements GroundFruit
{
   //Override getVitamin method of the Fruit abstract class
   public String getVitamin() {
       return "C K";
   }
   //Override
   public void pick() {
       System.out.println("Picking a blackberry");      

   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Peeling an apple Vitamins : A B12 Peeling a banana Vitamins: CD Picking a strawberry Vitamins : B5 E Picking a blackberry Vit

Add a comment
Know the answer?
Add Answer to:
object oriented programming java homework question about abstracting and interfaces 2. Definition of a hierarchy of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT