Hello, I am beginner Java programmer and I would greatly appreciate help. The sections with which I need help are pointed out by comment lines. I am having a lot of difficulty with methods and I do get the basic function of them but I don't know how to implement them.
Thank you
import java.util.Scanner;
public class Calculator {
double theAnswer;
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.mainLoop();
}
private void mainLoop() {
System.out.println("Welcome to the calculator. Enter two numbers, then an operator " +
"to be used. \n Entering an operator of x exits the program.");
while(true) {
System.out.println();
System.out.println("Enter the two integers to be used: ");
double operand1 = 0;
double operand2 = 0;
char operator=' ';
//write method such as readOneOperand() to just read a number in input and return it.
private double multiply(double operand1, double operand2)
operand1 = readOneOperand();
operand2 = readOneOperand();
//Now must read operator. The method readOperator() has not been written.
//read a character typed on the keyboard and return it in output,
//so that it is stored in the variable operator.
operator = readOperator();
//now there are two numbers in operand1 and operand2
//and a character in operator. The character is one of +, -, *, /, x if(operator == '*')
//if this is a multiplication
theAnswer = multiply(operand1,operand2);//must write the code for the method multiply
//write the code for the other operations and for the exit.
}
}
private double readOneOperand() {
Scanner keyboard = new Scanner(System.in);
return keyboard.nextInt();
}
private char readOperator() {
System.out.println("Not yet implemented");
return ' ';
}
private double multiply(double operand1, double operand2) {
System.out.println("Net yet implemented");
return ' ';
}
}