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

    Default Beginner needing help

    Im currently nitting a small calculator

    Im to create a mutator method that lets the user chose one of the four calculation forms ( + - * /) - The characters + - * / is passed as parameters but my method should check that the user is using a legal character.

    This is my method so far, however bluej needs a ')' somewhere - I cant see it. Also, since its a mutator method, I figure it should change one of my variables but Im not sure which.

    Java Code:
    public void enterOperator(char Oper)
        {
            if (Oper != '+' '-'  '*'  '/')
            System.out.println("Invalid operator");
        }
    Last edited by Fubarable; 01-06-2011 at 07:50 PM. Reason: Moderator Edit: code tags added

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

    Default

    Code tags added to help your code become readable. To learn to do this yourself, please see the first link in my signature links below. Also, This isn't really an IDE or an "other-IDE" type question as it has nothing to do with BlueJ and all to do with basic Java, so I'm moving this thread to the new to Java section. Also, your title is poor. Of course you need help, else you wouldn't be posting here. Better to give a one line summary of the problem, say: "compiler error, says missing parenthesis" or somesuch.

    Finally, you may not be showing us information to help us solve the problem. I suggest showing more code (with code tags), posting the actual error message and showing with a comment in your code which line causes the error to occur.

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

    Default

    Java Code:
    public void enterOperator(char Oper)
        {
            if (Oper != '+' '-'  '*'  '/')
            System.out.println("Invalid operator");
        }
    Two things wrong here I can see: 1. When you are checking for equality in Java, you need to do each one individually. 2. When comparing multiple conditions, you need to use a logical operator such as && (AND) or || (OR). Try breaking your if condition as follows:
    Java Code:
    if(Oper != '+' || Oper != '-' || Oper != '*' || Oper != '/') {
                 System.out.println("Invalid operator");
    }
    Best,
    --user0--

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

    Default

    Hello to both of you

    Thanks for the advice, I now know how to tag codes and I think my code is now partially working :-)))

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by user0 View Post
    Java Code:
    if(Oper != '+' || Oper != '-' || Oper != '*' || Oper != '/') {
                 System.out.println("Invalid operator");
    }
    That's correct!

    The missing parenthesis problem was also occurring because of the same issue and the compiler was expecting a ')' after the first token in the if statement.

    OP, please confirm if it works correctly now or not.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    yes, it works now.

    Although, in my assignment, this is meant to be a mutator method - but I dont see anything changing by this method.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    In that case, may be you are supposed to set this operator somewhere.

    Check the assignment one more time. You might be missing something out of it.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Since the method is about the calculation operator I suppose it has to be set with either the field concerning the operator or the accessor method also concerning the operator.

    [code]
    public Calculator(int _firstNumber, int _secondNumber, char operatorChar, boolean _ctrlTest)
    {
    _firstNumber = firstNumber;
    _secondNumber = secondNumber;
    operatorChar = ' ';
    ...
    // some code is left out
    ...
    public char getoperatorChar()
    {
    return operatorChar;
    }

    [coode]

    Either way, I dont know how to set it (or could you use the phrase "save it" ?)

    (again Im really new with this, so hints to anywhere I can read on this specific subject is very welcome)

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    An object's variables are typically accompanied by a pair of "getter" and "setter" methods. In this case, the getter method is already present as getoperatorChar(), and what you want to do is write the corresponding setter method. It seems that the method in your earlier post is halfway there, and you're just missing an assignment statement.

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Seems like your mutator method should look like this,

    Java Code:
    public void setOperatorChar(char Oper) {
    		if (Oper == '+' || Oper == '-' || Oper == '*' || Oper == '/') {
    			this.operatorChar = Oper;
    		} else {
    			// This can be anything as per your requirements 
                            // in case of invalid operator.
    		}
    	}
    Here is a link if you are interested to know about accessors and mutators : Accessors-Mutators

    Make sure to declare this correctly

    Java Code:
    operatorChar = ' ';
    The assignment above is not valid. Either put the char value in ' ' or assign the proper value.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Im really amazed with the help Im getting here.

    So far, I now know that I need to split up equality statements with a logic operator. I think my book mentions that mutator methods need to store variables more permanent. Also the right hand side must match the variable of the given method. I figure Oper is expressed by either getoperatorChar or just operatorChar. Any help on that?


    [code]
    public void enterOperator(char Oper)
    {
    if (Oper != '+' || Oper != '-' || Oper != '*' || Oper != '/')
    System.out.println("Invalid operator");
    operatorChar = Oper;
    }
    [code]

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

    Default

    Java Code:
     public void enterOperator(char Oper)
        {
            if (Oper == '+' || Oper ==  '-' || Oper == '*' || Oper == '/') {operatorChar = Oper;}
            
            else {System.out.println("Invalid operator");}
            
        }
    So I changed it a bit; != to == and assigned Oper til a previous variable (after the if statement)

    and my else statement remains the same.

    Does this look better?
    Last edited by Krakatau7; 01-07-2011 at 12:17 PM.

  13. #13
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Krakatau7 View Post

    if (Oper != '+' || Oper != '-' || Oper != '*' || Oper != '/')
    [code]
    if you can use Character class in your assignment
    Java Code:
    if ( "+-*/^%|&".contains(Character.toString(oper)) ){
                this.operatorChar = oper;
    }

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by Krakatau7 View Post
    and my else statement remains the same.

    Does this look better?
    No, it doesn't.

    The purpose of the mutator methods is to SET something. There is nothing getting set inside that else loop. What if the operator provided doesn't match with if statement? Then, your mutator method will eventually do nothing other than printing to console.

    Thats why I said in my previous post to handle this properly in that else loop.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    To Goldest

    Im not sure what you mean. I need to assign operatorChar = " " instead?

    and I think you need to explain to me how my else statement is not doing what is is supposed to do.

    Thank you.

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

    Default new mutator method that lets the user enter a number...

    ...but the method should check wether number is the first or the second in the calculation. This is done based on the previous action by the user - if it was a choice of calculation, the number entered is of course the second number. With no previous action by the user, the number is therefor the first number in the calculation.

    In the field, we were to create a logical field keeping track of if the next number entered is the first or the second number in the calculation. Im pretty sure that I need to involve this in the new mutator method.

    At the same time we were to make a a toString method changing a real number to string a string object shown with two decimals. Also very certain I need to include this in the new method.


    The last field is the logical
    Java Code:
    public Calculator(int _firstNumber, int _secondNumber, char operatorChar, boolean _ctrlTest)
        {
            // initialise instance variables
            _firstNumber  = firstNumber;
            _secondNumber = secondNumber;
            operatorChar  = " ";
            _ctrlTest      = ctrlTest;
        }
    
    ...
    some code left out
    ...
    This is the toString method
    Java Code:
     public String toString()
        {
            DecimalFormat df = new DecimalFormat("#.##");
            return df.format(firstNumber);
        }

    Im really without clues, so could you hint me away?

Similar Threads

  1. Ambitious Noob Here - Needing Guidance
    By DizzyEwok in forum New To Java
    Replies: 14
    Last Post: 06-28-2009, 10:25 PM
  2. All posts needing mod approval first?
    By xcallmejudasx in forum Suggestions & Feedback
    Replies: 0
    Last Post: 05-26-2009, 05:35 PM
  3. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 09:14 AM
  4. Reading Char without needing to press enter
    By x0psci in forum New To Java
    Replies: 0
    Last Post: 11-23-2007, 05:28 PM
  5. Needing Help!!
    By kingjut06 in forum JDBC
    Replies: 1
    Last Post: 07-08-2007, 12:38 AM

Posting Permissions

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