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