• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about using boolean to check a condition of an object?

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I have a program where i create an Object tv, from the Fernseher class which is the first class i make.Then in main i create the Object in the FernseherRemote class.
then trying to check the status of the tv, if it is on or off, it is initially off so i use the tv.setStatus(sum) to turn it on sum = true;then i use the tv.getStatus(sum) to return the "sum" and it is back to false??? i'll paste the code so you all can see it and maybe give me a hint as to why the "sum"= false after setting it...

first the Fernseher class,,



and here is the FernseherRemote class,, i have the "System.out.println("sum ="+sum+" is here too!");" to see the sum (true or false) to see if the correct sum gets to those points, which they don't.




Thanks for any tipps
Mike
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting the relevant code:
mike ryan wrote:


and here is the FernseherRemote class,, i have the "System.out.println("sum ="+sum+" is here too!");" to see the sum (true or false) to see if the correct sum gets to those points, which they don't.



Not sure if I got your doubt correctly.

You need to assign the value returned by setStatus to the sum variable. As it is a pass by value for primitives, the value of sum changed in the setStatus is not reflected in the FernseherRemote class. So I think changing
to might solve the problem.

Actually the APIs are not designed correctly-

void setStatus(boolean)
boolean getStatus()
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything is perfectly fine..Except you have made some compile time error.but as you have mention that you code is running so i assume that you have made error while posting this code on javaRanch.

Just declare the sum as globally..

Like


and see the magic
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and one more thing setStatus should be void

so convert it into


There should not be any return statement in the setStatus..
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:Everything is perfectly fine..Except you have made some compile time error.but as you have mention that you code is running so i assume that you have made error while posting this code on javaRanch.

Just declare the sum as globally..

Like


and see the magic


Making it global- Is it a good practice? I just zeroed in on the possible flaw and then told the solution. Going by the design of classes- They need to be fine tuned to follow the OO Principles.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you say,but in case of JavaBeans.
For getter and setter methods you have to declare a variable,which is common to both,
so globally declaring the variable is the best choice
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Thank you both for the quick replies, and i tried both versions as well as a mix of both...and still not working? the sum is set to true in the set method and when calling directly after the getStatus method it still comes back false immediately? When i make the variable Global, i still need to initialize it in the other class as false either locally or as an instance variable it is automatically set to false i believe? Is there anything else anyone sees that may be causing my problems?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of long lines, especially in code tags; they make the code difficult to read.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, primitive arguments are passed "by value" not "by reference." The thing you are passing (boolean sum) is a boolean primitive value.
Other issues with the code aside (why is a 'set' method returning a non-void value and why does your 'get' method accept an argument?) you might try something like


However, when you do this next:



sum is always going to end up being simply a copy of whatever value you passed in to the getStatus() call.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Hurtt wrote:In Java, primitive arguments are passed "by value" not "by reference." . . .
That should read
In Java, all arguments are passed "by value" not "by reference."
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have tried doing it a few different ways and still getting boolean sum=false?

Here is the code as it currently is





and the Fernseher class







any ideas? or is something more incorrect than i thought??

Mike
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:ok i have tried doing it a few different ways and still getting boolean sum=false?

Here is the code as it currently is





and the Fernseher class







any ideas? or is something more incorrect than i thought??

Mike


Where ever you are referring to the sum instance variable of class Fernseher- Use this.sum (to differentiate between the sum passed via the method parameters and the instance variable.

The order of methods being called can be changed to- setter (setStatus) and then getter(getStatus). You need not pass the any boolean to the getStatus. Instead in the getStatus() you could simply return this.status.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok ya kind of lost me there??
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

OK i got it to work now but it didn't work for me with this.on but rather with (Fernseher.on ----> i changed sum to on cause it seemed more appropriate) in the set and get methods and also in the FernseherRemote class.
Maybe i was using the "this.on" incorrectly not sure.I can post the code as well if anyone is interested in seeing it.

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic