Results 1 to 6 of 6
- 12-07-2012, 01:20 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Checking if a Boolean Within Another Class Object is True
So I have a project I'm working on, and for one part, I have to check if a boolean is true. However, this boolean is a local variable in a method of another class type I made, and I don't know how to check for that.
So here's the method in the non-main class, "Stock":
and here's how I'm trying to check, unsuccessfully.Java Code:public boolean Buy(int num, double pr) { boolean ok = false; if (pr > 0.95*sprice && pr < 1.05*sprice) { ok = true; svolume -= num; sprice = pr; } return ok; } public boolean Sell(int num, double pr) { boolean ok = false; if (pr > 0.95*sprice && pr < 1.05*sprice) { ok = true; svolume += num; sprice = pr; } return ok; }
So does anyone know how I would go about this?Java Code:choice4 = Integer.parseInt(JOptionPane.showInputDialog("Please enter one of the following numbers to sell that stock:" + stockOut + "\n" + (numberOfStocks + 1) + " to exit")); if(choice4 != (numberOfStocks + 1)) { Boolean testSell = myStocks[choice4 - 1].Sell(numberBought = Integer.parseInt(JOptionPane.showInputDialog("How many of the " + myStocks[choice4 - 1].getName() + " stocks would you like to sell?")), newPrice = Double.parseDouble(JOptionPane.showInputDialog("Please input the new price of the " + myStocks[choice4 - 1].getName() + " stocks."))); if(testSell = true) { myCustomers[choice1 - 1].successfulSell((choice1 - 1), newPrice, numberBought); } else { JOptionPane.showMessageDialog(null, "Invalid price entry."); } }
Thanks in advance!
-
Re: Checking if a Boolean Within Another Class Object is True
The value returned by the buy(...) method is what you're after. Having said that, where do you call the buy method in the second bit of code?
Note that method names (like variable names) should begin with a lower-case letter, not a capital letter, and so the method should be named buy(...) not Buy(...). Class names should begin with capital letters.
- 12-07-2012, 02:55 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
-
Re: Checking if a Boolean Within Another Class Object is True
I would suggest that you not do this -- it is guaranteed to confuse us (at least me). Please understand that we are at a distinct disadvantage since we do not have your whole program in front of us, nor do we know any of the background information or your thought processes involved in this process. So if you are going to have us analyze a method, show us that method itself rather than a different one. How are we to assume that the methods are symmetric without seeing it?
So having said that, your code posted has at least one minor problem and at least one major problem. The minor problem is that you're using a Boolean instead of boolean, with the latter being a primitive and the former being a reference type wrapper for the primitive type. Use the primitive type unless you have no choice (such as if you were putting them into a collection) as there's no need for the extra over-head that the wrapper type requires.
The major problem is that you're re-assigning the variable it seems. You're doing this:
which is trying to assign true to testSell. Does that even compile? It should never be done. I suspect you're trying to do this:Java Code:if(testSell = true) {
Notice the difference? This tests if testSell is true or not, but even this shouldn't be done. Instead you should use:Java Code:if(testSell == true) {
Java Code:if(testSell) {
- 12-07-2012, 03:27 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Re: Checking if a Boolean Within Another Class Object is True
Oh, okay, makes sense.
Thank you very much, and thanks for the suggestions!
-
Re: Checking if a Boolean Within Another Class Object is True
You're welcome!
Similar Threads
-
store and recall string and integer data when a boolean is set to true
By lukeirves in forum New To JavaReplies: 2Last Post: 06-16-2012, 05:07 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
my simple boolean code keeps saying true
By shazakala in forum New To JavaReplies: 8Last Post: 03-27-2011, 10:30 AM -
Boolean always true?? What am I doing wrong?
By Forty0ztoFreedom in forum New To JavaReplies: 4Last Post: 03-02-2011, 06:13 PM -
Checking class of object
By ianyappy in forum New To JavaReplies: 2Last Post: 12-06-2010, 12:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks