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)
Heres a suggested implementation for the NestedIfStatementsjava file java import javautilScanner pub... 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
-
Solve Utt = cUTT 0
-
You are presented with the following alphabetical list of accounts and balances (in thousands) for Taggar Enterprises Inc. at June 30, 2012: Additional information: During the year, common shares in...
-
Russell Westbrook of the Oklahoma City Thunder and James Harden of the Houston Rockets were the leading scorers in the NBA for the 20162017 regular season. Together they scored 4914 points, with...
-
In 2016, a hacker tricked a Lamps Plus employee into disclosing the tax information of approximately 1,300 other employees, resulting in the filing of a fraudulent income tax return of Lamps Plus...
-
Depreciation?SYD, Act., SL, and DDB the following data relate to the Plant Assets account of Eshkol, Inc. at December 31, 2010. *In the year an asset is purchased, Eshkol, Inc. does not record any...
-
ces Direct labor-hours Machine-hours Fixed manufacturing overhead cost Variable manufacturing overhead per machine-hour Variable manufacturing overhead per direct labor-hour 35,000 Department Molding...
-
Consider the following demand function for airline tickets (quantities are in thousands): Q = 10-0.1px Px = price of an airline ticket Q = quantity demanded According to the above equation, airline...
-
Q1 : Is it ever morally acceptable to contravene the laweven in an unjust society? Q2 : Discuss conscientious objections.?
-
Walt has a $300,000 listing at 8% commission. An agent from another firm sold the listing. Walt has a 70% commission split with his broker and there is a 50% co-brokerage split. What was Walts...
-
How do cross-cultural differences in socialization practices contribute to variations in social norms, roles, and expectations, and how do individuals navigate these cultural differences in diverse...
-
Echo fund has $80 million in assets and $5 million in liabilities. Shares of the fund currently sell for $100, which represents a 20% discount below NAV. Find the number of shares outstanding. Round...
-
Suppose the following bond quote for IOU Corporation appears in the financial page of today's newspaper. Assume the bond has a face value of $1,000, and the current date is April 19, 2022. Company...
-
A 1199 kg mass car travels with a constant speed of v = 50 km/hour towards a hill that has a semicircular cross section of radius, R = 70 m in a region near the top of the hill as shown in the...
-
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...
-
Draw a block diagram for the feedback control of a liquid-level system, which consists of a valve with a control knob (0\%-100\%) and a liquid-level sensor. Clearly label essential components and...
-
A control system is represented using the block diagram shown in Figure 10.59. Sketch the root locus with respect to the proportional control gain K. Determine all the values of \(K\) for which the...
-
Roughly sketch the root locus with respect to \(K\) for the equation of \(1+K L(s)=0\) and the following choices for \(L(s)\). Make sure to give the asymptotes, arrival or departure angles, and...
Study smarter with the SolutionInn App