Java simple calculator. The errors are: could not find or load main class, and i
ID: 3808794 • Letter: J
Question
Java simple calculator. The errors are: could not find or load main class, and illegal start of expressions for the if statement, and the if else statements. Can you steer me in the right direction, and explain my errors? In this lab the only code I wrote is the //call, and the //write portions. the rest was given.
// Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation
import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number: ");
numberOneString = input.nextLine();
numberOne = Double.parseDouble(numberOneString);
System.out.println("Enter the second number: ");
numberTwoString = input.nextLine();
numberTwo = Double.parseDouble(numberTwoString);
System.out.println("Enter an operator (+.-.*,/,%): ");
operation = input.nextLine();
// 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.
private static double performOperation(double num1, double num2, string operation)
{
if(operation == (+))
{
result = num1 + num2;
}
else if(operation == (-))
{
result = num1 - num2;
}
else if(operation == (*))
{
result = num1 * num2;
}
else if(operation == (/))
{
result = num1 / num2;
}
return result;
}
} // End of Calculator class.
Explanation / Answer
HI, Please find my coorected code.
It is running fine.Please let me know in case of any issue.
//Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
//Input: Interactive.
//Output: Result of arithmetic operation
import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number: ");
numberOneString = input.nextLine();
numberOne = Double.parseDouble(numberOneString);
System.out.println("Enter the second number: ");
numberTwoString = input.nextLine();
numberTwo = Double.parseDouble(numberTwoString);
System.out.println("Enter an operator (+.-.*,/,%): ");
operation = input.nextLine();
//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.
private static double performOperation(double num1, double num2, String operation)
{
double result;
if(operation.equals("+"))
{
result = num1 + num2;
}
else if(operation.equals("-"))
{
result = num1 - num2;
}
else if(operation.equals("*"))
{
result = num1 * num2;
}
else if(operation.equals("/"))
{
result = num1 / num2;
}
else if(operation.equals("%"))
{
result = num1 % num2;
}
else{
System.out.println("Invalid operation");
result = 0.0;
}
return result;
}
} // End of Calculator class
/*
Sample run:
Enter the first number:
5
Enter the second number:
3
Enter an operator (+.-.*,/,%):
%
5.00 % 3.00 = 2.00
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.