Question
Exercise This lab is designed to give you practice converting an if-else-if statement into nested if statements.Getting Started To start this exercise, you should: 1.
Exercise
This lab is designed to give you practice converting an if-else-if statement into nested if statements.Getting Started To start this exercise, you should:
1. Open eclipse and start a new Java project named Lab05
2. Add a Class (named Lab05) to this project.
3. Cut the program from this document and paste (replacing everything) in the Eclipse editor window.
Lab05.java
import java.util.Scanner;
public class Lab05 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
// System.out.println("CS 250 Restaurant Guide ");
String response; char s, f; boolean spicy, fancy;
// Ask user for his/her preference System.out.print("Do you like spicy food? (y / n) : "); // Get the next token response = stdIn.next(); // Look only at first character s = response.charAt(0); if (s == y || s == Y)
spicy = true; else
spicy = false;
// Ask user for his/her preference System.out.print("Do you want to go to a fancy restaurant? (y / n) : ");
// Get the next token response = stdIn.next(); // Look only at first character f = response.charAt(0); fancy = (f == y) || (f == Y);
// Make suggestion if (spicy && fancy)
System.out.println("I suggest you go to Thai Garden Palace."); else if (!spicy && !fancy)
System.out.println("I suggest you go to Joes Diner."); else if (spicy && !fancy)
System.out.println("I suggest you go to Albertos Tacqueria."); else if (!spicy && fancy)
System.out.println("I suggest you go to Chez Paris.");
stdIn.close(); }
}
Problem Description
-
Read through the given code ( Lab05.java ).
-
This program suggests restaurants based on a users preferences for spiciness and fanciness. Notice that the yes / no answers are converted to boolean results in two different ways, are they equivalent and why?
-
Note: there is a chain of if-else-if statements which selects and outputs the programs suggestion, based on the users preferences.
-
Your task is to convert this chain of if-else-if statements into a pair of nested if-else statements. Your new code will look something like this:
if (
) { if (
) { System.out.println( ... }
else // means if ( !
) { System.out.println( ... }
} else // means if ( !
) { if (
) { System.out.println( ... }
else // means if ( !
)
{ System.out.println( ...
} }
Note: the conditions are numbered for identification only, this does not imply that they are all unique.
-
Indicate which (if any) of the above ifs or elses above really need to be written in block form. Feel free to omit the blocks where they are not needed, in your code.
-
Once you have written your pair of nested if-else statements: (a) Make sure that your program compiles and runs without errors or warnings. (b) Run your program enough times to check all the choices for correctness.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started