Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

imageimageimageimageimage

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 +

Step by Step Solution

3.37 Rating (150 Votes )

There are 3 Steps involved in it

Step: 1

The skin friction coefficient Cf for a laminar boundary ... 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

Intermediate Accounting

Authors: J. David Spiceland, James Sepe, Mark Nelson, Wayne Thomas

10th edition

1260481956, 1260310175, 978-1260481952

More Books

Students also viewed these Programming questions

Question

Solve Utt = cUTT 0

Answered: 1 week ago

Question

Explain how anger can negatively affect a conflict situation.

Answered: 1 week ago