Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Error.java - part of this should be in code Overview We will review the following statements in this lab: Scanner Variables (access types) System.out.print/println/printf Conditional

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

Error.java - part of this should be in code

image text in transcribed

Overview We will review the following statements in this lab: Scanner Variables (access types) System.out.print/println/printf Conditional statements (if, else, else if) Arrays Loops Getting Started After starting Eclipse, create a new project called Lab21_3. Import Error.java from the assignment page. Examine at the code given in Error.java. It will run as is, so go ahead and run it. You should see the following output (prices per pound for cheese types D-J might be different than shown): We sell 10 kinds of Cheese Humboldt Fog: $25.0 per pound Red Hawk : $40.50 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound Cheese Type G: $9.88 per pound Cheese Type H: $2.91 per pound Cheese Type I: $6.66 per pound Cheese Type 3: $0.36 per pound Let us analyze the code to figure out how it is working. To begin with, we see a new kind of declaration for a variable: final int MAXCHEESE = 10; Final (final) is an access modifier that prevents the variable's value from changing after its initialization. Such an initialized variable, called a constant variable, is typically used when we want a value that cannot be accidently overwritten in the rest of the program. Thus, the value of a constant variable is set only once [cf. Section 2.9). Next we declare 3 arrays: String[] names - new String[MAXCHEESE); double[] prices - new double [MAX CHEESE); int[] amounts - new int[MAX CHEESE); Next, we set values corresponding to Humboldt Fog cheese (from Lab 02) as the first entry in the names and prices arrays: names[] - "Humboldt Fog"; prices [@] - 25.0; ... The code repeats for the other two special cheeses. Note the meaningful variable names that self-describe their purpose (cf. Section 2.3). The program then prints out how many types of cheese are currently being sold, and a list of names and prices: System.out.println("We sell " + MAXCHEESE + " kinds of Cheese"); System.out.println(names [@] + ": " + prices[] + " per pound" ); System.out.println(names [1] + ": " + prices[1] + " per pound" ); System.out.println(names[2] + ": $" + prices[2] + " per pound"); The code so far handles the first three cheeses. But this time, we want to sell up to MAXCHEESE (=10) cheeses. We assume the remaining cheeses besides the first three are generic cheeses whose names and prices aren't too important. We first need to come up with a way to set the prices of these generic cheeses. redetermined prices, we decide to set them randomly at the beginning. To do this we use a random number generator object (cf. Section 2.18] that will help us (like a Scanner) generate the prices. And to ensure that the Random object generates the same random sequence of integers each time the program is run (so that our randomly chosen prices stay the same), we specify a seed -- the input to the equation that generates the random values of 100. This is done as follows: Random ranGen - new Random(100); We can now output information about the rest of the cheeses with the following for-loop. for (int i = 3; i = @: 1.5 17 Display the itemized list? (1 for yes) 1 18 2.5 lb of Humboldt Fog @ $25.00 - $62.50 19 3.5 lb of Red Hawk @ $40.50 - $141.75 28 1.5 lb of Cheese Type D @ $9.15 - $13.73 21 22 Original Sub Total: $217.98 23 Specials... 24 Humboldt Fog (Buy 1 Get 1 Free): $25.00 25 Red Hawk (Buy 2 Get 1 Free) : -$40.50 26 New Sub Total: $152.48 27 Additional 10% Discount: -$15.25 28 Final Total: $137.23 ------SAMPLE RUN 6 Enter the number of Cheeses for shop setup: 10 3 We sell 10 kinds of Cheese (in 0.5 lb packages) Humboldt Fog: $25.0 per pound Red Hawk: $40.5 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound 18 Cheese Type G: $9.88 per pound 11 Cheese Type H: $2.91 per pound 12 Cheese Type I: $6.66 per pound 13 Cheese Type J: $0.36 per pound 15 Enter the amount of Humboldt Fog in lb: 1 16 Enter the amount of Red Hawk in lb: 2 17 Enter the amount of Teleme in lb: 3 18 Enter the amount of Cheese Type D in lb: 0 19 Enter the amount of Cheese Type E in lb: 0 19 Enter the amount of Cheese Type F in lb: 8.3 1 Invalid input. Enter a value that's multiple of 0.5: 4 2 Enter the amount of Cheese Type G in lb: 5 3 Enter the amount of Cheese Type H in lb: 24 Enter the amount of Cheese Type I in lb: 25 Enter the amount of Cheese Type ) in lb: 6 27 Display the itemized list? (1 for yes) 29 Original Sub Total: $244.27 38 Specials... 31 Humboldt Fog (Buy 1 Get 1 Free): -$12.50 32 Red Hawk (Buy 2 Get 1 Free) : - $20.25 33 New Sub Total: $211.52 34 Additional 10% Discount: -$21.15 35 Final Total: $190.37 --SAMPLE RUN 7 Enter the number of Cheeses for shop setup: 8 We sell 8 kinds of Cheese (in 0.5 lb packages) Humboldt Fog: $25.0 per pound Red Hawk : $40.5 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound Cheese Type G: $9.88 per pound 1 Cheese Type H: $2.91 per pound 13 Enter the amount of Humboldt Fog in lb: 4 Enter the amount of Red Hawk in lb: 5 15 Enter the amount of Teleme in lb: 0 16 Enter the amount of Cheese Type D in lb: 8 17 Enter the amount of Cheese Type E in lb: 8 18 Enter the amount of Cheese Type F in lb: 3.7 19 Invalid input. Enter a value that's multiple of 0.5: 3.2 29 Invalid input. Enter a value that's multiple of 0.5: 3.5 21 Enter the amount of Cheese Type G in lb: 5.5 22 Enter the amount of Cheese Type H in lb: 8 24 Display the itemized list? (1 for yes) 1 25 4.lb of Humboldt Fog @ $25.00 - $100.00 26 5.2 lb of Red Hawk @ $48.50 - $202.50 27 3.5 lb of Cheese Type F @ $8.74 - $30.59 28 5.5 lb of Cheese Type G @ $9.88 = $54.34 3e Original Sub Total: $387.43 1 Specials... 32 Humboldt Fog (Buy 1 Get 1 Free): $50.00 3 Red Hawk (Buy 2 Get 1 Free): - $60.75 14 New Sub Total: $276.68 35 Additional 25% Discount : - $69.17 36 Final Total: $207.51 Quick Access : @ OP EET #99996- Error.java X * final int MAXCHEESE = 10; String() names = new String [MAXCHEESE]; doublecl prices = new double [MAXCHEESE]; doublecl amounts = new double[MAXCHEESE]; // Three Special Cheeses names [0] = "Humboldt Fog"; prices[O] - 25.00; names [1] = "Red Hawk"; prices[1] = 40.50; names [2] = "Teleme"; prices[2] = 17.25; System.out.println("We sell " + MAXCHEESE + " kinds of Cheese: "); System.out.println(names [0] + ": $" + prices [0] + " per pound"); System.out.println(names[1] + ": $" + prices[1] + " per pound"); System.out.println(names [2] + ": $" + prices [2] + " per pound"); Random ranGen = new Random(100); for (int i = 0; i Error (Java Application) /Library/Java/JavaVirtual Machines/jdk1.8.0_221.jdk/Contents/Home/bin/java (Feb 19, 2020, 2:29:38 PM) We sell 10 kinds of Cheese: Humboldt Fog: $25.0 per pound Red Hawk: $40.5 per pound Teleme: $17.25 per pound Cheese Type A: $9.15 per pound Cheese Type B: $2.5 per pound Cheese Type C: $8.74 per pound Cheese Type D: $9.88 per pound Cheese Type E: $2.91 per pound Cheese Type F: $6.66 per pound Cheese Type G: $0.36 per pound Cheese Type H: $2.88 per pound Cheese Type I: $7.23 per pound Cheese Type J: $7.13 per pound Overview We will review the following statements in this lab: Scanner Variables (access types) System.out.print/println/printf Conditional statements (if, else, else if) Arrays Loops Getting Started After starting Eclipse, create a new project called Lab21_3. Import Error.java from the assignment page. Examine at the code given in Error.java. It will run as is, so go ahead and run it. You should see the following output (prices per pound for cheese types D-J might be different than shown): We sell 10 kinds of Cheese Humboldt Fog: $25.0 per pound Red Hawk : $40.50 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound Cheese Type G: $9.88 per pound Cheese Type H: $2.91 per pound Cheese Type I: $6.66 per pound Cheese Type 3: $0.36 per pound Let us analyze the code to figure out how it is working. To begin with, we see a new kind of declaration for a variable: final int MAXCHEESE = 10; Final (final) is an access modifier that prevents the variable's value from changing after its initialization. Such an initialized variable, called a constant variable, is typically used when we want a value that cannot be accidently overwritten in the rest of the program. Thus, the value of a constant variable is set only once [cf. Section 2.9). Next we declare 3 arrays: String[] names - new String[MAXCHEESE); double[] prices - new double [MAX CHEESE); int[] amounts - new int[MAX CHEESE); Next, we set values corresponding to Humboldt Fog cheese (from Lab 02) as the first entry in the names and prices arrays: names[] - "Humboldt Fog"; prices [@] - 25.0; ... The code repeats for the other two special cheeses. Note the meaningful variable names that self-describe their purpose (cf. Section 2.3). The program then prints out how many types of cheese are currently being sold, and a list of names and prices: System.out.println("We sell " + MAXCHEESE + " kinds of Cheese"); System.out.println(names [@] + ": " + prices[] + " per pound" ); System.out.println(names [1] + ": " + prices[1] + " per pound" ); System.out.println(names[2] + ": $" + prices[2] + " per pound"); The code so far handles the first three cheeses. But this time, we want to sell up to MAXCHEESE (=10) cheeses. We assume the remaining cheeses besides the first three are generic cheeses whose names and prices aren't too important. We first need to come up with a way to set the prices of these generic cheeses. redetermined prices, we decide to set them randomly at the beginning. To do this we use a random number generator object (cf. Section 2.18] that will help us (like a Scanner) generate the prices. And to ensure that the Random object generates the same random sequence of integers each time the program is run (so that our randomly chosen prices stay the same), we specify a seed -- the input to the equation that generates the random values of 100. This is done as follows: Random ranGen - new Random(100); We can now output information about the rest of the cheeses with the following for-loop. for (int i = 3; i = @: 1.5 17 Display the itemized list? (1 for yes) 1 18 2.5 lb of Humboldt Fog @ $25.00 - $62.50 19 3.5 lb of Red Hawk @ $40.50 - $141.75 28 1.5 lb of Cheese Type D @ $9.15 - $13.73 21 22 Original Sub Total: $217.98 23 Specials... 24 Humboldt Fog (Buy 1 Get 1 Free): $25.00 25 Red Hawk (Buy 2 Get 1 Free) : -$40.50 26 New Sub Total: $152.48 27 Additional 10% Discount: -$15.25 28 Final Total: $137.23 ------SAMPLE RUN 6 Enter the number of Cheeses for shop setup: 10 3 We sell 10 kinds of Cheese (in 0.5 lb packages) Humboldt Fog: $25.0 per pound Red Hawk: $40.5 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound 18 Cheese Type G: $9.88 per pound 11 Cheese Type H: $2.91 per pound 12 Cheese Type I: $6.66 per pound 13 Cheese Type J: $0.36 per pound 15 Enter the amount of Humboldt Fog in lb: 1 16 Enter the amount of Red Hawk in lb: 2 17 Enter the amount of Teleme in lb: 3 18 Enter the amount of Cheese Type D in lb: 0 19 Enter the amount of Cheese Type E in lb: 0 19 Enter the amount of Cheese Type F in lb: 8.3 1 Invalid input. Enter a value that's multiple of 0.5: 4 2 Enter the amount of Cheese Type G in lb: 5 3 Enter the amount of Cheese Type H in lb: 24 Enter the amount of Cheese Type I in lb: 25 Enter the amount of Cheese Type ) in lb: 6 27 Display the itemized list? (1 for yes) 29 Original Sub Total: $244.27 38 Specials... 31 Humboldt Fog (Buy 1 Get 1 Free): -$12.50 32 Red Hawk (Buy 2 Get 1 Free) : - $20.25 33 New Sub Total: $211.52 34 Additional 10% Discount: -$21.15 35 Final Total: $190.37 --SAMPLE RUN 7 Enter the number of Cheeses for shop setup: 8 We sell 8 kinds of Cheese (in 0.5 lb packages) Humboldt Fog: $25.0 per pound Red Hawk : $40.5 per pound Teleme: $17.25 per pound Cheese Type D: $9.15 per pound Cheese Type E: $2.5 per pound Cheese Type F: $8.74 per pound Cheese Type G: $9.88 per pound 1 Cheese Type H: $2.91 per pound 13 Enter the amount of Humboldt Fog in lb: 4 Enter the amount of Red Hawk in lb: 5 15 Enter the amount of Teleme in lb: 0 16 Enter the amount of Cheese Type D in lb: 8 17 Enter the amount of Cheese Type E in lb: 8 18 Enter the amount of Cheese Type F in lb: 3.7 19 Invalid input. Enter a value that's multiple of 0.5: 3.2 29 Invalid input. Enter a value that's multiple of 0.5: 3.5 21 Enter the amount of Cheese Type G in lb: 5.5 22 Enter the amount of Cheese Type H in lb: 8 24 Display the itemized list? (1 for yes) 1 25 4.lb of Humboldt Fog @ $25.00 - $100.00 26 5.2 lb of Red Hawk @ $48.50 - $202.50 27 3.5 lb of Cheese Type F @ $8.74 - $30.59 28 5.5 lb of Cheese Type G @ $9.88 = $54.34 3e Original Sub Total: $387.43 1 Specials... 32 Humboldt Fog (Buy 1 Get 1 Free): $50.00 3 Red Hawk (Buy 2 Get 1 Free): - $60.75 14 New Sub Total: $276.68 35 Additional 25% Discount : - $69.17 36 Final Total: $207.51 Quick Access : @ OP EET #99996- Error.java X * final int MAXCHEESE = 10; String() names = new String [MAXCHEESE]; doublecl prices = new double [MAXCHEESE]; doublecl amounts = new double[MAXCHEESE]; // Three Special Cheeses names [0] = "Humboldt Fog"; prices[O] - 25.00; names [1] = "Red Hawk"; prices[1] = 40.50; names [2] = "Teleme"; prices[2] = 17.25; System.out.println("We sell " + MAXCHEESE + " kinds of Cheese: "); System.out.println(names [0] + ": $" + prices [0] + " per pound"); System.out.println(names[1] + ": $" + prices[1] + " per pound"); System.out.println(names [2] + ": $" + prices [2] + " per pound"); Random ranGen = new Random(100); for (int i = 0; i Error (Java Application) /Library/Java/JavaVirtual Machines/jdk1.8.0_221.jdk/Contents/Home/bin/java (Feb 19, 2020, 2:29:38 PM) We sell 10 kinds of Cheese: Humboldt Fog: $25.0 per pound Red Hawk: $40.5 per pound Teleme: $17.25 per pound Cheese Type A: $9.15 per pound Cheese Type B: $2.5 per pound Cheese Type C: $8.74 per pound Cheese Type D: $9.88 per pound Cheese Type E: $2.91 per pound Cheese Type F: $6.66 per pound Cheese Type G: $0.36 per pound Cheese Type H: $2.88 per pound Cheese Type I: $7.23 per pound Cheese Type J: $7.13 per pound

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

More Books

Students also viewed these Databases questions