Lab 9-4: Writing Methods that Return a Value In this lab, you complete a partial
ID: 3572816 • Letter: L
Question
Lab 9-4: Writing Methods that Return a Value
In this lab, you complete a partially written Java program that includes a method that returns a value. The program is a simple calculator that prompts the user for two numbers and an operator ( +, ?, *, /, or % ). The two numbers and the operator are passed to the method where the appropriate arithmetic operation is performed. The result is then returned to the main() method where the arithmetic operation and result are displayed. For example, if the user enters 3, 4, and *, the following is displayed:
3.00 * 4.00 = 12.00
students will design the logic for an application and submit a partially completed Java program and the pseudocode.
In this assignment, students will design the logic for the program that prompts the user for two numbers and an operator (+, -, *, /, or %). The two numbers and the operator are passed to the method where the appropriate arithmetic operation is performed. The result is then returned to the main() method where the arithmetic operation and result are displayed.
// Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
numberOne = Double.parseDouble(numberOneString);
numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
numberTwo = Double.parseDouble(numberTwoString);
operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
System.out.format("%.2f",numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
// Write performOperation method here.
} // End of Calculator class.
nput Enter an operator OK CancelExplanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
It is working fine.
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
numberOne = Double.parseDouble(numberOneString);
numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
numberTwo = Double.parseDouble(numberTwoString);
operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
result = performOperation(operation, numberOne, numberTwo);
System.out.format("%.2f",numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
private static double performOperation(String operation, double numberOne, double numberTwo){
switch(operation){
case "+":
return (numberOne+numberTwo);
case "-":
return (numberOne-numberTwo);
case "*":
return (numberOne*numberTwo);
case "/":
return (numberOne/numberTwo);
default:
return -9999;
}
}
} // End of Calculator class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.