2. Write a program, using a switch statement, that prompts the user to enter the
ID: 3880814 • Letter: 2
Question
2. Write a program, using a switch statement, that prompts the user to enter the day of the week, and prints out their schedule according to the following, Note: You may wish to use fall-through. Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45 Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15 Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45 Thursday: PHYS 2 at 12:10 Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45 3. Problem 1 on page 95 of textbook. 4. Problem 4 on page 96 of textbook.Explanation / Answer
2) The idea of fall-through is to execute all the case statements that follows the case we execute (from the case that satisfies the switch condition till it encounters a break or end of switch statement '}' ). This can be done by not including "break" statement from the case that satisfies the switch condition till the case we want to fall-through(i.e., not include "break" in all the cases we want to fall-through and execute).
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchDemo {
public static void main(String[] args) throws IOException {
String dayOfWeek = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the day of the week");
dayOfWeek = br.readLine();
switch(dayOfWeek.toLowerCase()) {
case "monday":
System.out.println("Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
case "tuesday":
System.out.println("Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15");
case "wednesday":
System.out.println("Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
case "thursday":
System.out.println("Thursday: PHYS 2 at 12:10");
case "friday":
System.out.println("Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
}
}
}
1) Input : Monday
Output:
Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15
Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Thursday: PHYS 2 at 12:10
Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Explanation:
Here, the control will go inside case “Monday” and execute it. Since, it doesn’t encounter a single break statement in all the cases that follows it, all the cases will be executed.
2) Input :Tuesday
Output:
Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15
Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Thursday: PHYS 2 at 12:10
Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Explanation:
Here, the control will go inside case “Tuesday” and execute it. Since, it doesn’t encounter a single break statement in switch, it will execute all the cases that follows the case “Tuesday”.
3) Input : Wednesday
Output:
Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Thursday: PHYS 2 at 12:10
Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
We can also do the given problem statement as follows.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchDemo {
public static void main(String[] args) throws IOException {
String dayOfWeek = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the day of the week");
dayOfWeek = br.readLine();
switch(dayOfWeek.toLowerCase()) {
case "monday":
case "tuesday":
case "wednesday":
case "thursday":
case "friday":
System.out.println("Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
System.out.println("Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15");
System.out.println("Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
System.out.println("Thursday: PHYS 2 at 12:10");
System.out.println("Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45");
}
}
}
1) Input : Monday
Output:
Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15
Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Thursday: PHYS 2 at 12:10
Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Explanation:
This is a little bit different from the previous one. No matter what you enter from “Monday” to “Friday”, it will print all days schedule. What is happening here is case “Monday”,”Tuesday”,”Wednesday” and “Thursday” doesn’t have a body and break statement. Only case “Friday” has a body that prints the schedule. So, no matter what day you enter from “Monday” to “Friday”, it will go through all the cases that follows the current case and then prints the schedule that is in case “Friday”.
2) Input :Tuesday
Output:
Monday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Tuesday: PHYS 2 at 12:10, CSCI 10 lab at 2:15
Wednesday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Thursday: PHYS 2 at 12:10
Friday: CSCI 10 at 1:00, PHIL 11A at 2:15, MATH 12 at 4:45
Main purpose of break in switch statement : break out of the switch after the particular case executes.
Example 1:
id=Integer.parseInt(br.readLine());
switch(id) {
case 1:
System.out.println(“Hello”);
break;
case 2:
System.out.println(“World”);
break;
}
Input: 1
Output: Hello
Here the id we inputted is 1. So it will enter case 1 in switch statement and execute whatever is inside it. And as soon as it encounters a break, the control will come out of the switch statement.
Example 2:
id=Integer.parseInt(br.readLine());
switch(id) {
case 1:
System.out.println(“Hello”);
case 2:
System.out.println(“World”);
break;
case 3:
System.out.println(“How are you?”);
}
Input: 1
Output:
Hello
World
Here id =1. So, the control will enter case 1 and execute whatever is inside it. After finishing execution, it doesn’t have a break statement. So, the control will go inside the next case statement that follows. Here after case 1, the control will go to case 2 and execute it. On executing the break statement in case 2, the control will come out of the switch case and not go to case 3.
The control will come out of the switch case when it encounters a break statement or end of loop’}’.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.