Sunday, April 19, 2015

Simple Calculator in java for beginners

import java.util.Scanner;

public class CH1 {

static int a;
static int b, num;
int add, sub, mult, div;

  public void sum() {
add = a + b;
System.out.println("Result: "+add);

}
  public void sub() {
System.out.println("Result: "+(a - b));
}
  public void mult() {
System.out.println("Result: "+(a * b));
}

public void div() {

System.out.println("Result: "+(a / b));
}
  public void get() {
Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers for operation");
System.out.println("a= ");
a = in.nextInt();
System.out.println("b= ");
b = in.nextInt();
}
  public void total() {
Scanner in = new Scanner(System.in);
System.out.println("What u want to do");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION");
System.out.println("3. MULTIPLICATION");
System.out.println("4. DIVISION");
num = in.nextInt();

switch (num) {
case 1:
get();
sum();

break;
case 2:
get();
sub();

break;
case 3:
get();
mult();

break;
case 4:
get();
div();
   break;

default:
System.out.println("You enter wrong number");
break;
}
lastcall();
}
 public void lastcall() {
System.out.println("You want to exit Y/N");
Scanner s = new Scanner(System.in);
String y = s.nextLine();
if (y.equals("n") || y.equals("N")) {
total();
} else if (y.equals("y") || y.equals("Y")) {
System.out.println("Thank u");
}
}
public static void main(String[] args) {
  CH1 call = new CH1();
call.total();
}
}

5 comments: