I am new in Java. I would like to test this calculator in the main method. What is the most efficient way you could test this?
package BeginnerLevel;
import java.util.Scanner;
public class Calculator {
This is the constructor:
public Calculator(){
while (true){
System.out.println("Personal Calculator\n"+
"Addition---> 1 \n"+"Subtraction---> 2\n"+"Multiplication---> 3\n"+"Division---> 4\n"+"Exit--->0");
Scanner input = new Scanner(System.in);
int UsrIn= input.nextInt();
input.close();
switch (UsrIn){
case 1:
Addition();
break;
case 2:
Subtraction();
break;
case 3:
Multiplication();
break;
case 4:
Division();
break;
case 0:
break;
}
}
}
These are the methods that are called in the switch case.
Addition method:
private void Addition(){
Scanner input= new Scanner(System.in);
double total=0;
double firstnum=0;
double secondnum=0;
System.out.println("Enter Fist number: ");
firstnum= input.nextDouble();
System.out.println("Enter Fist number: ");
secondnum=input.nextDouble();
input.close();
total= firstnum + secondnum;
System.out.println("The result of Addition for "+ firstnum +" + "+secondnum+" is "+total);
}
Subtraction method:
private void Subtraction(){
Scanner input= new Scanner(System.in);
double total=0;
double first=0;
double second=0;
System.out.println("Enter First number ");
first=input.nextDouble();
System.out.println("Enter Second number ");
second=input.nextDouble();
total= first-second;
System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);
input.close();
}
Multiplication method:
private void Multiplication(){
Scanner input= new Scanner(System.in);
double total=0;
double first=0;
double second=0;
System.out.println("Enter First number ");
first=input.nextDouble();
System.out.println("Enter Second number ");
second=input.nextDouble();
total= first*second;
System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);
input.close();
}
Division Method:
private void Division(){
Scanner input= new Scanner(System.in);
double total=0;
double first=0;
double second=0;
System.out.println("Enter First number ");
first=input.nextDouble();
System.out.println("Enter Second number ");
second=input.nextDouble();
total= first*second;
System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);
input.close();
}
}
Any recommendation is appreciated !
Aucun commentaire:
Enregistrer un commentaire