1. WAP in Java language to create calculator using switch case.
import [Link].*;
class Calculator
{ Page | 30
public static void main(String args[]) throws IOException
{
int a,b,c,choice;
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
while(true)
{
[Link]("WELCOME TO SIMPLE CALCULATOR");
[Link]("Enter the first number");
a= [Link]([Link]());
[Link]("Enter the second number");
b= [Link]([Link]());
[Link]("**********Enter your choice***********");
[Link]("1 for addition");
[Link]("2 for substration");
[Link]("3 for multiplication");
[Link]("4 for division");
[Link]("0 for exit");
choice=[Link]([Link]());
switch(choice)
{
case 1:
c=a+b;
[Link]("Sum is:"+c);
break;
case 2:
c=a-b;
[Link]("Subs is:"+c);
break;
case 3:
c=a*b;
[Link]("Mul is:"+c);
break;
case 4:
c=a/b;
[Link]("div is:"+c); Page | 31
break;
case 0:
[Link]("Exiting the calculator. Goodbye!");
[Link](0);
break;
default:
[Link]("Invalid choice");
}
}
}
}
Save: [Link]
Compile: [Link]
Run : Calculator
2. Java Program to Display the ATM Transaction.
import [Link];
public class Atm
{
public static void main(String args[] )
{
int balance = 5000, withdraw, deposit;
Scanner s = new Scanner([Link]);
while(true)
{
[Link]("Welcome to Sonisoft Automated Teller Machine");
[Link]("Choose 1 for Withdraw");
[Link]("Choose 2 for Deposit");
[Link]("Choose 3 for Check Balance");
[Link]("Choose 4 for EXIT");
[Link]("Choose the operation you want to perform:");
int n = [Link]();
switch(n)
{
case 1: Page | 32
[Link]("Enter money to be withdrawn:");
withdraw = [Link]();
if(balance >= withdraw)
{
balance = balance - withdraw;
[Link]("Please collect your money");
}
else
{
[Link]("Insufficient Balance");
}
[Link]("");
break;
case 2:
[Link]("Enter money to be deposited:");
deposit = [Link]();
balance = balance + deposit;
[Link]("Your Money has been successfully depsited");
[Link]("");
break;
case 3:
[Link]("Balance : "+balance);
[Link]("");
break;
case 4:
[Link](0);
}
}
}
}
Save: [Link]
Compile: [Link]
Run : Atm
3. Calculate Total Electricity Bill in Java.
import [Link];
class ComputeElectricityBill
{
Page | 33
public static void main(String[] args)
{
int unit;
float amt, surcharge, bill_amt;
Scanner input = new Scanner([Link]);
[Link]("Enter The Electricity Unit : ");
unit = [Link]();
if(unit<=50)
{
amt = unit*2;
}
else if(unit<=150)
{
amt = unit*3;
}
else if(unit<=250)
{
amt = unit*4;
}
else
{
amt = unit*5.50f;
}
surcharge = amt*0.50f;
bill_amt = amt+surcharge;
[Link]("Total Electricity Bill : "+bill_amt);
}
}
Save: [Link]
Compile: [Link]
Run : ComputeElectricityBill