java only please.... The switch statement in Java allows for multi-way branching
ID: 3857839 • Letter: J
Question
java only please....
The switch statement in Java allows for multi-way branching in an easy to read and easy to write format. The switch statement has many restrictions on when and how it can be used. The sytnax is as follows:
switch(variable or expression goes here)
{
case XXX: a possible value the variable may hold: statements to execute when the
variable holds the value
optional break;
case XXX: a possible value the variable may hold: statements to execute when the
variable holds the value
optional break;
case XXX: possible value the variable may hold: statements to execute when the
variable holds the value
optional break;
optional default: statements to execute if the variable did not match the value in
any of the cases
}
Here is an example to read and use as a reference.
char choice;
System.put.print( “Enter your choice, A, B, C, or D: “)
choice input.next( ).charAt(0);
switch (choice)
{
case ‘A’: System.out.println( “You chose A for Apple! “);
break;
case ‘B’: System.out.println( “You chose B for Banana!”);
break;
case ‘C’: System.out.println( “You chose C for Candy!”);
break;
case ‘D’: System.out.println(“You chose D for Donuts!”);
break;
default: System.out.println(“You did not pick a valid choice!”)
} //end of switch
The expression in the switch condition must be of type int or char ( or String with Java 8).
Each of the case labels (like ‘A’, ‘B’…) must be either hard coded or symbolic constants, they cannot be variables.
You can put as many statements as you need in each case.
The default case is optional.
After you have read the code above write a program that asks the user to enter 2 floating point numbers and a character * / % + or – Then use a switch statement to perform the operation on the two floating point numbers and print the answer after performing that operation.
If the user enters a 0 for a denominator (second input number) when choosing /, print a message stating that it is not possible to divide by 0.
Part B: Create a Java project. Name this project 5B
You must use a switch statement in this next program: Write a program that asks the user to enter two integers. The first is the month and the second integer is the day in the month. Then calculate and print how many days have passed in the year. If the user enters a date that cannot be in the month they specified, print an error message. If the user enters a month other than 1 to 12, print an error message.
Use the following piece of code to help you get started.
int totalDays = 0;
switch(month)
{ case 1: if(day > 31 || day < 0)
{
System.out.println(“Your day of month is invalid”;
}
else { totalDays = day; }
break;
Explanation / Answer
SwitchTester1.java
import java.util.Scanner;
public class SwitchTester1 {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
//asks the user to enter 2 floating point numbers and a character * / % + or –
System.out.println("Please enter 2 floating point numbers and a character * / % + or –");
Float fl1 = Float.valueOf(scan.nextLine());
Float fl2 = Float.valueOf(scan.nextLine());
String operator = scan.nextLine();
double sub=0;
//use a switch statement to perform the operation on the two floating point numbers
//and print the answer after performing that operation.
switch(operator){
case "*" : System.out.println(fl1 +" * "+ fl2+" = "+fl1*fl2);
break;
case "/" : if(fl2 == 0){
// If the user enters a 0 for a denominator (second input number) when choosing /,
// print a message stating that it is not possible to divide by 0.
System.out.println("not possible to divide by 0.");
}
else{
System.out.println(fl1 +" / "+ fl2+" = "+fl1/fl2);
}
break;
case "%" : System.out.println(fl1 +" % "+ fl2+" = "+fl1%fl2);
break;
case "+" : System.out.println(fl1 +" + "+ fl2+" = "+fl1+fl2);
break;
case "-" : sub= fl1.floatValue() - fl2.floatValue();
System.out.println(fl1 +" - "+ fl2+" = "+sub);
break;
default : System.out.println("Invalid option!");
}
}
}
Sample output:
Please enter 2 floating point numbers and a character * / % + or –
5.0
3.0
-
5.0 - 3.0 = 2.0
SwitchTester2.java
import java.util.Scanner;
public class SwitchTester2 {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
//asks the user to enter two integers.
//The first is the month and the second integer is the day in the month
System.out.println("Please enter month and date: ");
int month = Integer.valueOf(scan.nextLine());
int day = Integer.valueOf(scan.nextLine());
int totalDays = 0;
switch(month)
{
case 1: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = day; }
break;
case 2: if(day > 28 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 + day; }
break;
case 3: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 + day; }
break;
case 4: if(day > 30 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31+ day; }
break;
case 5: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30+ day; }
break;
case 6: if(day > 30 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 + 28 + 31 + 30 + 31 + day; }
break;
case 7: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30 + 31+ 30 + day; }
break;
case 8: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30 + 31+ 30 + 31+ day; }
break;
case 9: if(day > 30 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30 + 31+ 30 + 31 + 31 + day; }
break;
case 10: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30 + 31+ 30 + 31 + 31 + 30 + day; }
break;
case 11: if(day > 30 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays = 31 +28 +31 + 30 + 31+ 30 + 31 + 31 + 30 + 31 + day; }
break;
case 12: if(day > 31 || day < 0)
{
System.out.println("Your day of month is invalid");
}
else { totalDays =31 +28 +31 + 30 + 31+ 30 + 31 + 31 + 30 + 31 + 30 + +day; }
break;
}
System.out.println("Total days over : "+totalDays);
}
}
sample output:
Please enter month and date:
12
30
Total days over : 364
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.