Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implementpublic abstract class Food{}: The class has three attributes (private data fields): String name double price static int numFood = 0 //a var to hold

 
  1. Implement public abstract class Food{}:
    1. The class has three attributes (private data fields): 
      1. String name 
      2. double price
      3. static int numFood = 0 //a var to hold the count of all Foods created
    2. Define at least one constructor as defined in the UML above 
    3. Implement Getters & Setters for private instance data fields
    4. Implement a getter method for the static counter variable 
    5. Declare an abstract method getType() that returns a String
    6. Override toString() from java.lang.Object to return name
    7. Override equals(Object obj) from java.lang.Object to compare the name of two Foods, and return true if and only if the names are the same.


 

[COMPREHENSION 1]: Include a block comment explaining why Food is declared as abstract, as well as why the method getType() is declared as abstract.


 

  1. Implement a concrete subclass, Vegetable{}, that extends Food
    1. The class has two additional attributes (private data fields): 
      1. String color
      2. static int numVeg = 0;
    2. Overload the constructor at least two times as described in the UML
    3. Implement Getters & Setters for private data fields
    4. Implement a getter method for the static counter variable 
    5. Override (provide a definition for) getType() to return the name of the class
    6. Override toString() from Food to include the type of food, color and price 


 

[COMPREHENSION 2]: Include a block comment explaining what constructor chaining is, how it is used in your program (implicitly or explicitly), and specifically describe the chaining that would happen when you invoke each/any of the overloaded Vegetable constructors.


 

  1. Implement a concrete subclass, Fruit{}, that extends Food
    1. The class has two additional attributes (private data fields): 
      1. boolean seeds
      2. static int numMeat = 0;
    2. Overload the constructor at least two times as described in the UML
    3. Implement Getters & Setters for private data fields
    4. Implement a getter method for the static counter variable 
    5. Override (provide a definition for) getType() to return the name of the class
    6. Override toString() from Food to include the type of food, whether or not seedless, and price


 

[COMPREHENSION 3]: Include a block comment explaining the difference between overriding and overloading methods, how/where each are used within the class, as well as explaining how/when the overridden methods in the Fruit class will be invoked.


 

  1. Implement a Test program named Test with a main method and four additional methods, defined below. Note that starting code is provided for you, including stubs for the methods you will need to complete. It is your responsibility to make sure that the program/main method functions as intended with the sample output below. 
    1. main method: 
      1. The main method creates a single-dimensional array to represent a shopping cart at the grocery store. 

Food[] groceryCart = new Food[50]

groceryCart[0] = new Vegetable("Romaine", 1.09, "Green");

groceryCart[1] = new Fruit("Mango", 3.79, true);

groceryCart[2] = new Vegetable("Brussel Sprouts", 4.56, "Green");

groceryCart[3] = new Fruit("Blueberry", 0.89, false); 

groceryCart[4] = new Vegetable("Purple Carrots", 1.56, "Purple"); 

groceryCart[5] = new Vegetable("Spinach", 2.32, "Green");

groceryCart[6] = new Vegetable("Carrots", 1.45, "Orange");

groceryCart[7] = new Vegetable("Potatoes", 3.99, "Red");

groceryCart[8] = new Vegetable("Broccoli", 5.21, "Green");

groceryCart[9] = new Vegetable("Turnips", .99, "White");

groceryCart[10] = new Vegetable("Onions", 1.99, "Yellow");

groceryCart[11] = new Fruit("Apples", 5.79, true);

groceryCart[12] = new Fruit("Banana", .78, false);

groceryCart[13] = new Fruit("Kiwi", 2.65, true);

groceryCart[14] = new Fruit("Strawberry", 4.79, true);

groceryCart[15] = new Fruit("Watermelon", 6.32, false);

  1. Additionally, there are menu options printed out in the main method as well as method stubs for the methods described below. 
  2. public static void displayCart(Food[]list){} 
    1. This method should accept an array of Food as input and display the filled contents of the cart. 

[COMPREHENSION 4]: Include a block comment explaining which version of toString() is being invoked on each object in the array (or how the JVM would know which to invoke).

  1. public static void searchCart(Food[]list, String foodName){} 
    1. This method should accept an array of Food as input as well as the name of a Food object to search for within the Cart. 
    2. For full credit, this method must invoke the equals(Object o) method that you defined in Food.
  2. public static void sortByPrice(Food[]cart){} 
    1. accept an array of Food and sort by price per item in descending order using the sorting algorithm of your choice.
  3. public static void checkOut(Food[]cart){} 
    1. This method should accept an array of Food and calculate/display the total cost for everything in the cart as well as the average price per item.
    2. Be sure to display both values to 2 decimal places.

demonstrate the following abilities: 1. Given an abstract super class, derive concrete classes. 2. Override and Overload methods among object classes. 3. Use polymorphism to manipulate (display, search, sort) objects of the concrete classes as objects of the abstract class type Vegetable -color: String -numVeg: static int -name: String -price: double. -numFood: static int +Vegetable (String color) +Vegetable (String name, double price, String color) +getNumVeg(): int +getType(): String +toString(): String Food *Food (String, double). +getName(): String. +getPrice (): double + setName (String name): void +setPrice (double price): void +getNumFoods (): int +getType(): String +toString(): String +equals (Object o): boolean +getColor(): String setColor (String color): void w Fruit seeds: boolean -numFruit: static int +Fruit (boolean seeds) +Fruit (String name, double price, boolean seeds) +getSeeds (): boolean +setSeeds (boolean seeds): void +getNumFruit(): int +getType(): String +toString(): String

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Source Code Foodjava public abstract class Food attributes private String name private double price private static int numFood 0 Constructor param name param price protected FoodString name double pri... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Programming

Authors: Joyce Farrell

9th edition

1337397075, 978-1337397070

More Books

Students also viewed these Programming questions

Question

4. Identify the five steps in brand communications planning.

Answered: 1 week ago

Question

Explain Three functions of the financial market. (15 marks)

Answered: 1 week ago