Que: WAP to enter and display a string.
import [Link];
class Stri
{
public static void main(String args[])
{
String str;
Scanner in=new Scanner([Link]);/* create a object */
[Link]("Enter the string");
str=[Link]();
[Link]("entered string="+str);
}
}
Switch statement to select one of many code blocks to be executed. Each value is
called a case.
Flow Diagram of Switch-case:
1
Que: WAP to enter the day number and display its name using switch case.
import [Link];
class Day
{
public static void main(String args[])
{
int day;
Scanner in=new Scanner([Link]);
[Link]("Enter the day number");
day=[Link]();
switch (day)
{
case 1: [Link]("Monday");
break;
case 2: [Link]("Tuesday");
break;
case 3: [Link]("Wednesday");
break;
case 4: [Link]("Thursday");
break;
case 5: [Link]("Friday");
break;
case 6:[Link]("Saturday ");
break;
case 7: [Link]("Sunday ");
break;
default: [Link]("Invalid day");
break;
}
}
}
Que 1: WAP to create the basic calculator for two numbers using switch case.
import [Link];
class Calc
{
public static void main(String args[])
{
double a,b,sum,sub,multi,div;
int cal;
Scanner in = new Scanner([Link]);
[Link]("Enter first number:");
2
a = [Link]();
[Link]("Enter second number:");
b = [Link]();
[Link]("Enter the option number\n ");
[Link]("1 for Add\n 2 for Substract");
[Link]("\n 3 for Multiplication\n 4 for Division");
cal= [Link]();
switch(cal)
{
case 1: sum=a+b;
[Link]("Sum of a and b="+sum);
break;
case 2: sub=a-b;
[Link]("Substraction of a and b="+sub);
break;
case 3: multi=a*b;
[Link]("Multiplication of a and b="+multi);
break;
case 4: div=a/b;
[Link]("Division of a and b="+div);
break;
default:
[Link]("You have entered wrong option");
}
}
}