Question
Implementpublic abstract class Food{}: The class has three attributes (private data fields): String name double price static int numFood = 0 //a var to hold
- Implement public abstract class Food{}:
- The class has three attributes (private data fields):
- String name
- double price
- static int numFood = 0 //a var to hold the count of all Foods created
- Define at least one constructor as defined in the UML above
- Implement Getters & Setters for private instance data fields
- Implement a getter method for the static counter variable
- Declare an abstract method getType() that returns a String
- Override toString() from java.lang.Object to return name
- 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.
- The class has three attributes (private data fields):
[COMPREHENSION 1]: Include a block comment explaining why Food is declared as abstract, as well as why the method getType() is declared as abstract.
- Implement a concrete subclass, Vegetable{}, that extends Food
- The class has two additional attributes (private data fields):
- String color
- static int numVeg = 0;
- Overload the constructor at least two times as described in the UML
- Implement Getters & Setters for private data fields
- Implement a getter method for the static counter variable
- Override (provide a definition for) getType() to return the name of the class
- Override toString() from Food to include the type of food, color and price
- The class has two additional attributes (private data fields):
[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.
- Implement a concrete subclass, Fruit{}, that extends Food
- The class has two additional attributes (private data fields):
- boolean seeds
- static int numMeat = 0;
- Overload the constructor at least two times as described in the UML
- Implement Getters & Setters for private data fields
- Implement a getter method for the static counter variable
- Override (provide a definition for) getType() to return the name of the class
- Override toString() from Food to include the type of food, whether or not seedless, and price
- The class has two additional attributes (private data fields):
[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.
- 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.
- main method:
- The main method creates a single-dimensional array to represent a shopping cart at the grocery store.
- main method:
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);
- Additionally, there are menu options printed out in the main method as well as method stubs for the methods described below.
- public static void displayCart(Food[]list){}
- 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).
- public static void searchCart(Food[]list, String foodName){}
- 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.
- For full credit, this method must invoke the equals(Object o) method that you defined in Food.
- public static void sortByPrice(Food[]cart){}
- accept an array of Food and sort by price per item in descending order using the sorting algorithm of your choice.
- public static void checkOut(Food[]cart){}
- 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.
- 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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started