Results 1 to 16 of 16
Thread: Mutator method for a calculator
- 01-08-2011, 07:43 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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)
Furthermore I have a toString method - however, I dont think I need to use that in this method.Java Code:public Calculator(int _firstNumber, int _secondNumber, char operatorChar, boolean _ctrlTest) { _firstNumber = firstNumber; _secondNumber = secondNumber; operatorChar = ' '; _ctrlTest = ctrlTest;
Can anyone help me?
best regards
- 01-08-2011, 08:50 PM #2
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 01-09-2011, 06:40 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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.
- 01-09-2011, 07:19 PM #4
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
-
It sounds as if you need to create a mutator method for entering a number, such as
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.Java Code:public void setNumber(int number) { // ... your code goes here }
Luck.
- 01-09-2011, 07:31 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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; }
- 01-09-2011, 07:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
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.
- 01-10-2011, 06:41 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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.
But I'm not done yet, can you hint so I can finish my calculator?Java Code:public static void main (String args[]) Scanner sc = new Scanner(System.in); double firstNumber, secondNumber, result; firstNumber = sc.nextDouble(); secondNumber = sc.nextDouble();
I think I need to end it with; System.out.println(result)
- 01-10-2011, 07:19 PM #10
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 01-10-2011, 07:23 PM #11
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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.
Hope this helps.Java Code:if(operator.equals("+")) { result = firstNumber + second Number; } System.out.println("Result = " + result);
@ JosAH - thanks for that very informative link on Finite State Machine :)
Best,--user0--
- 01-10-2011, 07:29 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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?
- 01-10-2011, 07:52 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-10-2011, 09:14 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
- 01-10-2011, 10:03 PM #15
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
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?
I dont know how to work with the Scanner package.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(); } }
- 01-10-2011, 11:15 PM #16
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
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.
That is all I have time for now.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()
}
}
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
-
Calendar mutator problem
By WimHaar in forum New To JavaReplies: 7Last Post: 01-04-2011, 12:25 PM -
Accessor & Mutator methods understanding..
By 6thDAY in forum New To JavaReplies: 3Last Post: 08-14-2010, 07:27 PM -
Accessor/Mutator Question
By noble in forum New To JavaReplies: 4Last Post: 02-02-2010, 04:21 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:15 PM -
mutator method
By dirtycash in forum New To JavaReplies: 7Last Post: 11-22-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks