Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. Read through the given code ( Lab05.java ).

  2. 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?

  3. Note: there is a chain of if-else-if statements which selects and outputs the programs suggestion, based on the users preferences.

  4. 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.

  1. 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.

  2. 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

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

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

7. Explain how an employee could reduce stress at work.

Answered: 1 week ago