your program will correctly output the clothing that the user should wear. 1. Create a new...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
your program will correctly output the clothing that the user should wear. 1. Create a new java file and save it as NestedIfStatements.java. Copy and paste the code from Clothing PickerIfElself java into NestedIfStatements.java. Change the class name. 2. 3. 4. Modify the code, so that you ask the user for the temperature AND whether it is raining or not. At this time, we haven't yet discussed in class how to check if a variable of type String equals a certain set of characters, so for this part of the lab, the user will input 1 to indicate that it is raining, or 0 to indicate that it is not. Figure 6 shows the code that you should use to ask the user for two inputs. Modify the if-else-if, else condition so that each part of the conditional also contains a nested if. You want to take into account the temperature AND whether it is raining or not. The skeleton of that part of the code is shown in Figure 7. Add appropriate print statements throughout the code blocks that indicate what clothing should be worn based on the temperature and if it is raining or not. Compile the program (fix syntax errors, if you find any), and run it several times, to make sure that it gives you the correct outputs. Sample correct outputs of the program are shown in Figure 8. // declare two variables, one of type double and the other of type int double tempOutsideF :74; int raining = 0; // set up a Scanner to receive input from the keyboard Scanner keyboard = new Scanner(System.in); System.out.print("What is the temp outside (in F)? "); tempOutsideF = keyboard.nextDouble(); System.out.print("Is it raining? (enter 1 for yes, e for no)? "); raining keyboard.nextInt(); Figure 6: Use a single scanner class to get multiple inputs from the user // indicate what clothing to wear, based on temperature if (tempOutsideF > 70) { // for any temperature higher than 70 if (raining == 1) { // print statement for rain and temp > 70 }else{ // print statement for no rain and temp> 70 } } else if (tempOutsideF < 30){ // for any temperature below 30 if (raining == 1) { // print statement for rain and temp < 30 }else{ // print statement for no rain and temp < 30 } } } else { // this is the default else; all inputs that are // not caught by one of the above two, are processed // here if (raining == 1) { // default print statement for rain }else{ // default print statement for no rain } CS110, Tatiana Harrison, CWU, Lab 3: If statements Page 4 of 5 Page < 4 of 5 ZOOM + IV. What to hand in Figure 7: Nested if statements ----jGRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 30 Is it raining? (enter 1 for yes, 0 for no)? 1 Wear long-sleeve shirt, pants, shoes, and bring umbrella. ----jGRASP: operation complete. ----j GRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 30 Is it raining? (enter 1 for yes, 0 for no)? 0 Wear long-sleeve shirt, pants, shoes, and bring sunglasses. ----j GRASP: operation complete. -- -- j GRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 80 Is it raining? (enter 1 for yes, 0 for no)? 0 Wear shorts and shirt, and bring sunglasses. ----j GRASP: operation complete. Figure 8: Output for program NestedIfStatements.java The following files should be uploaded to Canvas: What To Wear.java Clothing Picker WScanner.java Clothing Picker IfElself.java NestedIfStatements.java Be sure that each java file is commented and be sure to include your name at the top of each file. If your code does not compile because you've been unable to fix a syntax error, then the comments that you provide in your code will allow you to receive partial credit. Code must also be indented, so that it is easy to read. Finally, make sure that you have given your variables good descriptive names. V. Rubric File / Lab What To Wear java compiles and generates correct output when run Clothing Picker WScannerjava compiles and generates correct output when run Clothing PickerIfElself java compiles and generates correct output when run NestedIfStatements java compiles and generates correct output when run All java files are commented, and contain your name and date Variable names are adequate and descriptive Code is well formatted (indentations), good naming conventions Points 20 20 20 20 5 5 10 Total 100 Page < 5 of 5 C ZOOM + You've learned in lecture about if-statements. In their simplest form, they check for a single condition. If the boolean condition evaluates to true, then the lines of code that are "inside the code block of the if statement are executed. For this part of the lab you'll write a simple program with two if statements. 1 Start jGRASP and create a new java file. 2. 3. Type the text that is shown in Figure 1 into the editor panel of the new java file that you just opened. Change the comments accordingly, so that your name is listed as the author. As was shown in class, be generous with using comments - they make the code much easier to read and use consistent indentation. Save the file as What To Wearjava in your lab3 folder. Compile the program (use the green plus sign), and run it (the red runner button), and make sure that the program outputs what you see in Figure 2. If you have typed any part of the code incorrectly (and introduced a syntax error, you'll have to fix the error, until your code is error-free). 2 3 4 5 10 11 12 16 17 18 19 // Name // Date // Description public class WhatToWear{ public static void main(String[] args) { // declare a variable of type double double tempOutsideF = 45; } // indicate what clothing to wear, based on temperature if (tempOutsideF < 32) System.out.println("Wear hat, boots, pants, jacket, and gloves."); if (tempOutsideF > 32) System.out.println("Wear a sun-hat, t-shirt, shorts, and sandals."); CS110, Tatiana Harrison, CWU, Lab 3: If statements Figure 1: Two simple if-conditional statements ----jGRASP exec: java WhatToWear Wear a t-shirt, shorts, and sandals. ----jGRASP: operation complete. Figure 2: The output of the program in Figure 1 Page 1 of 5 II. Adding user-input to your program You've also seen in lecture how to use the Scanner to retrieve user input from the keyboard. In this part of the lab, you'll revise your code that you wrote in part I; it can be improved in several ways. First, the original Page 1 of 5 с ZOOM + 2. II. Adding user-input to your program You've also seen in lecture how to use the Scanner to retrieve user input from the keyboard. In this part of the lab, you'll revise your code that you wrote in part I; it can be improved in several ways. First, the original program has the temperature hard-coded. Instead, you'd like to ask the user to enter the temperature using the keyboard. Also, in the code for Part I, what would happen if the variable tempOutsideF was assigned the value 30? Then, neither of the conditionals would evaluate to true. In this part of the lab, you'll fix that, too. 1. 3. 4. Figure 1: Two simple if-conditional statements ---- j GRASP exec: java WhatToWear Wear a t-shirt, shorts, and sandals. -jGRASP: operation complete. Figure 2: The output of the program in Figure 1 Create a new java file and save it as ClothingPicker WScannerjava. Copy and paste the code from What To Wear.java into ClothingPicker WScannerjava. You will have to change the name of the class because the class name must be the same as the java file name. To allow a user to provide input, make use of the Scanner class. Do the following: Add import java.util.Scanner to the top of your java file. Refer to Figure 3 of Lab 2 for an example or refer to the lecture slides. In your code, generate a new instance of a Scanner object, and use a System.out.print statement to ask the user to input the temperature. Use the next Double () method of the Scanner class and assign the user's input value to the variable tempOutsideF. Modify the second if conditional, so that it is checks if the temperature is greater than OR equal to 30. Compile the program (fix syntax errors, if you find any), and run it twice with different inputs, to make sure that it gives you the correct output. Sample correct outputs of the program are shown in Figure 3. ----jGRASP exec: java ClothingPickWithScanner What is the temp outside (in F)? 22 Wear boots, pants, sweater, and gloves. ----jGRASP: operation complete. CS110, Tatiana Harrison, CWU, Lab 3: If statements -jGRASP exec: java ClothingPickWithScanner What is the temp outside (in F)? 30 Wear boots, pants, sweater, and gloves. ----jGRASP: operation complete. ----jGRASP exec: java ClothingPickWithScanne What is the temp outside (in F)? 88 Wear a t-shirt, shorts, and sandals. ----jGRASP: operation complete. Figure 3: Output of modified program shown in Figure 1. Page 2 of 5 Page < 2 of 5 ZOOM + III. Adding if-else-if, else Now extend what you've written for part II, and use an if-else-if statement, that you learned about in lecture. An if-else-if statement allows you to have a much more complicated conditional clause. 1. 2. 3. 4. Create a new java file and save it as ClothingPickerIfElself.java. Copy and paste the code from Clothing Picker WScanner.java into ClothingPickerIfElself.java. Change the two if conditionals in the code to a single if-else-if, else conditional, where you check if the temperature is more than 70, less than 30, else the default. The skeleton of the if-else-if-else conditional is shown in Figure 4. Notice that the ordering of the ifs is important, in that the "else" catches those values that are intermediate between temperate values 30 and 70. Compile the program (fix syntax errors, if you find any), and run it three times, to make sure that it gives you the correct outputs. Sample correct output of the program are shown in Figure 5. // indicate what clothing to wear, based on temp if (tempOutsideF > 70) { // for any temperature higher than 70 } else if (tempOutsideF < 30) { // for any temperature below 30 } else { // this is the default else; all inputs that are // not caught by one of the above two, are processed // here } ►► ►► ► Figure 4: Sample if-else-if-else conditional. ----jGRASP exec: java cs110Lab3Part III What the temp outside (in F)? 27 Wear boots, pants, sweater, and gloves. ----j GRASP: operation complete. ----jGRASP exec: java cs110Lab3PartIII What the temp outside (in F)? 36 Wear long-sleeve shirt, pants, and shoes. ----jGRASP: operation complete. ---jGRASP exec: java cs110Lab3Part III What is the temp outside (in F)? 81 Wear t-shirt, shorts, and sandals. ----j GRASP: operation complete. Figure 5: Sample output of code in Figure 4. IV. Using nested ifs For this last part of the lab, you will extend your code from part III, so that you have nested ifs. Here, you'll ask the user to input the temperature AND whether it is raining or no raining outside. Based on those two inputs, CS110, Tatiana Harrison, CWU, Lab 3: If statements Page 3 of 5 Page < 3 of 5 ZOOM + your program will correctly output the clothing that the user should wear. 1. Create a new java file and save it as NestedIfStatements.java. Copy and paste the code from Clothing PickerIfElself java into NestedIfStatements.java. Change the class name. 2. 3. 4. Modify the code, so that you ask the user for the temperature AND whether it is raining or not. At this time, we haven't yet discussed in class how to check if a variable of type String equals a certain set of characters, so for this part of the lab, the user will input 1 to indicate that it is raining, or 0 to indicate that it is not. Figure 6 shows the code that you should use to ask the user for two inputs. Modify the if-else-if, else condition so that each part of the conditional also contains a nested if. You want to take into account the temperature AND whether it is raining or not. The skeleton of that part of the code is shown in Figure 7. Add appropriate print statements throughout the code blocks that indicate what clothing should be worn based on the temperature and if it is raining or not. Compile the program (fix syntax errors, if you find any), and run it several times, to make sure that it gives you the correct outputs. Sample correct outputs of the program are shown in Figure 8. // declare two variables, one of type double and the other of type int double tempOutsideF :74; int raining = 0; // set up a Scanner to receive input from the keyboard Scanner keyboard = new Scanner(System.in); System.out.print("What is the temp outside (in F)? "); tempOutsideF = keyboard.nextDouble(); System.out.print("Is it raining? (enter 1 for yes, e for no)? "); raining keyboard.nextInt(); Figure 6: Use a single scanner class to get multiple inputs from the user // indicate what clothing to wear, based on temperature if (tempOutsideF > 70) { // for any temperature higher than 70 if (raining == 1) { // print statement for rain and temp > 70 }else{ // print statement for no rain and temp> 70 } } else if (tempOutsideF < 30){ // for any temperature below 30 if (raining == 1) { // print statement for rain and temp < 30 }else{ // print statement for no rain and temp < 30 } } } else { // this is the default else; all inputs that are // not caught by one of the above two, are processed // here if (raining == 1) { // default print statement for rain }else{ // default print statement for no rain } CS110, Tatiana Harrison, CWU, Lab 3: If statements Page 4 of 5 Page < 4 of 5 ZOOM + IV. What to hand in Figure 7: Nested if statements ----jGRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 30 Is it raining? (enter 1 for yes, 0 for no)? 1 Wear long-sleeve shirt, pants, shoes, and bring umbrella. ----jGRASP: operation complete. ----j GRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 30 Is it raining? (enter 1 for yes, 0 for no)? 0 Wear long-sleeve shirt, pants, shoes, and bring sunglasses. ----j GRASP: operation complete. -- -- j GRASP exec: java cs110Lab3Part IV What is the temp outside (in F)? 80 Is it raining? (enter 1 for yes, 0 for no)? 0 Wear shorts and shirt, and bring sunglasses. ----j GRASP: operation complete. Figure 8: Output for program NestedIfStatements.java The following files should be uploaded to Canvas: What To Wear.java Clothing Picker WScanner.java Clothing Picker IfElself.java NestedIfStatements.java Be sure that each java file is commented and be sure to include your name at the top of each file. If your code does not compile because you've been unable to fix a syntax error, then the comments that you provide in your code will allow you to receive partial credit. Code must also be indented, so that it is easy to read. Finally, make sure that you have given your variables good descriptive names. V. Rubric File / Lab What To Wear java compiles and generates correct output when run Clothing Picker WScannerjava compiles and generates correct output when run Clothing PickerIfElself java compiles and generates correct output when run NestedIfStatements java compiles and generates correct output when run All java files are commented, and contain your name and date Variable names are adequate and descriptive Code is well formatted (indentations), good naming conventions Points 20 20 20 20 5 5 10 Total 100 Page < 5 of 5 C ZOOM + You've learned in lecture about if-statements. In their simplest form, they check for a single condition. If the boolean condition evaluates to true, then the lines of code that are "inside the code block of the if statement are executed. For this part of the lab you'll write a simple program with two if statements. 1 Start jGRASP and create a new java file. 2. 3. Type the text that is shown in Figure 1 into the editor panel of the new java file that you just opened. Change the comments accordingly, so that your name is listed as the author. As was shown in class, be generous with using comments - they make the code much easier to read and use consistent indentation. Save the file as What To Wearjava in your lab3 folder. Compile the program (use the green plus sign), and run it (the red runner button), and make sure that the program outputs what you see in Figure 2. If you have typed any part of the code incorrectly (and introduced a syntax error, you'll have to fix the error, until your code is error-free). 2 3 4 5 10 11 12 16 17 18 19 // Name // Date // Description public class WhatToWear{ public static void main(String[] args) { // declare a variable of type double double tempOutsideF = 45; } // indicate what clothing to wear, based on temperature if (tempOutsideF < 32) System.out.println("Wear hat, boots, pants, jacket, and gloves."); if (tempOutsideF > 32) System.out.println("Wear a sun-hat, t-shirt, shorts, and sandals."); CS110, Tatiana Harrison, CWU, Lab 3: If statements Figure 1: Two simple if-conditional statements ----jGRASP exec: java WhatToWear Wear a t-shirt, shorts, and sandals. ----jGRASP: operation complete. Figure 2: The output of the program in Figure 1 Page 1 of 5 II. Adding user-input to your program You've also seen in lecture how to use the Scanner to retrieve user input from the keyboard. In this part of the lab, you'll revise your code that you wrote in part I; it can be improved in several ways. First, the original Page 1 of 5 с ZOOM + 2. II. Adding user-input to your program You've also seen in lecture how to use the Scanner to retrieve user input from the keyboard. In this part of the lab, you'll revise your code that you wrote in part I; it can be improved in several ways. First, the original program has the temperature hard-coded. Instead, you'd like to ask the user to enter the temperature using the keyboard. Also, in the code for Part I, what would happen if the variable tempOutsideF was assigned the value 30? Then, neither of the conditionals would evaluate to true. In this part of the lab, you'll fix that, too. 1. 3. 4. Figure 1: Two simple if-conditional statements ---- j GRASP exec: java WhatToWear Wear a t-shirt, shorts, and sandals. -jGRASP: operation complete. Figure 2: The output of the program in Figure 1 Create a new java file and save it as ClothingPicker WScannerjava. Copy and paste the code from What To Wear.java into ClothingPicker WScannerjava. You will have to change the name of the class because the class name must be the same as the java file name. To allow a user to provide input, make use of the Scanner class. Do the following: Add import java.util.Scanner to the top of your java file. Refer to Figure 3 of Lab 2 for an example or refer to the lecture slides. In your code, generate a new instance of a Scanner object, and use a System.out.print statement to ask the user to input the temperature. Use the next Double () method of the Scanner class and assign the user's input value to the variable tempOutsideF. Modify the second if conditional, so that it is checks if the temperature is greater than OR equal to 30. Compile the program (fix syntax errors, if you find any), and run it twice with different inputs, to make sure that it gives you the correct output. Sample correct outputs of the program are shown in Figure 3. ----jGRASP exec: java ClothingPickWithScanner What is the temp outside (in F)? 22 Wear boots, pants, sweater, and gloves. ----jGRASP: operation complete. CS110, Tatiana Harrison, CWU, Lab 3: If statements -jGRASP exec: java ClothingPickWithScanner What is the temp outside (in F)? 30 Wear boots, pants, sweater, and gloves. ----jGRASP: operation complete. ----jGRASP exec: java ClothingPickWithScanne What is the temp outside (in F)? 88 Wear a t-shirt, shorts, and sandals. ----jGRASP: operation complete. Figure 3: Output of modified program shown in Figure 1. Page 2 of 5 Page < 2 of 5 ZOOM + III. Adding if-else-if, else Now extend what you've written for part II, and use an if-else-if statement, that you learned about in lecture. An if-else-if statement allows you to have a much more complicated conditional clause. 1. 2. 3. 4. Create a new java file and save it as ClothingPickerIfElself.java. Copy and paste the code from Clothing Picker WScanner.java into ClothingPickerIfElself.java. Change the two if conditionals in the code to a single if-else-if, else conditional, where you check if the temperature is more than 70, less than 30, else the default. The skeleton of the if-else-if-else conditional is shown in Figure 4. Notice that the ordering of the ifs is important, in that the "else" catches those values that are intermediate between temperate values 30 and 70. Compile the program (fix syntax errors, if you find any), and run it three times, to make sure that it gives you the correct outputs. Sample correct output of the program are shown in Figure 5. // indicate what clothing to wear, based on temp if (tempOutsideF > 70) { // for any temperature higher than 70 } else if (tempOutsideF < 30) { // for any temperature below 30 } else { // this is the default else; all inputs that are // not caught by one of the above two, are processed // here } ►► ►► ► Figure 4: Sample if-else-if-else conditional. ----jGRASP exec: java cs110Lab3Part III What the temp outside (in F)? 27 Wear boots, pants, sweater, and gloves. ----j GRASP: operation complete. ----jGRASP exec: java cs110Lab3PartIII What the temp outside (in F)? 36 Wear long-sleeve shirt, pants, and shoes. ----jGRASP: operation complete. ---jGRASP exec: java cs110Lab3Part III What is the temp outside (in F)? 81 Wear t-shirt, shorts, and sandals. ----j GRASP: operation complete. Figure 5: Sample output of code in Figure 4. IV. Using nested ifs For this last part of the lab, you will extend your code from part III, so that you have nested ifs. Here, you'll ask the user to input the temperature AND whether it is raining or no raining outside. Based on those two inputs, CS110, Tatiana Harrison, CWU, Lab 3: If statements Page 3 of 5 Page < 3 of 5 ZOOM +
Expert Answer:
Answer rating: 100% (QA)
The skin friction coefficient Cf for a laminar boundary ... View the full answer
Related Book For
Intermediate Accounting
ISBN: 978-1260481952
10th edition
Authors: J. David Spiceland, James Sepe, Mark Nelson, Wayne Thomas
Posted Date:
Students also viewed these programming questions
-
1-50. Compute the stress in member BD shown in Figure P1-50 if the applied force F is 2800 lb. Foo 30 45 Fe = 2800 LB
-
Drive-thru Restaurant A B C D Order Accurate 321 263 235 121 Order Not Accurate 39 53 35 18 If one order is selected, find the probability of getting an order from restaurant C or D or an order that...
-
Python A bookstore is organising an online sale. All books on the website are now $15 + 10% GST. If the total price is higher than $100, the shipping is free, else a $10 fee is added. Write a Python...
-
Solve Utt = cUTT 0
-
A tightrope walker who weighs 640 N walks along a steel cable. When he is halfway across, the cable makes an angle of 0.040 rad below the horizontal. (a) What is the strain in the cable? Assume the...
-
For items 26-27: On January 1, 2022, an entity entered into a 5-year lease with a lessor. Annual lease payments of P2,400,000 including annual executory cost of P400,000 are payable at the end of...
-
What was the importance of the Big Bang?
-
The Condé Nast Traveler Gold List for 2012 provided ratings for the top 20 small cruise ships (Condé Nast Traveler website, March 1, 2012). The data shown below are the...
-
A simple random sample of 50 items from a population with = 7 resulted in a sample mean of 37. If required, round your answers to two decimal places. a. Provide a 90% confidence interval for the...
-
You, CPA, work as a consultant on various engagements. Your client, Over The Edge Ltd. (OTE), has grown from a small custom snowboard manufacturer servicing the local market to a multinational...
-
A weigh feeder having four idlers in its weigh-frame indicates a load of 60 kg for the feed material (after all corrections). The idler spacing is 300 mm and the effective belt width is 700 mm. If...
-
When locating an attorney in or outside your state, you can often do your searching online. Martindale-Hubbell has an online directory (http://www.martindale.com), as do most state bar associations....
-
Find the explicit solutions of the following stochastic differential equations: (a) Ornstein-Uhlenbeck process: \[d X_{t}=\mu X_{t} d t+\sigma d B_{t}\] (b) Mean reverting Ornstein-Uhlenbeck process:...
-
For turbulent flow of water in a pipe of diameter \(5 \mathrm{~cm}\) with \(R e=10^{5}\) estimate the magnitude of the length and velocity scales at which viscous dissipation becomes important.
-
If the per-phase line loss in a $60-\mathrm{km}$-long transmission line is not to exceed $60 \mathrm{~kW}$ while it is delivering 100 A per phase, compute the required conductor diameter, if the...
-
There is nothing magical about the eigenfunctions being orthogonal. This can be shown by integration by parts twice. Consider two eigenfunctions \(F_{n}\) and \(F_{m}\), both of which satisfy the...
-
A company you are working for has not been profitable selling its product. Where, relative to the breakeven point, on the graph of the breakeven analysis is the firm currently operating, and why?
-
What is your opinion of advertising awards, such as the Cannes Lions, that are based solely on creativity? If you were a marketer looking for an agency, would you take these creative awards into...
-
On January 1, 2021, Sanderson Variety Store adopted the dollar-value LIFO retail inventory method. Accounting records provided the following information: Estimate ending inventory using the...
-
Ace Leasing acquires equipment and leases it to customers under long-term direct financing leases. Ace earns interest under these arrangements at a 6% annual rate. Ace leased a machine it purchased...
-
Accounting for acquired goodwill has been a controversial issue for many years. In the United States, the amount of acquired goodwill is capitalized and not amortized. Globally, the treatment of...
-
Which of the symbolic representations in Figure 2-24 indicates that a sales invoice has been filed? Figure 2.24 [a] [b] [c] [p]
-
During the review of an internal control system an auditor may review decision tables prepared by the client. A decision table is usually prepared by a client to supplement or replace the preparation...
-
Group dynamics affect the culture of the board. What do you think are the key elements to take into account when a new member arrives?
Study smarter with the SolutionInn App