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(); 

	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("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 ' '; 
5 Years
Discussion Span
Last Post by Rameshwar Soni

I think you are unnecessarily making your code tough

System.out.println("Enter the two integers to be used: ");

Just get these two numbers.

double op1=scanner.nextDouble();

Similarly the second operand and then after asking the operator from the user check the operator in if statement whether it is '+' or "*" etc and do the following


That's it. But if it is a compulsion to make a method for each operation like addition, multiplication etc then in the if statement you can call that method.
I don't know why there is a need for readOneOperand(), readOperator() method. Yes they can be used but by unnecessarily make confusion for yourself.

Edited by Rameshwar Soni: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.