# Thread: Calculator Program HELP NEEDED FAST! Homework assignment

1. Member Join Date
Mar 2009
Posts
31
Rep Power
0

## Calculator Program HELP NEEDED FAST! Homework assignment

I have a homework assignment in which i must create a four function calculator. I must call the methods addNumbers, subtractNumbers, divideNumbers, and multiplyNumbers. I must have some syntax wrong because the methods that are declared at the bottom of the code are not assigning the operation specified to result. All help is appreciated!!!
This is my code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package TimothyFarmer;
import java.util.Scanner; //program uses class Scanner

/**
*
* @author Tech
*/
public class TimothyFarmer {

Scanner input = new Scanner ( System.in );

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
int choice=0;
float num1=1;
float num2=1;
float result=1;

Scanner input = new Scanner( System.in );

System.out.println( "Enter first number:" );
num1 = input.nextFloat();

System.out.println( "Enter second number" );
num2 = input.nextFloat();

while (choice != 5){ //starts while loop
System.out.println ( "1. Addition\n2. Subtraction\n" +
choice = input.nextInt();//ask user to input integer for choice

if ( choice == 1 ){ //if choice is 1
System.out.println ("Number 1 is:" + num1);
System.out.println ("Number 2 is:" + num2);

System.out.println ("Result is:" + result);
}
if ( choice == 2 ){ //if choice is 2
subtractNumbers(num1, num2, result); //call method subtractNumbers
System.out.println("Number 1:" + num1 );
System.out.println("Number 2:" + num2 );
System.out.println ("Result is:" + result);
}
if (choice == 3 ){ //if choice is 3
multiplyNumbers(num1 , num2, result);// multiply numbers
System.out.println("Number 1:" + num1 );
System.out.println("Number 2:" + num2 );
System.out.println ("Result is:" + result);
}
if (choice == 4){//if choice is 4
divideNumbers(num1, num2, result);//call method divideNumbers
System.out.println("Number 1:" + num1 );
System.out.println("Number 2:" + num2 );
System.out.println ("Result is:" + result);
}
//end while loop

} //end while loop

if (choice == 5){//if choice is 5
System.out.println("You have chosen to exit the program. Bye!");
}

}//end class main
private static float addNumbers(float num1,float num2,float result)
{

result = num1 + num2;
return result;
}

public static float subtractNumbers(float num1,float num2,float result){

return result = num1 - num2;
}

public static float multiplyNumbers(float num1,float num2,float result){

return result = num1 * num2;
}
public static float divideNumbers(float num1,float num2, float result){

return result = num1 / num2;
}

}
Last edited by SteroidalPsycho; 03-05-2009 at 03:35 AM.  Reply With Quote

2. Member Join Date
Feb 2009
Posts
46
Rep Power
0

## My only first point is why are you passing 'float result' into all the static methods? there is no need.
IE: public static float divideNumbers(float num1,float num2, float result){

return result = num1 / num2;
}

turn it into

public static float divideNumbers(float num1,float num2){
float result = num 1 / num2;
return result;
}  Reply With Quote

3. Member Join Date
Feb 2009
Posts
46
Rep Power
0

## Ahh, Your passing a float result of '1' into each method (not sure why). your return isn't returning correctly as its the same name - i will try test it in a minute.  Reply With Quote

4. Member Join Date
Feb 2009
Posts
46
Rep Power
0

## Ah sorry, I really should have looked better.

you're not re-assigning 'result' as anything, result never changes it's always '1' - whilst you are returning it its never re-assigning it.

result = subtractNumbers(num1, num2, result); //call method subtractNumbers

This should fix it all up (ignore the above comments) its as simple as adding 'result =' to your method calls :D

please note however, you likely shouldn't be passing 'result' into your method call, you really should just be passing it num1,num2, and then it returns a float result back to you.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•