Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing a java program that uses inheritance. Create an application th

ID: 3774455 • Letter: N

Question

Need help writing a java program that uses inheritance.

Create an application that allows the user to put different types of fruit into a basket. First create a base class called Fruit. Using inheritance, create at least 5 types of specific fruits (e.g. Apple, Banana, Pear, Orange, Grape) and define those classes as subclasses from Fruit. Each Fruit object has the following fields: color, weight, price. In addition, each specific fruit can have additional fields. Examples are, an Apple can have a field AppleSubtype, which can hold the apple's subtype (e.g. Granny Smith). A banana can have a field called Length. Grapes can have a field for number of grapes on one vine. Be creative and add at least 1 field to each individual, specific fruit class. The user has a basket (an array) that can hold 10 times. The user is asked how many of each items he wants to add to the basket, until either the basket is full, or each fruit has been offered to the user. Once the basket is full or the user chooses not to add 10 items, let the user know what the total weight of the basket is, and what the total price of the fruits is. You can decide yourself on the weights and the prices, but use somewhat appropriate numbers.

Explanation / Answer

package snippet;

import java.util.Scanner;

class Apple extends Fruit
{
   String subType;
   public Apple() {
       weight=10;
       price=10;
       color="red";
       subType="Granny smith Apple";
      
   }
  
   int getPrice()
   {
       return price;
   }
  
}


class Banana extends Fruit
{
   int length;
   public Banana() {
       weight=20;
       price=20;
       color="Yellow";
       length=20;
      
   }
}

class Grapes extends Fruit
{
   int numberofgrapes;
   public Grapes() {
       weight=30;
       price=30;
       color="green";
       numberofgrapes=30;
      
   }
}


class Pear extends Fruit
{
   String region;
   public Pear() {
       weight=40;
       price=40;
       color="green";
       region="USA";
      
   }
}

class Oranges extends Fruit
{
   String taste;
  
   public Oranges() {
       weight=50;
       price=50;
       color="orange";
       taste="sweet";
      
   }

}

public class Fruit {
  
  
   String color;
   int weight,price;
  
  
  

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Fruit o[]=new Fruit[10];
       int j=0;
       int n;
       int total_w=0,total_cos=0;
       Scanner sc=new Scanner(System.in);
      
       System.out.println("Enter no of Apples");
      
       n=sc.nextInt();
       Apple a[]=new Apple[n];
      
       for(int i=0;i<n;i++)
       {
           a[i]=new Apple();
           o[j++]=a[i];
       }
       total_cos+=a[0].price*n;
       total_w+=a[0].weight*n;
      
       System.out.println("Enter no of Banana");
       n=sc.nextInt();
       Banana b[]=new Banana[n];
      
       for(int i=0;i<n;i++)
       {
           b[i]=new Banana();
           o[j++]=b[i];
       }
       total_cos+=b[0].price*n;
       total_w+=b[0].weight*n;
      
      
       System.out.println("Enter no of Pear");
      
       n=sc.nextInt();
       Pear p[]=new Pear[n];
       for(int i=0;i<n;i++)
       {
           p[i]=new Pear();
           o[j++]=p[i];
       }
       total_cos+=p[0].price*n;
       total_w+=p[0].weight*n;
      
       System.out.println("Enter no of oranges");
      
       n=sc.nextInt();
       Oranges or[]=new Oranges[n];
       for(int i=0;i<n;i++)
       {
           or[i]=new Oranges();
           o[j++]=or[i];
       }
       total_cos+=or[0].price*n;
       total_w+=or[0].weight*n;
      
      
       System.out.println("Enter no of Grapes");
      
       n=sc.nextInt();
       Grapes gr[]=new Grapes[n];
      
       for(int i=0;i<n;i++)
       {
           gr[i]=new Grapes();
           o[j++]=gr[i];
       }
      
       total_cos+=gr[0].price*n;
       total_w+=gr[0].weight*n;
System.out.println("Total weight "+total_w);
System.out.println("Total cost "+total_cos);

      
   }

}

===================================================================

Enter no of Apples
2
Enter no of Banana
2
Enter no of Pear
2
Enter no of oranges
2
Enter no of Grapes
2
Total weight 300
Total cost 300

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote