Starting from:

$30

CSE021- Lab 08 Array of Objects Solved

Overview
We will see the power of Object-Oriented Programming (OOP) when we want to create a program that behaves similar to what we have done before (code reuse). This lab will not be limited to just 3 specialty cheeses but any amount set by the user. Luckily, this does not change anything for the Cheese class, and we just have to create a shop that contains an array of items to sell, which we will call ShopArr.

 

Before you get started, read chapters 7.10, 7.11, and 7.12. Answer the Assessment questions as you encounter them in the next section. The prompts for answering assessment questions are placed immediately following the material to which the listed questions relate.

Getting Started
After following the import instructions in the assignment page, you should have a Java project in Eclipse titled Lab 21_8. This PDF document is included in the project in the doc directory. The Java files you will use in this lab are in the src directory.

 

Copy over RunShop.java and Cheese.java from the previous lab. Your program must produce an output matching the sample runs given below.

Part 1: Modify RunShop.java (version 2)
In the original RunShop.java (from previous lab), we are creating an instance of Shop class named shop. Then we call the method run() of this shop object. Now we no longer have an object or class called Shop and instead, we will be using ShopArr. Import RunShop.java from the previous lab to do this step.

 

[Answer assessment question 1]

 

Make the change in RunShop.java file which will allow you to run the newest cheese shop. (same as answer to Q1)

Part 2: Fill-in ShopArr.java 
Everywhere you see comment to “Fill in Code” is where you need to add code to make this program behave correctly. If it says “Fix Code” you need to change existing code. In most places a sample is provided to help you get started. The program currently runs but the behavior is obviously incorrect.

 

Here is a simpler version of intro() as reference

 

public static void intro(String[] names, double[]prices, int[] amounts) {

 // Special 3 Cheeses  if (names.length 0) {   names[0] = " Humboldt Fog";   prices[0] = 25.00;

 }

 if (names.length 1) {   names[1] = "Red Hawk ";

  prices[1] = 40.50;

 }

 if (names.length 2) {   names[2] = "Teleme";

  prices[2] = 17.25;

 }   Random ranGen = new Random(100);

 System.out.println("We sell " + names.length + " kinds of Cheese");  if (names.length 0)

  System.out.println(names[0] + ": $" + prices[0] + " per pound");  if (names.length 1)

  System.out.println(names[1] + ": $" + prices[1] + " per pound");  if (names.length 2)

  System.out.println(names[2] + ": $" + prices[2] + " per pound");

  for (int i = 3; i < names.length; i++) {   names[i] = "Cheese Type " + (char)('A'+i);   prices[i] = ranGen.nextInt(1000)/100.0;

  amounts[i] = 0;

  System.out.println(names[i] + ": $" + prices[i] + " per pound");

 }

}

 

We must now split this method into two methods for this lab: init() and intro(). The reason is because we want to repeat intro() if needed, but creating objects using init() needs to be done only once. So all the println statements will be moved to intro(). Now take a look at ShopArr.java and the init() method in it. We see the code is very similar but the variables have changed. The very first thing we do is create the array of Cheese pointers since we are given the argument max.

 

// Create max number of Cheese pointers cheese = new Cheese[max];

 

Then you will see the code to handle Humboldt Fog cheese:

 

if (max 0) {

 cheese[0] = new Cheese();  cheese[0].setName("Humboldt Fog");

 cheese[0].setPrice(25.00);

}

 

Instead of names.length, we now use max in this code.

 

[Answer assessment question 2]

 

Also, instead of using three different arrays (names, prices and amounts), we now have only 1 array of cheeses. So the code is changed to use cheese[0] which points to the first Cheese object in the array. If we want to change the name then we use a mutator setName, which exists inside the Cheese object

(cheese[0]), that we access using the “.” operator. We instantiate Humboldt Fog using default constructor with 0 arguments followed by invoking two mutators. Red Hawk is instantiated with a 1-argument constructor followed by invoking only one mutator. And finally, Teleme uses a 2-argument constructor, so no mutator calls are necessary. Note that you must use the corresponding accessor method calls in other parts of the code to get to the value of the variables set by the mutators.

 

[Answer assessment questions 3, 4 and 5]

 

Now you will need to implement the for-loop in init(). The original loop is as follows:

 

for (int i = 3; i < names.length; i++) {  names[i] = "Cheese Type " + (char)('A' + i);  prices[i] = ranGen.nextInt(1000)/100.0;  amounts[i] = 0;

}

 

You must figure out the transformations needed in the for-loop to work with a single cheese array instead of 3 arrays, as shown by the code already inside init(). You can assume the amount is already set to 0 for each cheese so the loop doesn’t need to set it again to 0.

 

[Answer assessment question 6]

 

We have also implemented the basic version of ShopArr constructor which just invokes init method with a fixed number, 10. You must fill-in the 1-argument constructor which will invoke init using the max parameter instead.

 

[Answer assessment question 7]

 

Now implement intro(Scanner), itemizedList(), calcSubTotal() and discountSpecials() so they work for an array of cheese pointer. The methods printSubTotals() and printFinalTotal() should be identical to Lab 07.

 

[Answer assessment questions 8 and 9]

Part 3: Modify RunShop.java (version 3)
Now notice that there are two constructors for ShopArr available and version 2 of RunShop (from Part 1) is only calling the default constructor with no arguments. So, the program will always create 10 cheeses to sell. We will need to make use of the second constructor which takes an argument max and sets the amount of cheeses to sell.

 

Modify RunShop so it asks the following question to the user and then pass the number user enters to the ShopArr constructor. So the program now starts as follows: 

 

Enter the number of Cheeses for shop setup: 12

We sell 12 types of Cheese (in 0.5 lb packages)

 

Everything should work as it did before, now you can just change the number of cheese from 10 to any amount you want (including 0 but not negatives).

 

[Answer assessment question 10]

 

Part 4: (Assessment) Logic Check and Level of Understanding 
Create a Word document or text file named Part4 that contains answers to the following:

1)     What are the minimal changes required to instantiate ShopArr and invoke run() on it?

2)     We can also use a <something.length instead of max. What is the valid <something to use in ShopArr.java?

3)     How can we tell which instantiation (new Cheese) corresponds to which constructor definition inside the Cheese class?

4)     How can we identify a mutator method call?

5)     What would be the result if we added this line right after Teleme is created:    cheese[2].setName("Wrong Name"); ?

6)     Why is the init() method both private and void?

7)     What are the distinguishing features of constructor methods? (i.e., How do we tell them apart from other methods?)

8)     How can we figure out the number of required iterations for each loop?

9)     Should we pass in Cheese array pointer (cheese[]) as arguments into calcSubTotal or itemizedList? (Why or why not)

10)  What value will be printed by RunShop for “Ran with Cheese Total”? (fixed number or a formula)

 

Sample Runs (user input shown in green, with each run separated by a dashed-line):

 

-------------------------------------------------------------------------------SAMPLE RUN 1

1   Enter the number of Cheeses for shop setup: 0

2   We sell 0 kinds of Cheese (in 0.5 lb packages)

3   

4   

5  Display the itemized list? (1 for yes): 1 6  No items were purchased.

7   

8   Original Sub Total:     $0.00

9   Specials...

10  None      -$0.0

11  New Sub Total:    $0.00

12  Additional 0% Discount:      -$0.0

13  Final Total:     $0.00

14

15 Do you wish to redo your whole order? (1 for yes): 0

16

17  Thanks for coming!

18  Ran with Cheese Total: 0

-------------------------------------------------------------------------------SAMPLE RUN 2

1   Enter the number of Cheeses for shop setup: 1

2   We sell 1 kinds of Cheese (in 0.5 lb packages)

3   Humboldt Fog: $25.0 per pound

4   

5  Enter the amount of Humboldt Fog in lb: 1

6   

7        Display the itemized list? (1 for yes): 1

8        1.0 lb of Humboldt Fog @ $25.00 = $25.00 9   

10  Original Sub Total:     $25.00

11  Specials...

12  Humboldt Fog (Buy 1 Get 1 Free): -$12.50

13  New Sub Total:    $12.50

14  Additional 0% Discount:      -$0.0

15  Final Total:     $12.50

16

17 Do you wish to redo your whole order? (1 for yes): 1

18

19  We sell 1 kinds of Cheese (in 0.5 lb packages)

20  Humboldt Fog: $25.0 per pound

21

22 Enter the amount of Humboldt Fog in lb: 0

23

24 Display the itemized list? (1 for yes): 1 25 No items were purchased.

26

27  Original Sub Total:     $0.00

28  Specials...

29  None      -$0.0

30  New Sub Total:    $0.00

31  Additional 0% Discount:      -$0.0

32  Final Total:     $0.00

33

34 Do you wish to redo your whole order? (1 for yes): 0

35

36 Thanks for coming!
 

37 Ran with Cheese Total: 1

-------------------------------------------------------------------------------SAMPLE RUN 3

1   Enter the number of Cheeses for shop setup: 4

2   We sell 4 kinds of Cheese (in 0.5 lb packages)

3   Humboldt Fog: $25.0 per pound

4   Red Hawk: $40.5 per pound

5   Teleme: $17.25 per pound

6   Cheese Type D: $9.15 per pound

7   

8        Enter the amount of Humboldt Fog in lb: 4.5

9        Enter the amount of Red Hawk in lb: 4.5

10       Enter the amount of Teleme in lb: -3

11       Invalid input. Enter a value = 0: 1.7

12       Invalid input. Enter a value that's multiple of 0.5: -4

13       Invalid input. Enter a value = 0: 1.9

14       Invalid input. Enter a value that's multiple of 0.5: 1.5 15 Enter the amount of Cheese Type D in lb: 10 16

17       Display the itemized list? (1 for yes): 1

18       4.5 lb of Humboldt Fog @ $25.00 = $112.50

19       4.5 lb of Red Hawk @ $40.50 = $182.25

20       1.5 lb of Teleme @ $17.25 = $25.88

21       10.0 lb of Cheese Type D @ $9.15 = $91.50 22

23  Original Sub Total:     $412.13

24  Specials...

25  Humboldt Fog (Buy 1 Get 1 Free): -$50.00

26  Red Hawk (Buy 2 Get 1 Free): -$60.75

27  New Sub Total:    $301.38

28  Additional 15% Discount:     -$75.34

29  Final Total:     $226.03

30

31 Do you wish to redo your whole order? (1 for yes): 0

32

33  Thanks for coming!

34  Ran with Cheese Total: 4

-------------------------------------------------------------------------------SAMPLE RUN 4

1        Enter the number of Cheeses for shop setup: 12

2        We sell 12 kinds of Cheese (in 0.5 lb packages)

3        Humboldt Fog: $25.0 per pound

4        Red Hawk: $40.5 per pound

5        Teleme: $17.25 per pound

6        Cheese Type D: $9.15 per pound 7  Cheese Type E: $2.5 per pound

8   Cheese Type F: $8.74 per pound

9   Cheese Type G: $9.88 per pound

10  Cheese Type H: $2.91 per pound

11  Cheese Type I: $6.66 per pound

12  Cheese Type J: $0.36 per pound

13  Cheese Type K: $2.88 per pound

14  Cheese Type L: $7.23 per pound

15

16  Enter the amount of Humboldt Fog in lb: 1.5

17  Enter the amount of Red Hawk in lb: 2.2

18  Invalid input. Enter a value that's multiple of 0.5: 2.5

19  Enter the amount of Teleme in lb: 0

20  Enter the amount of Cheese Type D in lb: 0

21  Enter the amount of Cheese Type E in lb: 0

22  Enter the amount of Cheese Type F in lb: 0

23  Enter the amount of Cheese Type G in lb: 8

24  Enter the amount of Cheese Type H in lb: 10
25       Enter the amount of Cheese Type I in lb: 0

26       Enter the amount of Cheese Type J in lb: 0

27       Enter the amount of Cheese Type K in lb: 0

28       Enter the amount of Cheese Type L in lb: 0 29

30       Display the itemized list? (1 for yes): 1

31       1.5 lb of Humboldt Fog @ $25.00 = $37.50

32       2.5 lb of Red Hawk @ $40.50 = $101.25

33       8.0 lb of Cheese Type G @ $9.88 = $79.04

34       10.0 lb of Cheese Type H @ $2.91 = $29.10 35

36  Original Sub Total:     $246.89

37  Specials...

38  Humboldt Fog (Buy 1 Get 1 Free): -$12.50

39  Red Hawk (Buy 2 Get 1 Free): -$20.25

40  New Sub Total:    $214.14

41  Additional 10% Discount:     -$21.41

42  Final Total:     $192.73

43

44 Do you wish to redo your whole order? (1 for yes): 1

45

46  We sell 12 kinds of Cheese (in 0.5 lb packages)

47  Humboldt Fog: $25.0 per pound

48  Red Hawk: $40.5 per pound

49  Teleme: $17.25 per pound

50  Cheese Type D: $9.15 per pound

51  Cheese Type E: $2.5 per pound

52  Cheese Type F: $8.74 per pound

53  Cheese Type G: $9.88 per pound

54  Cheese Type H: $2.91 per pound

55  Cheese Type I: $6.66 per pound

56  Cheese Type J: $0.36 per pound

57  Cheese Type K: $2.88 per pound

58  Cheese Type L: $7.23 per pound

59

60       Enter the amount of Humboldt Fog in lb: 0

61       Enter the amount of Red Hawk in lb: 0

62       Enter the amount of Teleme in lb: 12

63       Enter the amount of Cheese Type D in lb: 0

64       Enter the amount of Cheese Type E in lb: 1

65       Enter the amount of Cheese Type F in lb: 2

66       Enter the amount of Cheese Type G in lb: 3

67       Enter the amount of Cheese Type H in lb: 0

68       Enter the amount of Cheese Type I in lb: 0

69       Enter the amount of Cheese Type J in lb: 5

70       Enter the amount of Cheese Type K in lb: 0

71       Enter the amount of Cheese Type L in lb: -3

72       Invalid input. Enter a value = 0: 1.1

73       Invalid input. Enter a value that's multiple of 0.5: 4 74

75 Display the itemized list? (1 for yes): 0 76

77  Original Sub Total:     $287.34

78  Specials...

79  None      -$0.0

80  New Sub Total:    $287.34

81  Additional 15% Discount:     -$71.84

82  Final Total:     $215.51

83

84 Do you wish to redo your whole order? (1 for yes): 0
 

85  Thanks for coming!

86  Ran with Cheese Total: 12

 

 

More products