Question: GIVEN CODE public class Dessert { private String name; private String storeName; private String type; private double calories; public Dessert(String name, String storeName, String type,

GIVEN CODE
public class Dessert { private String name; private String storeName; private String type; private double calories; public Dessert(String name, String storeName, String type, double calories) { this.name = name; this.storeName = storeName; this.type = type; this.calories = calories; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStoreName() { return storeName; } public void setStoreName(String store) { this.storeName = store; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getCalories() { return calories; } public void setCalories(double calories) { this.calories = calories; } public String toString() { return String.format("Dessert [name:%s, category:%s, storeName:%s, calories:%.1f]", name, type, storeName, calories); } }
public class DessertListTest {
public static void main(String[] args) { DessertList list = new DessertList(); Dessert d1 = new Dessert("Lemon Tart", "My Mom Homemade", "Tart", 242); list.add(d1); list.printInfo();
Dessert d2 = new Dessert("Banoffee pie", "Bake a Wish", "Pie", 436.2); list.add(d2); list.printInfo();
Dessert d3 = new Dessert("Honey Toast", "After You", "Bakery", 900.5); list.add(d3); list.printInfo(); Dessert d4 = new Dessert("Tokyo Sundae", "Bake a Wish", "Ice Cream", 500); list.add(d4); list.printInfo();
Dessert d5 = new Dessert("Scone", "Starbucks", "Biscuit", 353); list.add(d5); list.printInfo();
Dessert d6 = new Dessert("Pumpkin pie", "Papa House", "Pie", 436.2); list.add(d6); list.printInfo();
}
}




Goals: To be able to use proper control flow statements to solve a problem To be able to accept input from and output to the console Direction: Do NOT use an array or a collection class For each question, generally, you need at least 2 classes, i.e., at least one working class and only one main/test class (a class with a proper main method). 1 Using the given Dessert class and write a class: Dessertlist which has attributes: list to store a list of added desserts in a form of String data. Dessert List has constructor to initialize its attributes (.e., List to blank String), add method to add a dessert into the list and also keeps the top 3 highest calories desserts, first, second and third, respectively. Additionally, it has a method to printInfo to display details inside it (see the below example) DessertList Dessert -list: String constructor+DessertList() -second +add(dessert: Dessert): void -third +printinfo(): void -first Given the following test program: (Feel free to modify it to check the of other possible case public class DessertListTest { public static void main(String[] args) { DessertList list = new DessertList(); Dessert d1 = new Dessert("Lemon Tart", "My Mom Homemade", "Tart", 242); list.add(di); list.printInfo(); Dessert d2 = new Dessert("Banoffee pie", "Bake a Wish", "Pie", 436.2); list.add(d2); list.printInfo(); Dessert d3 = new Dessert("Honey Toast", "After You", "Bakery", 900.5); list.add(d3); list.printInfo(); Dessert d4 = new Dessert("Tokyo Sundae", "Bake a Wish", "Ice Cream", 500); list.add(04); list.printInfo(); Dessert d5 = new Dessert ("Scone", "Starbucks", "Biscuit", 353); list.add(05); list.printInfo(); Dessert d6 = new Dessert("Pumpkin pie", "Papa House", "Pie", 436.2); list.add(d6); list.printInfo(); } After running the program, below is an example of expected result: List of desserts: Lemon Tart 242.0 Top 3 high calories desserts: 1. Dessert [name: Lemon Tart, category: Tart, storeName: My Mom Homemade, calories: 242.0] 2. null 3. null List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Top 3 high calories desserts: 1. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories:436.2] 2. Dessert [name: Lemon Tart, category:Tart, storeName : My Mom Homemade, calories: 242.0] 3. null List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Top 3 high calories desserts: 1. Dessert [name: Honey Toast, category: Bakery, storeName:After You, calories:900.5] 2. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories: 436.2] 3. Dessert [name: Lemon Tart, category:Tart, storeName: My Mom Homemade, calories: 242.0] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Top 3 high calories desserts: 1. Dessert [name : Honey Toast, category:Bakery, storeName: After You, calories:900.5] 2. Dessert [name : Tokyo Sundae, category: Ice Cream, storeName:Bake a wish, calories:500.0] 3. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories:436.2] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Scone 353.0 Top 3 high calories desserts: 1. Dessert [name : Honey Toast, category: Bakery, storeName: After You, calories:900.5] 2. Dessert [name : Tokyo Sundae, category:Ice Cream, storeName: Bake a Wish, calories: 500.0] 3. Dessert (name:Banoffee pie, category:Pie, storeName : Bake a wish, calories:436.2] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Scone 353.0 Pumpkin pie 436.2 Top 3 high calories desserts: 1. Dessert [name: Honey Toast, category: Bakery, storeName: After You, calories: 900.5] 2. Dessert [name : Tokyo Sundae, category: Ice Cream, storeName: Bake a Wish, calories :500.0] 3. Dessert [name : Banoffee pie, category:Pie, storeName: Bake a wish, calories: 436.2] 2 Write a class, Rational to represent rational number. a. The Rational class has the following properties and methods: o Instance variables: numer and denom to keep the values of a Rational numerator and a denominator, respectively -numer: int Methods: -denom: int set is used for initial the numerator and the denominator gcd returns the greatest common divisor of two numbers. +set(numer: int, denom: int): void This method helps to perform the next method, normalize. The +gcd(a: int, b: int): int god of two numbers can be computed by using the Euclidean +normalize(): String algorithm, which uses a division algorithm such as long division as +format(): void you studied in the school level. The steps of the algorithm is for two numbers a and b (with a >=b), find the remainder of the division of a and b. If the remainder is zero then b is the gcd. Otherwise, find the remainder of b and the previous remainder. Repeat until you get the remainder as zero. For example, ged(4, 6) is 6%4 = 2 ged(4,2) is 4%2 = 0 Then the god of (4, 6) is 2 However, you need to take care of the negative numbers and zero. For instance, ged(-4, 6) = 2, ged(7,-3) = 1, ged(6, 3) = 3 and ged(4,0) = 4. o normalize has 2 main tasks: 1. Simplify the rational 2. Take care and arrange the proper position of the negative sign. For example, if the numerator is 5 and the denominator is -15, it returns String with the simplified rational as -1/3. Or if the numerator is 10 and the denominator is -3, it returns String as -3 1/3. Also, it takes care of the case that the denominator is 0 to return String Infinity or - Infinity O format prints the numerator/denominator = simplified form of the rational For instance, the numerator -7 and the denominator -14, would give: -7/-14 = 1/2 b. Write the test program to accept input a numerator and denominator and show the result. Allow the user to input the numerator and denominator until he/she would like to exit. The below is the sample run of the test program. Enter the numerator: 4 Enter the denominator: 0 -4/0 = - Infinity Do you want to continue? (y) y Enter the numerator: 15 Enter the denominator: 25 15/25 = 3/5 Do you want to continue? (y) y Enter the numerator: 22 Enter the denominator: 10 22/10 = 2 1/5 Do you want to continue? (y) y Enter the numerator: 7 Enter the denominator: -3 7/-3 = -2 1/3 Do you want to continue? (y) y Enter the numerator: -45 Enter the denominator: -9 -45/-9 = 5 Do you want to continue? (y) n Bye! 3 Write the shape drawer which draws a pattern of basic geometry like square and pyramid. Shape DrawerTest ShapeDrawer +FILL_PYRAMID: int = 0 {readOnly} +HOLLOW PYRAMID: int = 1 {readonly} +FILL_SQUARE: int = 2 {readOnly} +HOLLOW_SQUARE: int = 3 {readonly} -INSIDE CHAR: char ='+'{readOnly} -type: int -height: int -drawChar: char constructor+Shape Drawer(type: int, height: int, drawChar: char) constructor+ShapeDrawer() +setType(type: int): void +setHeight(height: int): void +setCharacter(character: char): void +draw(): void +main(args: String[*l): void a. Declare ShapeDrawer class with attributes, type (for type of the pattern shape), height, drawChar (character that uses for drawing) and methods as shown in the given class diagram. The static variables provide for representing the symbolic name of the shape type and the INSIDE_CHAR is the character to fill inside the shape. The logic details of the methods are as follows: - constructors - Default constructor, the constructor with no parameter, sets the type to FILL_SQUARE, height to 3 and drawChar to '*' (star) - Constructor with parameters: sets the type, height and drawChar to the corresponding variables. However, when set to these values, use the setter (if available) in order to check the correctness of them. setType: set a new value for the type. It should set only to the possible type only. If the type is incorrect, give an error message and set the type to FILL_SQUARE (as default). - setHeight: set a new height of the shape drawer. The possible value for the height is between 1 and 12. If the given argument is invalid, give an error message and set the height to 3 which is a default value. draw: draw the pattern shape (fill square, hollow square, fill pyramid, hollow pyramid) corresponding to its type with specified height using specified drawing character of that Shape Drawer object. You can write additional private methods to help to draw each different shape. However, declare the accessibility of them to private. The sample shape with height using * as the drawing character and in the filling using + character. height FILL SQUARE HOLLOW_SQUARE FILL PYRAMID HOLLOW PYRAMID * 1 * * 2 ** ** * * *** ** *** *** 3 *** *** *+* * * *+* ***** *** *** ***** height FILL_SQUARE HOLLOW_SQUARE FILL_PYRAMID HOLLOW_PYRAMID 4 ***** *** *++* *++* * *+ *+++* ******* **** **** ******* 6 ****** ****** * *++++* *++++* *++++* *++++* *+* *+++* *+++++* *+++++++* *********** ****** ****** *********** b. Declare a new test program to accept input from a user. Let the user specify drawing character, type and height. After getting all inputs, draw the shape. Let the user to decide whether he/she wants to continue to draw the previous shape with different height and shape. If yes, continue. Otherwise, stop the program. See example below. The below is the sample run of the test program. Enter the character for drawing: A Possible shapes: 0.Fill Pyramid 1. Hollow Pyramid 2. Fill Square 3: Hollow Square Select the number of shape type: 0 Enter the height of the shape: 3 A A+A AAAAA Do you want to continue? (y) y Possible shapes: 0.Fill Pyramid 1. Hollow Pyramid 2. Fill Square 3: Hollow Square Select the number of shape type: 4 Enter the height of the shape: 13 Error invalid height. Set height to 3! Error invalid type. Set type to FILL_SQUARE! AAA A+A AAA Do you want to continue? (y) n Have a good day. Bye! Goals: To be able to use proper control flow statements to solve a problem To be able to accept input from and output to the console Direction: Do NOT use an array or a collection class For each question, generally, you need at least 2 classes, i.e., at least one working class and only one main/test class (a class with a proper main method). 1 Using the given Dessert class and write a class: Dessertlist which has attributes: list to store a list of added desserts in a form of String data. Dessert List has constructor to initialize its attributes (.e., List to blank String), add method to add a dessert into the list and also keeps the top 3 highest calories desserts, first, second and third, respectively. Additionally, it has a method to printInfo to display details inside it (see the below example) DessertList Dessert -list: String constructor+DessertList() -second +add(dessert: Dessert): void -third +printinfo(): void -first Given the following test program: (Feel free to modify it to check the of other possible case public class DessertListTest { public static void main(String[] args) { DessertList list = new DessertList(); Dessert d1 = new Dessert("Lemon Tart", "My Mom Homemade", "Tart", 242); list.add(di); list.printInfo(); Dessert d2 = new Dessert("Banoffee pie", "Bake a Wish", "Pie", 436.2); list.add(d2); list.printInfo(); Dessert d3 = new Dessert("Honey Toast", "After You", "Bakery", 900.5); list.add(d3); list.printInfo(); Dessert d4 = new Dessert("Tokyo Sundae", "Bake a Wish", "Ice Cream", 500); list.add(04); list.printInfo(); Dessert d5 = new Dessert ("Scone", "Starbucks", "Biscuit", 353); list.add(05); list.printInfo(); Dessert d6 = new Dessert("Pumpkin pie", "Papa House", "Pie", 436.2); list.add(d6); list.printInfo(); } After running the program, below is an example of expected result: List of desserts: Lemon Tart 242.0 Top 3 high calories desserts: 1. Dessert [name: Lemon Tart, category: Tart, storeName: My Mom Homemade, calories: 242.0] 2. null 3. null List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Top 3 high calories desserts: 1. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories:436.2] 2. Dessert [name: Lemon Tart, category:Tart, storeName : My Mom Homemade, calories: 242.0] 3. null List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Top 3 high calories desserts: 1. Dessert [name: Honey Toast, category: Bakery, storeName:After You, calories:900.5] 2. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories: 436.2] 3. Dessert [name: Lemon Tart, category:Tart, storeName: My Mom Homemade, calories: 242.0] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Top 3 high calories desserts: 1. Dessert [name : Honey Toast, category:Bakery, storeName: After You, calories:900.5] 2. Dessert [name : Tokyo Sundae, category: Ice Cream, storeName:Bake a wish, calories:500.0] 3. Dessert [name: Banoffee pie, category:Pie, storeName: Bake a Wish, calories:436.2] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Scone 353.0 Top 3 high calories desserts: 1. Dessert [name : Honey Toast, category: Bakery, storeName: After You, calories:900.5] 2. Dessert [name : Tokyo Sundae, category:Ice Cream, storeName: Bake a Wish, calories: 500.0] 3. Dessert (name:Banoffee pie, category:Pie, storeName : Bake a wish, calories:436.2] List of desserts: Lemon Tart 242.0 Banoffee pie 436.2 Honey Toast 900.5 Tokyo Sundae 500.0 Scone 353.0 Pumpkin pie 436.2 Top 3 high calories desserts: 1. Dessert [name: Honey Toast, category: Bakery, storeName: After You, calories: 900.5] 2. Dessert [name : Tokyo Sundae, category: Ice Cream, storeName: Bake a Wish, calories :500.0] 3. Dessert [name : Banoffee pie, category:Pie, storeName: Bake a wish, calories: 436.2] 2 Write a class, Rational to represent rational number. a. The Rational class has the following properties and methods: o Instance variables: numer and denom to keep the values of a Rational numerator and a denominator, respectively -numer: int Methods: -denom: int set is used for initial the numerator and the denominator gcd returns the greatest common divisor of two numbers. +set(numer: int, denom: int): void This method helps to perform the next method, normalize. The +gcd(a: int, b: int): int god of two numbers can be computed by using the Euclidean +normalize(): String algorithm, which uses a division algorithm such as long division as +format(): void you studied in the school level. The steps of the algorithm is for two numbers a and b (with a >=b), find the remainder of the division of a and b. If the remainder is zero then b is the gcd. Otherwise, find the remainder of b and the previous remainder. Repeat until you get the remainder as zero. For example, ged(4, 6) is 6%4 = 2 ged(4,2) is 4%2 = 0 Then the god of (4, 6) is 2 However, you need to take care of the negative numbers and zero. For instance, ged(-4, 6) = 2, ged(7,-3) = 1, ged(6, 3) = 3 and ged(4,0) = 4. o normalize has 2 main tasks: 1. Simplify the rational 2. Take care and arrange the proper position of the negative sign. For example, if the numerator is 5 and the denominator is -15, it returns String with the simplified rational as -1/3. Or if the numerator is 10 and the denominator is -3, it returns String as -3 1/3. Also, it takes care of the case that the denominator is 0 to return String Infinity or - Infinity O format prints the numerator/denominator = simplified form of the rational For instance, the numerator -7 and the denominator -14, would give: -7/-14 = 1/2 b. Write the test program to accept input a numerator and denominator and show the result. Allow the user to input the numerator and denominator until he/she would like to exit. The below is the sample run of the test program. Enter the numerator: 4 Enter the denominator: 0 -4/0 = - Infinity Do you want to continue? (y) y Enter the numerator: 15 Enter the denominator: 25 15/25 = 3/5 Do you want to continue? (y) y Enter the numerator: 22 Enter the denominator: 10 22/10 = 2 1/5 Do you want to continue? (y) y Enter the numerator: 7 Enter the denominator: -3 7/-3 = -2 1/3 Do you want to continue? (y) y Enter the numerator: -45 Enter the denominator: -9 -45/-9 = 5 Do you want to continue? (y) n Bye! 3 Write the shape drawer which draws a pattern of basic geometry like square and pyramid. Shape DrawerTest ShapeDrawer +FILL_PYRAMID: int = 0 {readOnly} +HOLLOW PYRAMID: int = 1 {readonly} +FILL_SQUARE: int = 2 {readOnly} +HOLLOW_SQUARE: int = 3 {readonly} -INSIDE CHAR: char ='+'{readOnly} -type: int -height: int -drawChar: char constructor+Shape Drawer(type: int, height: int, drawChar: char) constructor+ShapeDrawer() +setType(type: int): void +setHeight(height: int): void +setCharacter(character: char): void +draw(): void +main(args: String[*l): void a. Declare ShapeDrawer class with attributes, type (for type of the pattern shape), height, drawChar (character that uses for drawing) and methods as shown in the given class diagram. The static variables provide for representing the symbolic name of the shape type and the INSIDE_CHAR is the character to fill inside the shape. The logic details of the methods are as follows: - constructors - Default constructor, the constructor with no parameter, sets the type to FILL_SQUARE, height to 3 and drawChar to '*' (star) - Constructor with parameters: sets the type, height and drawChar to the corresponding variables. However, when set to these values, use the setter (if available) in order to check the correctness of them. setType: set a new value for the type. It should set only to the possible type only. If the type is incorrect, give an error message and set the type to FILL_SQUARE (as default). - setHeight: set a new height of the shape drawer. The possible value for the height is between 1 and 12. If the given argument is invalid, give an error message and set the height to 3 which is a default value. draw: draw the pattern shape (fill square, hollow square, fill pyramid, hollow pyramid) corresponding to its type with specified height using specified drawing character of that Shape Drawer object. You can write additional private methods to help to draw each different shape. However, declare the accessibility of them to private. The sample shape with height using * as the drawing character and in the filling using + character. height FILL SQUARE HOLLOW_SQUARE FILL PYRAMID HOLLOW PYRAMID * 1 * * 2 ** ** * * *** ** *** *** 3 *** *** *+* * * *+* ***** *** *** ***** height FILL_SQUARE HOLLOW_SQUARE FILL_PYRAMID HOLLOW_PYRAMID 4 ***** *** *++* *++* * *+ *+++* ******* **** **** ******* 6 ****** ****** * *++++* *++++* *++++* *++++* *+* *+++* *+++++* *+++++++* *********** ****** ****** *********** b. Declare a new test program to accept input from a user. Let the user specify drawing character, type and height. After getting all inputs, draw the shape. Let the user to decide whether he/she wants to continue to draw the previous shape with different height and shape. If yes, continue. Otherwise, stop the program. See example below. The below is the sample run of the test program. Enter the character for drawing: A Possible shapes: 0.Fill Pyramid 1. Hollow Pyramid 2. Fill Square 3: Hollow Square Select the number of shape type: 0 Enter the height of the shape: 3 A A+A AAAAA Do you want to continue? (y) y Possible shapes: 0.Fill Pyramid 1. Hollow Pyramid 2. Fill Square 3: Hollow Square Select the number of shape type: 4 Enter the height of the shape: 13 Error invalid height. Set height to 3! Error invalid type. Set type to FILL_SQUARE! AAA A+A AAA Do you want to continue? (y) n Have a good day. Bye
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
