Making a Simple Calculator with Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinNeedsJavaHelp
    New Member
    • Feb 2007
    • 22

    Making a Simple Calculator with Java

    I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5

    The program needs to compute this and display the result as "The result is: 10"

    I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it.

    Thanks

    This is what I have so far:

    Code:
     import java.io.*; 
    import java.util.*;
    public class Calculator
    {
    public static void main(String[] args)
    throws java.io.IOException
    {
    String I;
    double Input;
    double Result;
    double Interest;
     
    InputStreamReader isr = new InputStreamReader(System.in);
     
    BufferedReader br = new BufferedReader(isr);
     
    System.out.println("What do you want to compute? ");
    I = br.readLine();
     
    Interest = Double.parseDouble(I);
     
    Result = Math(Interest);
     
    System.out.println("The result is: " +Result);
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by colinNeedsJavaH elp
    I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5

    The program needs to compute this and display the result as "The result is: 10"

    I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it.

    Thanks

    This is what I have so far:

    import java.io.*;
    import java.util.*;
    public class Calculator
    {
    public static void main(String[] args)
    throws java.io.IOExcep tion
    {
    String I;
    double Input;
    double Result;
    double Interest;

    InputStreamRead er isr = new InputStreamRead er(System.in);

    BufferedReader br = new BufferedReader( isr);

    System.out.prin tln("What do you want to compute? ");
    I = br.readLine();

    Interest = Double.parseDou ble(I);

    Result = Math(Interest);

    System.out.prin tln("The result is: " +Result);
    }
    }
    What operations do you want to be able to perform on your calculator? Just the * or other operations as well. The code you have written is not doing any multiplication so you can't expect it to give you the correct answer yet. And colin, next time you post code, please wrap it around code tags.

    Comment

    • colinNeedsJavaHelp
      New Member
      • Feb 2007
      • 22

      #3
      I need it to perform addition, subtraction, multiplication, and division.

      I think there is a math function that will just calculate whatever input, right? I just cannot find it in my book.

      thx,
      colin

      Comment

      • colinNeedsJavaHelp
        New Member
        • Feb 2007
        • 22

        #4
        Actually I might need to use the if command i just feel stuck right now

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by colinNeedsJavaH elp
          I need it to perform addition, subtraction, multiplication, and division.

          I think there is a math function that will just calculate whatever input, right? I just cannot find it in my book.

          thx,
          colin
          There is no math function which will just figure it out. That is the function that you want to write.

          You will need to be able to split the input String into the operands and the operation first, then use a switch to select which operation to do. Do you think you can write code to split the input String into the operands and operations first?

          Comment

          • colinNeedsJavaHelp
            New Member
            • Feb 2007
            • 22

            #6
            Originally posted by r035198x
            There is no math function which will just figure it out. That is the function that you want to write.

            You will need to be able to split the input String into the operands and the operation first, then use a switch to select which operation to do. Do you think you can write code to split the input String into the operands and operations first?

            I am not really sure how to do that. I would have to make it perfom seperate calculations based on what the user inputs. How would I do that? I am a very novice programmer.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by colinNeedsJavaH elp
              I am not really sure how to do that. I would have to make it perfom seperate calculations based on what the user inputs. How would I do that? I am a very novice programmer.
              The operation to perform depends on the operator that has been supplied. If you don't manage to separate the operator from the operands you won't be able to finish this. Have a look at the methods of the String class and chose the methods you think will be required to split a String like "4+5" into three strings, "4", "+", and "5".

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                And of course.... it all depends on how simple youmean by simple computation. If you always know two variable and an operand all is goos....if you need to be able to do (slightly) more complex things, look up "Reverse Polish Notation". If you can set up a stack (or two ) in RPN you can do more complex math (though still using the most basic operations)

                Comment

                • colinNeedsJavaHelp
                  New Member
                  • Feb 2007
                  • 22

                  #9
                  I found out I need to use the If command.

                  Here are my instructions
                  It only expects primitive arithmetic expressions involving two operands connected by one of the operators. +,-,*, /

                  It only expects non-negative numbers in the input

                  It assumes that all numbers are real numbers

                  It assumes that the operands and operators in the user-input expressions are separated by spaces. for example 20 - 15 instead of 20-15


                  I do not know what to do for the if statements

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    As r0 said, you should focus first on separating the input into the numbers and the operator.

                    For now, let's assume you have the user input. Both operators are in integer variables (call them num1 and num2), and the operator is held in a String variable (call it oper). If the user entered a + sign, then oper's contents are "+", and you want to add num1 and num2 for the result. You can use the .equals() method on oper to see if it equals "+", "-", "*", or "/". These will be your if...else branches.

                    Comment

                    • colinNeedsJavaHelp
                      New Member
                      • Feb 2007
                      • 22

                      #11
                      Code:
                      import java.io.*;
                      import java.util.*;
                      public class Calculator
                      {
                      public static void main(String[] args)
                      throws java.io.IOException
                      {
                      float num1;
                      float oper;
                      float num2;
                      double result;
                      
                      InputStreamReader isr = new InputStreamReader(System.in);
                      
                      BufferedReader br = new BufferedReader(isr);
                      
                      System.out.println("What do you want to compute? ");
                      num1 = br.readLine();
                      oper = br.readLine();
                      num2 = br.readLine();
                      
                      if (oper= "*") {result =  num1*num2;}
                      if(oper = "+") { result = num1 + num2; }
                      if(oper = "-") { result = num1 - num2; }
                      
                      if(oper = "/") { result = num1 / num2;}
                      
                      System.out.println("The result is: " +result);
                       }
                      }
                      What am I doing wrong??

                      Comment

                      • nmadct
                        Recognized Expert New Member
                        • Jan 2007
                        • 83

                        #12
                        Originally posted by colinNeedsJavaH elp
                        What am I doing wrong??
                        You're actually on the right track. Some things I noticed:

                        - You are reading a whole line of text for each input, but that's not the input that you said you're expecting. Since you can expect that each number and operator is separated by a space, I would try the split method of the String class -- read a line an break it up into tokens (token=meaningf ul unit of text) that are divided by space characters. Like this: inputLine.split (" "). That's just one way to do it, but I think in this case it's a very convenient way.
                        - You are trying to do math with string variables. You need to convert them to numbers first. You were doing this in your first version, with this expression: Double.parseDou ble(I)

                        A question, do you need to honor the standard algebraic order of operations, or are you just computing from left to right? That is, which answer to you expect to get here? "3 + 4 * 7 = 31" or "3 + 4 * 7 = 84"? The second one will be easier to do because it doesn't involve any trickery to make sure the right operation is evaluated first.

                        Comment

                        • colinNeedsJavaHelp
                          New Member
                          • Feb 2007
                          • 22

                          #13
                          I do not understand how you got 84. But I dont have toworry about that. I just need to be able to perform one thing at a time. For example 6 + 5 or 10 * 5 instead of multiple operations in one command. It is a very simplecalculato r

                          Comment

                          • nmadct
                            Recognized Expert New Member
                            • Jan 2007
                            • 83

                            #14
                            Originally posted by colinNeedsJavaH elp
                            I do not understand how you got 84. But I dont have toworry about that. I just need to be able to perform one thing at a time. For example 6 + 5 or 10 * 5 instead of multiple operations in one command. It is a very simplecalculato r
                            Yeah, ah... you don't get how I got 84 cuz I did it wrong, hehe. It should have been 49, sorry.

                            Anyway, if the requirement is just simple expressions like that, then the program you have is close to working, assuming you straighten out the issues I mentioned in my last post.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by colinNeedsJavaH elp
                              Code:
                              import java.io.*;
                              import java.util.*;
                              public class Calculator
                              {
                              public static void main(String[] args)
                              throws java.io.IOException
                              {
                              float num1;
                              float oper;
                              float num2;
                              double result;
                               
                              InputStreamReader isr = new InputStreamReader(System.in);
                               
                              BufferedReader br = new BufferedReader(isr);
                               
                              System.out.println("What do you want to compute? ");
                              num1 = br.readLine();
                              oper = br.readLine();
                              num2 = br.readLine();
                               
                              if (oper= "*") {result = num1*num2;}
                              if(oper = "+") { result = num1 + num2; }
                              if(oper = "-") { result = num1 - num2; }
                               
                              if(oper = "/") { result = num1 / num2;}
                               
                              System.out.println("The result is: " +result);
                              }
                              }
                              What am I doing wrong??
                              nmadct has given a lot of good pointers for getting this close to the answer. Here are a few more:

                              when comparing strings, do not use == operator.
                              Use the .equals method. You can read here for the reason for that. So to you should do something like
                              if(string.equal s("+")) {
                              //do stuff here
                              }

                              Also use an if-else construct for this rather than a series of ifs. Won't notice the difference but you should use a series of ifs only if it is possible that more than one of them can be executed. In this case the operator is only going to be one of +,-,* or / so need for series of ifs

                              Comment

                              Working...