CS110 Programming Language I Lab 6: Multiple branching Mechanisms Computer Science Department Fall 2016
Lab Objectives: In this lab, the student will practice: Using switch as a branching mechanism Lab Exercise 1: Programming Output Question 1: a. Convert the following if statements to Switch statement: int x = 10; if( x == 10 x == 15) x += 15; else if( x == 12 ) x -= 5; else x *= 3; Solution: int x = 10; Switch( x ){ Case 10 : case 15: X+=15; Break; Case 12 : x-=5; default: x*=3; b. What would be the value of x after executing the switch statement? Solution: 25 P a g e 2
Question 2: Suppose the input is 5. What is the output of the following java code? (Assume that alpha is an int variable and input is a Scanner object initialized to the keyboard). alpha = input.nextint(); switch ( alpha ){ case 1: case 2: alpha = alpha + 2; System.out.print("2 is added to alpha"); case 4: alpha ++; System.out.print("alpha is increased \n"); case 5 : alpha = 2 * alpha; System.out.println("alpha is multiplied by 2"); case 6: alpha = alpha +5; System.out.println("5 is added to alpha"); default: alpha --; System.out.println("The value of alpha is "+ alpha); Solution: Alpha is multiplied by 2 5 is added to alpha The value of alpha is 15 P a g e 3
Lab Exercise 2: Products Problem Description A Shopping website sells four different designs of a specific t-shirt. Each design has its own name: DY: Yellow t-shirt with red sleeves for $10.98. DR: Red t-shirt with white sleeves for $12.98. DB: Blue t-shirt with white sleeves for $11.98. DP: Pink t-shirt with Yellow sleeves for $9.98. Write an application that displays the menu to the customer and gets the Design name, and the Quantity of the design which the customer would like to buy. Your program should use a switch statement to determine the menu. It also should calculates and displays the total price of the t-shirts bought by the customer. Sample output DY : Yellow t-shirt with red sleeves for $10.98. DR : Red t-shirt with white sleeves for $12.98. DB : Blue t-shirt with white sleeves for $11.98. DP : Pink t-shirt with Yellow sleeves for $9.98. Enter the Design name: DB Enter the quantity : 5 The total is $ 59.90 P a g e 4
Solution: package test2; import java.util.scanner; public class Test2 { public static void main(string[] args) { Scanner input=new Scanner (System.in); double total_price=0; System.out.println("DY: Yellow t-shirt with red sleeves for $10.98.\n" + "DR: Red t-shirt with white sleeves for $12.98.\n" + "DB: Blue t-shirt with white sleeves for $11.98. \n" + "DP: Pink t-shirt with Yellow sleeves for $9.98."); System.out.println("Enter the Design name :"); String tshirt=input.next(); System.out.println("Enter the quantity :"); int quantity=input.nextint(); switch(tshirt) { case "DY":case "dy": total_price=10.98*quantity; case "DR":case "dr": total_price=12.98*quantity; case "DB":case "db": total_price=11.98*quantity; case "DP":case "dp": total_price=9.98*quantity; default: System.out.println("invalid design.."); System.out.printf("The total is $%.2f\n",total_price); P a g e 5
Assignment Problem(s) Question 1: Solve the following problems: double Max = 12.7; if (Max >= 12) System.out.print("FRENCH"); else System.out.print("FRIES"); Can the code be converted to Switch statement? Choose one of the following answers: No. Why? the switch statement cannot use double value as an operand Yes. The code after converting is Question 2: What is the output of the following code segment: int x = 12; if (x >= 12) System.out.print("HEY\t"); System.out.print("YOU"); The Output is HEY YOU. P a g e 6
Question 3: A fresh juice shop has a very amazing way to serve its delicious fresh juice.it serves the juice in three different sizes: S: small size which costs $10.25 M: medium size which costs $12.5 L: Large size which costs $15.25 Write a program that displays the menu to the customer and reads from the customer: The size of drink(s) ( S, M, or L) How many drinks would the customer like to buy? Using switch statement, your program should calculate and display the total price followed by the following message: "Please GO to the First line and enjoy our fresh juice and Have a nice day.." If the customer chooses any other letter or enters a number the following message will appear: "sorry the size is invalid.. Have a nice day " Sample output: Welcome to the Fresh juice shop S: small size which costs $10.25 M: medium size which costs $12.5 L: Large size costs $15.25 Enter the size of your drink: S Enter the number of drinks you would like to buy: 5 The total is $ 51.25 Please GO to the First line and enjoy our fresh juice and Have a nice day.. P a g e 7
Solution: package juiceshop; import java.util.scanner; public class JuiceShop { public static void main(string[] args) { Scanner input=new Scanner(System.in); double total=0; System.out.println("Welcome to the Fresh juice shop\n"); System.out.println("S: small size which costs 10.25$, \n"+ "M: medium size which costs 12.5$ \n" + "L: Large size costs 15.25$.\n"); System.out.println("Enter the size of your drink :"); char size=input.next().charat(0); System.out.println("Enter the number of drinks you would like to buy :"); int num=input.nextint(); switch(size) { case 's':case's': total=10.25*num; case 'm':case'm': total=12.5*num; case 'l':case'l': total=15.25*num; default: System.out.println("Sorry the size is undefined.. Have a nice day "); System.out.printf("The total is %.2f\n",total); System.out.println("Please GO to the First line and enjoy our fresh juice and Have a nice day.."); // TODO code application logic here P a g e 8