Question: Goal: Create and execute a switch statement and an if-else-if operation in Java, using the three simple tools: (1) a text editor, (2) the Java
Goal: Create and execute a switch statement and an if-else-if operation in Java, using the three simple tools: (1) a text editor, (2) the Java compiler and (3) the Java interpreter.
This lab focuses on developing a simple program on your own using the switch statement and modifying the program to use an if-else-if statement(s).
Programming Activity: THEORY
The general format of the switchstatement is given below.
if (expression_1)
{
statement;
statement;
etc.
}
else if (expression_2)
{
statement;
statement;
etc.
}
Insert as many else if clauses as necessary
else
{
statement;
statement;
etc.
}
The general format of the switch statement is given below.
switch (SwitchExpression)
{
case CaseExpression:
// place one or more statements here
break;
case CaseExpression:
// place one or more statements here
break;
// case statements may be repeated
//as many times as necessary
default:
// place one or more statements here
}
PROGRAM DESCRIPTION
In this lab, you will write a program called DaysWeek.java. Write a program to prompt a letter from the user. Use a decision structure on the letter to decide the day of the week that begins with the letter. Recall: Logical operators, use the keyword break in the switch statement after each decision. Use the steps given below as a guide to write your program. Steps:-Create and import your Scanner object.-Create your class header and main method.-Use a System.out.printlnto display the following greeting to the user. Select a day of the week. Enter the first letter: M or m for Monday, T or t for Tuesday and Thursday, etc.
See sample output below. -Prompt the user for a letter. Scanner does not have a next Char()method so youll need to get the letter as a String -If the user enters more than one letter, use a String method to filter the first letter entered(refer: PetFood.java).-Create if else if statements testing the variable containing the letter.-Of the 7 days in a week, only 5letters are used as the first letter of the week (M, T, W, F, S).-For the 5 letters, both upper and lowercase letters must be taken as valid input. You will need a Logical Operator to tests multiple conditions.-Depending on the letter entered, output the day or days that begin with that letter. If an s or Sis entered your code should display The days that begin with an S are Saturday and Sunday.
-The else part should display the message, There is no day that begins with that letter. Run your code testing each of the letters: M t F and v
Sample Output:
C:\Users\AH>java Days Week Select a day of the week.
Enter the first letter: M or m for Monday, T or t for Tuesday and Thursday, etc.
M
The day that begins with m or M is Monday.
C:\Users\AH>java DaysWeek
Select a day of the week.
Enter the first letter: M or m for Monday, T or t for Tuesday and Thursday, etc.
t
The days that begin with the letter t or T are Tuesday and Thursday.
C:\Users\AH>java DaysWeekSelect a day of the week.
Enter the first letter: M or m for Monday, T or t for Tuesday and Thursday, etc.
F
The day that begins with f or F is Friday.
C:\Users\AH>java DaysWeekSelect a day of the week.
Enter the first letter: M or m for Monday, T or t for Tuesday and Thursday, etc.
v
There is no day that begins with this letter.
C:\Users\AH>Return to the code:-Comment out the if else if statements. -Use a switch statement to recreate the same decision structure as the if else if statement you commented out.-Remember to account for both upper and lowercase letters for the 5 letters used. -The default of the switch should output There is no day that begins with this letter.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
