Results 1 to 4 of 4
- 03-05-2009, 04:33 AM #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" +
"3. Multiplication\n4. Division\n5. Quit");//displays menu
choice = input.nextInt();//ask user to input integer for choice
if ( choice == 1 ){ //if choice is 1
addNumbers(num1, num2, result); //call method addNumbers
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 04:35 AM.
- 03-05-2009, 04:49 AM #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;
}
- 03-05-2009, 04:52 AM #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.
- 03-05-2009, 05:02 AM #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.
On your calls to your methods try this.
result = addNumbers(num1, num2, result); //call method addNumbers
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.
Similar Threads
-
Help needed for a 40 mark homework due in 1 hour .. plz
By q8ysurgeon in forum New To JavaReplies: 5Last Post: 01-09-2009, 06:07 PM -
Help needed for a 40 mark homework due in 1 hour .. plz
By q8ysurgeon in forum Advanced JavaReplies: 3Last Post: 01-09-2009, 05:09 AM -
unreachable statement - Java calculator program
By V2001Gordon in forum New To JavaReplies: 3Last Post: 12-13-2008, 01:57 AM -
HELP FAST!!----Interest Calculator
By Coop33 in forum New To JavaReplies: 8Last Post: 10-15-2008, 02:44 PM -
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:14 AM
Bookmarks