Hello, So this is my assignment: Question: Lab 9-4: writing methods that return
ID: 3575289 • Letter: H
Question
Hello, So this is my assignment:
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.
Here is what i have so far :
package calculator;
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(numberOne,numberTwo,operation);
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.
public static double performOperation(double numberOne, double numberTwo, String operation)
{
double result = 4;
return result;
end of calculator class
I used a case and return method after the operation method that i later omitted from my code because i was required to use an IF statement to calculate the math for me, but I do not know how to do this.
can you please help me finish my assignment?
Explanation / Answer
package calculator;
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(numberOne,numberTwo,operation);
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.
public static double performOperation(double numberOne, double numberTwo, String operation)
{
double result =0;
if(operation.equals("+")){ //check if the operator is +
result = numberOne + numberTwo;
}
else if (operation.equals("-") ){ //check if the operator is -
result = numberOne - numberTwo;
}
else if (operation.equals("*") ){ //check if the operator is *
result = numberOne * numberTwo;
}
else if (operation.equals("/")){ //check if the operator is /
result = numberOne / numberTwo;
}
else if (operation.equals("%")){ //check if the operator is /
result = numberOne % numberTwo;
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.