Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUESTION 1 [32 Marks]. Partial code for the classes Thing, FoodThing, and ExerciseThing is given in Appendix 1. Complete the code as follows: a) Complete

QUESTION 1 [32 Marks]. Partial code for the classes Thing, FoodThing, and ExerciseThing is given in Appendix 1. Complete the code as follows: a) Complete the class header for Thing to indicate that two objects of type Thing can be compared against each other. [1] b) Write the method that performs comparison between two objects of type Thing so that a collection of objects of this type will be naturally sorted in ascending alphabetical order by name. [2] c) The class FoodThing is a subclass of Thing that stores data about a food item. The data shall include: the name of the food item, food category (a character), number of servings (a whole number), and number of calories per serving. As an example, the code Thing pasta = new FoodThing(PASTA, C, 2, 150) creates an object for 2 servings of an item named PASTA, that is of category C (presumably carbohydrate), and has 150 calories per serving. Write code for this class including: i. Appropriate class header [1] ii. Declaration of attributes [1] iii. A getter (accessor) method for each attribute. [2] iv. A constructor that accepts (as parameters) name, category, number of servings, and calories per serving (in that order). The constructor shall ensure that all relevant attributes are correctly initialized. [2] v. A constructor that accepts an object of type FoodThing as its parameter. This method shall initialize the name of the new object to the string Copy-of- + the name of the parameter object. All other attributes of the new object shall be values from the parameter object. For example, if the constructor is called with the pasta object from the example above, it should initialize the attributes of the new object to, Copy-of-PASTA, C, 2, and 150 respectively. [4] vi. A method named totalCalories that accepts no parameter and returns the total calories calculated as: number of servings * calories per serving. [1] vii. A toString method that returns a string formatted as demonstrated by the following example: Category: C Name of food: PASTA Total calories 300 [1]

d) Write segments of code to accomplish each of the following tasks: i. Declare and instantiate foodList as an ArrayList of objects of type Thing. [1] ii. Add an object of type FoodThing to the list. [1]

iii. Assuming that the list has been populated with several objects, write code to create an array named foodArray that can hold all the objects that are in the ArrayList. [1] iv. Sort the ArrayList. [1] v. Copy the objects from foodList to foodArray so that each object in foodArray is created by invoking the second constructor of FoodThing. For example, if the object at position 0 of foodList is PASTA then the object at position 0 of foodArray should be named Copy-of-PASTA and shall have all the other values from the PASTA object.

[2]

vi. Display all objects in foodArray. [2] e) The natural order for comparison of FoodThing objects is by name. Explain briefly how you would accomplish the task of sorting the array of FoodThings by category without changing the natural order of comparison. [2] f) Cite an example (if any) in the code you have written that demonstrates: i. Polymorphism [1] ii. Method overloading [1] iii. Method overriding. [1] g) Discuss briefly, what changes you would have to make to the code you have written if: i. Thing is declared as an abstract class instead of as a concrete class. [2] ii. Thing is declared as an interface.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

Tell the merits and demerits of Mendeleev's periodic table.

Answered: 1 week ago