90 lines
3.3 KiB
Java
90 lines
3.3 KiB
Java
package Week10_Lab;
|
|
|
|
import java.util.Scanner;
|
|
|
|
public class calculator {
|
|
private int input1;
|
|
private int input2;
|
|
private int result;
|
|
public calculator(int input1, int input2){
|
|
this.input1 = input1;
|
|
this.input2 = input2;
|
|
}
|
|
public calculator(){
|
|
this(0,0);
|
|
}
|
|
public int addr(int input1, int input2){
|
|
this.result = input1 + input2;
|
|
return this.result;
|
|
}
|
|
public int subtractor(int input1, int input2){
|
|
this.result = input1 - input2;
|
|
return this.result;
|
|
}
|
|
public int multiplier(int input1, int input2){
|
|
this.result = input1 * input2;
|
|
return this.result;
|
|
}
|
|
public int divider(int input1, int input2){
|
|
this.result = input1 / input2;
|
|
return this.result;
|
|
}
|
|
public void clear(){
|
|
this.result = 0;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
calculator c = new calculator();
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
do {
|
|
System.out.println("Enter 1 for addition");
|
|
System.out.println("Enter 2 for subtraction");
|
|
System.out.println("Enter 3 for multiplication");
|
|
System.out.println("Enter 4 for division");
|
|
System.out.println("Enter 5 to clear");
|
|
System.out.println("Enter 6 to exit");
|
|
System.out.println("Enter your choice: ");
|
|
int choice = sc.nextInt();
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
System.out.println("Enter the first number: ");
|
|
int input1 = sc.nextInt();
|
|
System.out.println("Enter the second number: ");
|
|
int input2 = sc.nextInt();
|
|
System.out.println("The Addition of " + input1 + " and " + input2 + " is " + c.addr(input1, input2));
|
|
break;
|
|
case 2:
|
|
System.out.println("Enter the first number: ");
|
|
input1 = sc.nextInt();
|
|
System.out.println("Enter the second number: ");
|
|
input2 = sc.nextInt();
|
|
System.out.println("The Subtraction of " + input1 + " and " + input2 + " is " + c.subtractor(input1, input2));
|
|
break;
|
|
case 3:
|
|
System.out.println("Enter the first number: ");
|
|
input1 = sc.nextInt();
|
|
System.out.println("Enter the second number: ");
|
|
input2 = sc.nextInt();
|
|
System.out.println("The Multiplication of " + input1 + " and " + input2 + " is " + c.multiplier(input1, input2));
|
|
break;
|
|
case 4:
|
|
System.out.println("Enter the first number: ");
|
|
input1 = sc.nextInt();
|
|
System.out.println("Enter the second number: ");
|
|
input2 = sc.nextInt();
|
|
System.out.println("The Division of " + input1 + " and " + input2 + " is " + c.divider(input1, input2));
|
|
break;
|
|
case 5:
|
|
c.clear();
|
|
System.out.println("The result is cleared");
|
|
break;
|
|
default:
|
|
System.out.println("Invalid choice");
|
|
break;
|
|
}
|
|
} while (true);
|
|
}
|
|
}
|