Question

In: Computer Science

Please include all classes including driver. IN JAVA Suppose that there is a big farm in...

Please include all classes including driver.

IN JAVA

Suppose that there is a big farm in California that supplies the majority of the Grocery stores in the twin cities with vegetables and fruits. The stores submit their orders and receive the shipment a week later. Based on the size of the orders, the farm management decides on the size of the truck to load.

Create a Produce class that have an instance variable of type String for the name, appropriate constructors, appropriate accessor and mutator methods, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String, the price (this is the price per box) as a double, the quantity as an integer, and invoke the appropriate constructor from the base class to set the name. Also, they should override toString method to display the name of the produce, the price, and its type. For instance, Mango is a Fruit and Cauliflower is a Vegetable.

Finally, create a class called TruckOfProduce that will keep track of the boxes of Vegetables and Fruits added in to the truck. This class should use an array of Produce to store both vegetables and fruits. Also, it should have the following:

• Constructor that accepts an integer to initialize the array of Produce

• addProduce method that adds either fruit or vegetable to the array

• search method that accepts a name string, which can be either the name of a fruit or vegetable, and returns true if the name exists. Otherwise, it returns false.

• remove method that accepts a produce object and returns true if the produce is found and removed successfully. Otherwise, it returns false.

• computeTotal method that will return the total cost of all the produce in the truck.

• toString method that returns all the produce from in the truck.

• ensureCapacity method this is a void method. When this method is called it will double the size of the array and copy all the content of the smaller array in to this bigger array.

For instance, one could initialize the array to hold 1000 boxes of vegetables and fruits. What will happen if you try to add one more box (1001)? Of course, you will get an ArrayIndexOutOfBound error. So, thing about ensureCapacity as the method that will solve ArrayIndexOutBound error.

Then wrap it up with the driver class that tests all methods and displays the name of the produce, the price, and its type.

Solutions

Expert Solution

Below is the java classes and the main driver class :

class Produce
{
   //instance variable of the product name
   protected String name;

   //public constructors
   public Produce(String name)
   {
       this.name = name;
   }
   public Produce()
   {
       this.name = "";
   }

   //Accesors and mutators for the name

   public void setName(String name)
   {
       this.name = name;
   }

   public String getName()
   {
       return this.name;
   }

   public String toString()
   {
       return this.name;
   }
}

//The Fruit class
class Fruit extends Produce
{
   protected double price;
   protected int quantity;

   public Fruit(String name, double price, int quantity)
   {
       super.setName(name);
       this.price = price;
       this.quantity = quantity;
   }

   public String toString()
   {
       //This stores the string representation of the product
       String str = "";

       str += "Name : " + this.name + "\n";
       str += "Price : " + this.price + "\n";
       str += "Type : " + "Fruit";

       return str;
   }

   //Accesors and mutators
   public void setPrice(double price)
   {
       this.price = price;
   }

   public void setQuantity(int quantity)
   {
       this.quantity = quantity;
   }

   public double getPrice()
   {
       return this.price;
   }

   public int getQuantity()
   {
       return this.quantity;
   }
}

//The Vegetable class
class Vegetable extends Produce
{
   protected double price;
   protected int quantity;

   public Vegetable(String name, double price, int quantity)
   {
       super.setName(name);
       this.price = price;
       this.quantity = quantity;
   }

   public String toString()
   {
       //This stores the string representation of the product
       String str = "";

       str += "Name : " + this.name + "\n";
       str += "Price : " + this.price + "\n";
       str += "Type : " + "Vegetable";

       return str;
   }

   //Accesors and mutators
   public void setPrice(double price)
   {
       this.price = price;
   }

   public void setQuantity(int quantity)
   {
       this.quantity = quantity;
   }

   public double getPrice()
   {
       return this.price;
   }

   public int getQuantity()
   {
       return this.quantity;
   }

}

class TruckOfProduce
{
   //Array of products
   protected Produce products[];

   //Stores the number of products currently present in the array
   protected int num;

   //the current capacity of the array
   protected int capacity;

   public TruckOfProduce(int size)
   {
       this.capacity = size;
       this.num = 0;

       //initializing the produce array
       this.products = new Produce[this.capacity];
   }

   //Addes a fruit or vegetable to the array
   public void addProduce(Produce produce)
   {
       // Checking if the array has exeeded its limit
       if (this.num >= this.capacity)
           this.ensureCapacity();

       //adding the produce
       this.products[this.num++] = produce;
   }

   //Search for a string or vegetable
   public boolean search(String str)
   {
       int i;

       for (i = 0; i < this.num; i++)
       {
           //Cheking if the string matchs using the String.equals() method
           if (this.products[i].getName().equals(str))
               return true;
       }

       //control will come here if the str does not match so returning false
       return false;
   }

   public String toString()
   {
       String str = "";

       for (int i = 0; i < this.num; i++)
       {
           str += "Produce " + (i + 1) + " : \n";
           str += this.products[i].toString() + "\n\n";
       }

       return str;
   }

   protected void ensureCapacity()
   {
       //increase the capacity by 2
       this.capacity *= 2;

       Produce newArray[] = new Produce[this.capacity];

       //moving the elements form the old array to the new one
       for (int i = 0; i < this.num; i++)
       {
           newArray[i] = this.products[i];
       }

       this.products = newArray;
   }

   public boolean remove(Produce p)
   {
       int i, j;

       for (i = 0; i < this.num; i++)
       {
           if (this.products[i] == p)
               break;
       }

       //if the object was not found, i must have gone the this.num
       if (i == this.num)
           return false;
       else
       {
           for (j = i; j < this.num - 1; j++)
               this.products[j] = this.products[j + 1];

           this.products[this.num - 1] = null;
           this.num--;
       }

       return true;
   }

   public double computeTotal()
   {
       double total = 0.0;

       for (int i = 0; i < this.num; i++)
       {
           double price = 0;
           int q = 0;
          
           //Checking the class type to down cast
           if (this.products[i].getClass() == Fruit.class)
           {
               price = ((Fruit)this.products[i]).getPrice();
               q = ((Fruit)this.products[i]).getQuantity();
           }
           else
           {
               price = ((Vegetable)this.products[i]).getPrice();
               q = ((Vegetable)this.products[i]).getQuantity();
           }
          
           total += price * q;
       }

       return total;
   }
}

//The main driver class
class Main
{
   public static void main(String args[])
   {
       Produce f1 = new Fruit("Mango", 20, 3);
       Produce f2 = new Fruit("Bananna", 10, 6);
       Produce v1 = new Fruit("Tomato", 15, 2);
       Produce v2 = new Fruit("Carrot", 10, 5);

       TruckOfProduce truck = new TruckOfProduce(2);

       truck.addProduce(f1);
       truck.addProduce(f2);
       truck.addProduce(v1);
       truck.addProduce(v2);

       System.out.println("Contents of the truck are : \n");
       System.out.println(truck);

       if (truck.remove(v1))
       {
           System.out.println("Contents of the truck after removing " + v1.getName() + " : \n");
           System.out.println(truck);
       }

       System.out.println("Total cost of all the produce in the truck : " + truck.computeTotal() + "\n");

       if (truck.search("Mango"))
           System.out.println("Mango found in the truck");
       else
           System.out.println("Mango not found in the truck");
       if (truck.search("Strawberry"))
           System.out.println("\nStrawberry found in the truck");
       else
           System.out.println("\nStrawberry not found in the truck");
          
   }
}

OUTPUT :


C:\Users\HeathCliff\Desktop>javac my.java

C:\Users\HeathCliff\Desktop>java Main
Contents of the truck are :

Produce 1 :
Name : Mango
Price : 20.0
Type : Fruit

Produce 2 :
Name : Bananna
Price : 10.0
Type : Fruit

Produce 3 :
Name : Tomato
Price : 15.0
Type : Fruit

Produce 4 :
Name : Carrot
Price : 10.0
Type : Fruit


Contents of the truck after removing Tomato :

Produce 1 :
Name : Mango
Price : 20.0
Type : Fruit

Produce 2 :
Name : Bananna
Price : 10.0
Type : Fruit

Produce 3 :
Name : Carrot
Price : 10.0
Type : Fruit


Total cost of all the produce in the truck : 170.0

Mango found in the truck

Strawberry not found in the truck


Related Solutions

Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
IN JAVA Question 1 The following classes along with the driver has been created and compiled....
IN JAVA Question 1 The following classes along with the driver has been created and compiled. public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Please explain what the Big O Notation is in plain English. Please include the logic behind...
Please explain what the Big O Notation is in plain English. Please include the logic behind it and calculations if necessary. I have trouble understanding it, I will be taking a test soon so I will need an easier way to remember it. I use Python btw.
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited) Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND TEST your CeilingFan classes (see pp. 222 and 223), AND Finally, implement and Test the MacroCommand class on pp. 224-227 EXAMPLE output for homework assignment…………… ======= The COMMAND PATTERN ============= guest house garage door is UP guest house garage...
Java Programming Please describe the roles of the following classes in processing a database: Connection ResultSet
Java Programming Please describe the roles of the following classes in processing a database: Connection ResultSet
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT