Results 1 to 16 of 16
Thread: Beginner needing help
- 01-06-2011, 06:42 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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 06:50 PM. Reason: Moderator Edit: code tags added
-
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.
- 01-06-2011, 07:07 PM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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:public void enterOperator(char Oper) { if (Oper != '+' '-' '*' '/') System.out.println("Invalid operator"); }
Best,Java Code:if(Oper != '+' || Oper != '-' || Oper != '*' || Oper != '/') { System.out.println("Invalid operator"); }--user0--
- 01-06-2011, 07:30 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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 :-)))
- 01-06-2011, 07:35 PM #5
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-06-2011, 08:33 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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.
- 01-06-2011, 08:41 PM #7
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.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-06-2011, 08:52 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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)
- 01-06-2011, 09:03 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 01-06-2011, 09:10 PM #10
Seems like your mutator method should look like this,
Here is a link if you are interested to know about accessors and mutators : Accessors-MutatorsJava 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. } }
Make sure to declare this correctly
The assignment above is not valid. Either put the char value in ' ' or assign the proper value.Java Code:operatorChar = ' ';
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-07-2011, 10:40 AM #11
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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]
- 01-07-2011, 11:00 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
So I changed it a bit; != to == and assigned Oper til a previous variable (after the if statement)Java Code:public void enterOperator(char Oper) { if (Oper == '+' || Oper == '-' || Oper == '*' || Oper == '/') {operatorChar = Oper;} else {System.out.println("Invalid operator");} }
and my else statement remains the same.
Does this look better?Last edited by Krakatau7; 01-07-2011 at 11:17 AM.
- 01-07-2011, 11:02 AM #13
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
- 01-07-2011, 11:13 AM #14
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-07-2011, 05:09 PM #15
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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.
- 01-07-2011, 05:45 PM #16
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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
This is the toString methodJava 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 ...
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
-
Ambitious Noob Here - Needing Guidance
By DizzyEwok in forum New To JavaReplies: 14Last Post: 06-28-2009, 09:25 PM -
All posts needing mod approval first?
By xcallmejudasx in forum Suggestions & FeedbackReplies: 0Last Post: 05-26-2009, 04:35 PM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM -
Reading Char without needing to press enter
By x0psci in forum New To JavaReplies: 0Last Post: 11-23-2007, 04:28 PM -
Needing Help!!
By kingjut06 in forum JDBCReplies: 1Last Post: 07-07-2007, 11:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks