Results 1 to 16 of 16
  1. #1
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default Mutator method for a calculator

    So this is very complex to me, since I havent got the hang of java (bluej) so far. But Ill try and explain what the mutator method need to do.

    Its a method that lets the user enter a number. It says its passed as a parameter. The result of entering or passing the number depends on the previous action done by the user. If that was a choice of calculation, it is the second or a following number in the calculation otherwise it is the first number.

    Im not sure where to start, but a field (a logical) that supposed to keep track of if the next number entered is the first number or the second number in the calculation. And its initialised in the constructor below (ctrlTest)

    Java Code:
    public Calculator(int _firstNumber, int _secondNumber, char operatorChar, boolean _ctrlTest)
        {
            
            _firstNumber  = firstNumber;
            _secondNumber = secondNumber;
            operatorChar  = ' ';
            _ctrlTest      = ctrlTest;
    Furthermore I have a toString method - however, I dont think I need to use that in this method.

    Can anyone help me?

    best regards

  2. #2
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Hi Krakatau7,

    Can you ask the question more clearly. Can you give us more information on EXACTLY what you are asking?

    Your question could be a number of things and I don't want to start explaining the wrong thing.

  3. #3
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Im not sure how I can explain it more simple

    But

    The method should let a user enter a number in any given calculation. However, the method need to know wether the number entered is the first number or second number in the calculation.

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by Krakatau7 View Post
    Im not sure how I can explain it more simple

    But

    The method should let a user enter a number in any given calculation. However, the method need to know wether the number entered is the first number or second number in the calculation.
    Can you send what you have so far? Are you asking for someone to create the entire method for you (this will rob your learning experience)?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It sounds as if you need to create a mutator method for entering a number, such as

    Java Code:
    public void setNumber(int number) {
      // ... your code goes here
    }
    Your class is going to have to know its state, that is whether or not a method that tries to set the type of operation to performed was called previously or not, and if not, you set your first number (perhaps called firstNumber?) to the value passed into the parameter. If the operation was set just prior to calling the mutator method, then you will set the second number (perhaps called secondNumber?). You might want to give the class a boolean, say operationRecentlySet and set it equal to false. If the operation is set (by its mutator method), then set this boolean to true. If any other mutator method method is called, then set this boolean to false in the mutator method. Use the value held by this boolean variable to decide whether to set the first number or the second number.

    Luck.

  6. #6
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Hi, this is what I had gotten so far - it might be a bit of what youre speaking of. Does it make any sense?

    Java Code:
    public int enterNumber(int num)
        {
            if(ctrlTest == true) {this.firstNumber = num;}
            else {this.secondNumber = num;}
            return num;
        }

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    It sounds as if you need to create a mutator method for entering a number, such as [ ... ]
    Good advice because that's exactly what an infix calculator needs: a state machine. This little problem has been solver in the 60s of the previous century already; maybe the OP finds the following link interesting: Finite State Machine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Krakatau7 View Post
    Hi, this is what I had gotten so far - it might be a bit of what youre speaking of. Does it make any sense?

    Java Code:
    public int enterNumber(int num)
        {
            if(ctrlTest == true) {this.firstNumber = num;}
            else {this.secondNumber = num;}
            return num;
        }

    This is close to what I mean but with changes. For one, in general a mutator method doesn't return a value, or if it does, I've seen it return a boolean to somewhat let the caller know if the input was valid or not. So on that note, I'd have the method be a "void" method and not return anything.

    Next, while this is sort of a nitpick, it's much cleaner to say
    if (ctrlTest)

    rather than
    if (ctrlTest == true)

    if only to avoid the sneaky mistake of doing
    if (ctrlTest = true)

    Lastly, if ctrlTest is the variable that tells whether an operation was just recently set, you'll want to set it at the end of this mutator method.

    edit: and I second all that JosAH has had to say above.

  9. #9
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Hi, thanks for your great great help - and sorry for being annoying with my stupid questions.

    I was wondering if you could assist me further with some adjustments.

    I have no real code giving the user opportunity to enter anything from my methods. I have learned I need to use java.util.scanner.

    So ive decided to make a main function where this is done.
    Java Code:
    public static void main (String args[])
    
    Scanner sc = new Scanner(System.in);
    double firstNumber, secondNumber, result;
    firstNumber     =  sc.nextDouble();
    secondNumber =  sc.nextDouble();
    But I'm not done yet, can you hint so I can finish my calculator?

    I think I need to end it with; System.out.println(result)

  10. #10
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    If this was a simple calculator, the operators would be +, -, / and *. After the user types in the first number, they would type in the operator. Since you are using Scanner, you might want to use String for the operator variable, just to keep things simple.

    So you will read the operator just like you read your numbers in the previous post. Then you read the second number. After that, you will need some simple if statements to get the result:
    if(operator.equals("+"))
    --user0--

  11. #11
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Sorry, I posted an incomplete response earlier. Reposting complete response:

    If this was a simple calculator, the operators would be +, -, / and *. After the user types in the first number, they would type in the operator. Since you are using Scanner, you might want to use String for the operator variable, just to keep things simple.

    So you will read the operator just like you read your numbers in the previous post. Then you read the second number. After that, you will need some simple if statements to get the result and then print to the console.
    Java Code:
    if(operator.equals("+")) {
       result = firstNumber + second Number;
    }
    System.out.println("Result = " + result);
    Hope this helps.

    @ JosAH - thanks for that very informative link on Finite State Machine :)

    Best,
    --user0--

  12. #12
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    The thing is, that I have already made a method to define the operators +-*/. I was wondering if I could somehow call for that in that scanner package? or should I just make four of those equals - one for each operator?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by user0 View Post
    @ JosAH - thanks for that very informative link on Finite State Machine :)
    You're welcome of course but note that a FSM (Finite State Machine) can handle only part of the problem; it fails when parentheses are introduced, e.g. 2*(3+4) needs a stack and FSMs by themselves can't handle that. A FSM needs a stack, and it thereby transforms itself to an (almost) full blown parser (a context free machine). Discussing those beasts would be fun but well beyond the scope of this thread ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Krakatau7 View Post
    The thing is, that I have already made a method to define the operators +-*/. I was wondering if I could somehow call for that in that scanner package? or should I just make four of those equals - one for each operator?
    I don't think you posted this method.

  15. #15
    Krakatau7 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Im really sorry to be doing this, but can you take a look at my complete code and give me some pointers to what I need to look at?


    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    /**
     */
    public class Calculator
    {
        private double firstNumber;
        private double secondNumber;
        private char operator;
        private boolean hasSet;
        
        /**
         */
        public Calculator()
        {
            this.firstNumber  = 0;
            this.secondNumber = 0;
            this.operator     = ' ';
            this.hasSet       = false;
        }
    
        /**
        */
        public double getfirstNumber()
        {
            return firstNumber;
        }
        public double getsecondNumber()
        {
            return secondNumber;
        }
        public char getoperator()
        {
            return operator;
        }
        public String toString()
        {
            DecimalFormat df = new DecimalFormat("#.##");
            return df.format(firstNumber);
        }
        public void setOperator(char oper)
        {
            if (oper == '+' || oper == '-' || oper == '*' || oper == '/') {operator = oper;}
            else {System.out.println("Invalid operator");}
            Scanner sc = new Scanner(System.in);
            char Operator = oper;
        }
        public void setNumber(double num)
        {
            if(hasSet) {firstNumber = num;}
            else {secondNumber = num;}
            Scanner st = new Scanner(System.in);
            double firstNumber, secondNumber;
            firstNumber  = st.nextDouble();
            secondNumber = st.nextDouble();
        }
        
       public void execute()
        {
            if (hasSet == true)
            {
                if(operator == '+')
                {   
                firstNumber = firstNumber + secondNumber;
                }
                else if(operator == '-')
                {
                firstNumber = firstNumber - secondNumber;
                }
                else if(operator == '*')
                {
                firstNumber = firstNumber * secondNumber;
                }
                else if(operator == '/')
                {
                     if (secondNumber == 0)               
                     {
                     System.err.println("Kan ikke dividere med 0");
                     }
                     else
                     {
                     firstNumber = firstNumber / secondNumber; 
                     }
                }
                else        
                {
                System.err.println("2 numbers needed");
                }
            }
        
        }
        
        public static void main(String args[])
        {
            Calculator c = new Calculator();
        }
    }
    I dont know how to work with the Scanner package.

  16. #16
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I think you need to step back think about the design in pseudocode.
    You have lots of parts but are not quite sure how to put them together.
    I'll give you a rough start.

    class Calculator {
    declare Double firstNumber, secondNumber, Character operator

    constructor {
    set the above to null

    public void run(){
    while (firstNumber == null)
    getNumber();
    while (operater == null) getOperator()
    while (secondNumber is null) getNumber()
    // have all values now
    execute(firstNumber, secondNumber, operator)
    }

    public void execute(firstNumber, secondNumber, operator){
    Double result= null;
    switch (operator)
    case '+' : result = firstNumber + secondNumber;
    break;
    case '-' . . . .
    if (result != null)
    System.out.println(" result = " + result)
    else
    error message
    }

    public static void main(String args[])
    {
    Calculator c = new Calculator();
    c.run()
    }

    }
    That is all I have time for now.
    You will need the getNumber method and getOperator method.
    These will get the input but not set the values if the input is invalid.
    I can help with those tomorrow if needed.

    Good luck.

Similar Threads

  1. Calendar mutator problem
    By WimHaar in forum New To Java
    Replies: 7
    Last Post: 01-04-2011, 12:25 PM
  2. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 07:27 PM
  3. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  4. Replies: 6
    Last Post: 12-02-2008, 11:15 PM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 10:29 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
  •