Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
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