Question: Use of Multi-way if-else statements and switch statements Problem statement: Use a random number generator to generate an integer in the range of 0 to

 Use of Multi-way if-else statements and switch statements Problem statement: Use

Use of Multi-way if-else statements and switch statements Problem statement: Use a random number generator to generate an integer in the range of 0 to 6 and store it in an integer type variable day. Use a multi-way if-else statement to print name of weekday -Repeat the process with a switch statement. Task 1: Using a multi-way if-else statement that prints out the name of weekday represented by the number stored in variable day (0 Sunday, 1-Monday, 2 Tuesday, 3- Wednesday, 4- Thursday, 5 -Friday, 6 Saturday - and Invalid day for all other values) Declare a variable named day of int type Place a random number generated in the range of [0-6] in day // Generate a random integer between 0 to 6 (both inclusive) and store in day day - (int) (Math.random() * 7); Write a multi-way if- else if statement to print the day of the week if (day== 0) System .out . printin day+ "- \t Sunday "); else if (day == 1) System . out . printin day + "- \t Monday else if (day --2) "); etc. fill the write code here else System. out.println( day + " Error - Invalid Day number"); Run the program several times and verify correctness of output Task 2: Add the following code by replacing if-else if statement in the previous Task code with a switch statement switch (day) case 0: Sv ( day + "- \t Sunday ); case 1:System.out.println ( day + "- \t Monday "); stem.out.printLn break; break; etc. fill the write code here default: System.out.println ("day + " Error - Invalid Day number"); Run the program several times to check if it is working. Add descriptive Output statements before each of the tasks "Output of Task 1: nested if else if statements", and "Output of Task2: switch statement), and verify that both approaches give identical results each time

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!