Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with beginner's calculator

  1. #1
    Junior Member CrashOverride's Avatar
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with beginner's calculator

    So I'm just starting out with JAVA at school and within the first week I'm way beyond what my book can do for me so I started writing a calculator that had four inputs, three for numbers and the fourth one for what I wanted to do to the numbers i.e. addition subtraction multiplication, and limited division(no decimals ).

    public class Calculator
    {
     
        public int number1;
        public int number2;
        public int number3;
        public int total;
        public String function;
     
        public void Number3(int number3)
        {
            number3 = number3;
        }
     
        public int Calculatorinput(int number1, int number2,String function) 
        {
     
     
            if(number3 == null)
                {number3 = 0;}
                else{number3 = number3;}
            if (function == "+")
            {total = number1+number2+number3;}
            else if (function == "-")
            {total = number1-number2-number3;}
            else if (function == "x")
            {total = number1*number2*number3;}
            else if (function =="/")
            {total = number1/number2/number3;}
            if (total == 0)
            {System.out.println("something is wrong");}
            else 
            {total = total;}
            return total;
        }
     
    }

    I know at this point it won't compile, the problem I'm having is making that third number an optional integer within the calculator, I know I'm either really close....or waaay off.
    Last edited by pbrockway2; October 14th, 2012 at 12:24 AM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with beginner's calculator

    Hi CrashOverride, welcome to the forums!

    I've added "code" tags to your post. The idea is to put [code] at the start of a section of code and [/code] at the end. That way the code will be properly formatted and more readable.

    In a similar vein it is a *very* good idea to format code according to Java coding standards - in particular one brace ({ and }) per line, and have the closing } brace line up with the start of the line containing the opening { brace. Also methods should start with a lowercase letter, and be descriptive of what the methods do. So, eg, setNumber3() would be a good name for the method that sets the value of number3.

    I know at this point it won't compile
    So, what are the compiler messages?

    ---

    public void Number3(int number3)
    {
        number3 = number3;
    }

    A small point, but assigning the value of number3 to itself does nothing at all. You probably mean

    public void Number3(int number3)
    {
        this.number3 = number3;
    }

    Ask if you are unclear about the difference in meaning between these two.

    ---

    the problem I'm having is making that third number an optional integer
    So-called "primitives" like int can't be null; they always have an actual integer value. So "if(number3==null)" makes no sense. (I'll come back to that.)

    I'm not exactly sure what you mean by saying that number3. You set it to zero at one point. That's OK for addition and subtraction where it will basically have no effect. But for multiplication you'll get zero for number1*number2*number3. And for division you should think about what you expect number1/number2/number3 to evaluate to.

    One way of implementing an optional value would be to have a boolean variable that represents whether a value has been assigned. (These are often called "flags"). Set the variable to true when a number gets assigned to number3 otherwise it will have its default value of false.

    public class Calculator
    {
        private int number1;
        //private int number2;
        //private int number3;
        private boolean number3set;
        //private int total;
        //private String function
     
        private void setNumber3(int number3)
        {
            this.number3 = number3;
                // added so that we can tell elsewhere whether or not
                // number3 has been set with this method
            number3set = true;
        }

    (always "private" unless you have an actual reason why not. I've commented out the unused variables: don't declare things unless and until you are going to use them for some purpose.)

    With that set up you can split your other method up into two parts:

        // renamed from Calculatorinput()
    private int doCalculation(int number1, int number2,String function) 
    {
        if(number3set)
        {
            // calculation with all three numbers here
        }
        else
        {
            // calculation that ignores number3 here
        }
    }
    Last edited by pbrockway2; October 14th, 2012 at 12:51 AM.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    CrashOverride (October 14th, 2012)

  4. #3
    Junior Member CrashOverride's Avatar
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with beginner's calculator

    Thanks a lot, after I read all your comments I felt a bit embarrassed that I overlooked all those minor things such as having zero within a multiplication equation would equal zero(I face palmed for a good minute just thinking stupid to myself), but I digress.

    What I meant at the beginning by "I know that this won't compile" is that I know the code as it is won't pass so I didn't want anyone to look at it to spend time thinking about why it won't pass as is. When I put number3 equal to itself I wasn't trying to achieve anything at the moment so I should've just commented that out, same thing with "if(number3==null)" I was just trying to put something along the lines of what I was thinking so that when I came back to it I wouldn't forget what I wanted to do.

    And again I apologize for having no comments...it completely escaped me to put them in for someone reason explaining what I wanted it to do and what I was thinking. Next program I'll make sure to pay extra attention to those things.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with beginner's calculator

    same thing with "if(number3==null)" I was just trying to put something along the lines of what I was thinking
    Yes, and it had (I hope) exactly that effect. Post back if what I suggested with a boolean flag doesn't work out for any reason.

Similar Threads

  1. Calculator
    By Vibex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2012, 05:23 PM
  2. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  3. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM
  4. Help with a beginner's java assignment: Survey results
    By lavloki in forum Java Theory & Questions
    Replies: 17
    Last Post: October 14th, 2010, 09:08 AM
  5. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM

Tags for this Thread