Question: Java methods: Calculator Prompt the user for the first value, then prompt the operation to be performed (+, -, *, /), then prompt for the
Java methods: Calculator
Prompt the user for the first value, then prompt the operation to be performed (+, -, *, /), then prompt for the second value, then show the result, repeat the process until the letter q is entered. Create five methods addition(), subtraction(), multiplication(), division() and inputValueCheck().
What I have so far:
import java.util.Scanner; public class calculatorLab{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); float var1, var2, result; char op; do{ System.out.println("Please enter the first number"); var1 = scan.nextFloat(); System.out.println("Please enter the next number"); var2 = scan.nextFloat(); System.out.println("Which operation would you like to perfor$ op = scan.next().charAt(0); switch (op){ case '+': result = addition(var1, var2); break; case '-': result = subtraction(var1, var2); break; case '*': result = multiplication(var1, var2); break; case '/': result = division(var1, var2); break; }//closing switch }while(op != 'Q'); }//close main
private static float addition(float var1, float var2){ return var1+var2; }// closing addition brace private static float addition(float var1, float var2){ return var1+var2; }//closing addition brace
private static float subtraction(float var1, float var2)
return var1-var2;
}// closing subtraction brace private static float multiplication(float var1, float var2){ return var1*var2; }// closing multiplication brace private static float division(float var1, float var2){ return var1/var2; }// closing divison method brace
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
