Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default 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 03:35 AM.

  2. #2
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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;
    }

  3. #3
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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.

  4. #4
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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

  1. Help needed for a 40 mark homework due in 1 hour .. plz
    By q8ysurgeon in forum New To Java
    Replies: 5
    Last Post: 01-09-2009, 05:07 PM
  2. Help needed for a 40 mark homework due in 1 hour .. plz
    By q8ysurgeon in forum Advanced Java
    Replies: 3
    Last Post: 01-09-2009, 04:09 AM
  3. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM
  4. HELP FAST!!----Interest Calculator
    By Coop33 in forum New To Java
    Replies: 8
    Last Post: 10-15-2008, 01:44 PM
  5. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM

Posting Permissions

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