JAVA Create an application that allows the user to put diffe

JAVA: 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

Solution

public class Fruit {

   String color;
   double weight;
   String price;

   public Fruit() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   */
   public Fruit(String color, double weight, String price) {

       this.color = color;
       this.weight = weight;
       this.price = price;
   }

   /**
   * @return the color
   */
   public String getColor() {
       return color;
   }

   /**
   * @param color
   * the color to set
   */
   public void setColor(String color) {
       this.color = color;
   }

   /**
   * @return the weight
   */
   public double getWeight() {
       return weight;
   }

   /**
   * @param weight
   * the weight to set
   */
   public void setWeight(double weight) {
       this.weight = weight;
   }

   /**
   * @return the price
   */
   public String getPrice() {
       return price;
   }

   /**
   * @param price
   * the price to set
   */
   public void setPrice(String price) {
       this.price = price;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Fruit [color=\" + color + \", weight=\" + weight + \", price=\"
               + price + \"]\";
   }

}


public class Apple extends Fruit {

   String subType;

   public Apple() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   * @param subType
   */
   public Apple(String color, double weight, String price, String subType) {
       super(color, weight, price);
       this.subType = subType;
   }

   /**
   * @return the subType
   */
   public String getSubType() {
       return subType;
   }

   /**
   * @param subType
   * the subType to set
   */
   public void setSubType(String subType) {
       this.subType = subType;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Apple [subType=\" + subType + \", toString()=\" + super.toString()
               + \"]\";
   }

}


public class Banana extends Fruit {

   double length;

   public Banana() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   * @param length
   */
   public Banana(String color, double weight, String price, double length) {
       super(color, weight, price);
       this.length = length;
   }

   /**
   * @return the length
   */
   public double getLength() {
       return length;
   }

   /**
   * @param length
   * the length to set
   */
   public void setLength(double length) {
       this.length = length;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Banana [length=\" + length + \", toString()=\" + super.toString()
               + \"]\";
   }

}


public class Grapes extends Fruit {

   int numberOfGrapes;

   public Grapes() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   * @param numberOfGrapes
   */
   public Grapes(String color, double weight, String price, int numberOfGrapes) {
       super(color, weight, price);
       this.numberOfGrapes = numberOfGrapes;
   }

   /**
   * @return the numberOfGrapes
   */
   public int getNumberOfGrapes() {
       return numberOfGrapes;
   }

   /**
   * @param numberOfGrapes
   * the numberOfGrapes to set
   */
   public void setNumberOfGrapes(int numberOfGrapes) {
       this.numberOfGrapes = numberOfGrapes;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Grapes [numberOfGrapes=\" + numberOfGrapes + \", toString()=\"
               + super.toString() + \"]\";
   }

}


public class Orange extends Fruit {

   double subType;

   public Orange() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   * @param subType
   */
   public Orange(String color, double weight, String price, double subType) {
       super(color, weight, price);
       this.subType = subType;
   }

   /**
   * @return the subType
   */
   public double getSubType() {
       return subType;
   }

   /**
   * @param subType
   * the subType to set
   */
   public void setSubType(double subType) {
       this.subType = subType;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Orange [subType=\" + subType + \", toString()=\"
               + super.toString() + \"]\";
   }

}


public class Pear extends Fruit {

   int length;

   public Pear() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param color
   * @param weight
   * @param price
   * @param length
   */
   public Pear(String color, double weight, String price, int length) {
       super(color, weight, price);
       this.length = length;
   }

   /**
   * @return the length
   */
   public int getLength() {
       return length;
   }

   /**
   * @param length
   * the length to set
   */
   public void setLength(int length) {
       this.length = length;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Pear [length=\" + length + \", toString()=\" + super.toString()
               + \"]\";
   }

}


public class Basket {

   Fruit[] fruits;

   int numberOfItems;
   double capacity;

   public Basket(double c) {
       // TODO Auto-generated constructor stub
       fruits = new Fruit[10];
       numberOfItems = 0;
       capacity = c;
   }

   public boolean addFruit(Fruit fruit) {

       if ((getTotalWeight() + fruit.getWeight()) <= capacity) {

           fruits[numberOfItems++] = fruit;
           return true;
       } else
           return false;

   }

   public double getTotalWeight() {
       double totalWeight = 0;
       for (int i = 0; i < fruits.length; i++) {
           totalWeight += fruits[i].getWeight();

       }
       return totalWeight;
   }

}

JAVA: 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, creat
JAVA: 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, creat
JAVA: 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, creat
JAVA: 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, creat
JAVA: 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, creat
JAVA: 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, creat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site