• Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble testing my boolean statement  RSS feed

 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a class for encapsulating coins and I was to do a boolean statement but when I test the statement the results are not showing. Can anyone tell me what I a doing wrong?

Here is the code for my coin class coins.java



----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is the code to my main test program project3.java







 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. When you post source, UseCodeTags to help format your listing. I've added them for you (go back and edit your message to see how they are used). Although, maybe because you embedded some plain text in the middle, it doesn't look to me like the curly brackets have matching opens and closes everywhere they should.

I also don't see an actual main routine.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which boolean statement is giving you problems? What output are you getting? What output are you expecting?
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for responding. The Boolean statement is located in the coins.java code. I was expecting the return to be the print out number of nickels equals number of dimes in the main project.java test program
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you referring to the equals method at Line 94 of the first listing of source code? I don't see that method being called from anywhere.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that was it. I guess that was the problem I thought that was all that was needed. How do I call the method?
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard the last post I see what I need to do to call it. Thank you Stevens.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey CC,
In regard to the method equals. It is custom in Java to start any method with a boolean as return tip with "is". In this case it would result in IsEquals. Although that name is not very descriptive. The name off the method should be kinda selfexplanatory to what it does.

On the functionallity of the mehtod. Is the println there for debugging purpose. Or do you want that line printed in case the test results true?
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted println in case it were true I went back in to test my results and the printout can back true in both true and false cases so I tried to edit the code. Now I am having an issue with the new Boolean code. Do you have any tips since I am getting error messages with my Boolean expression now.




public boolean equals (double NON, double NOD)

{
System.out.println(" number nickels equals number dimes");
return NON == NOD;

else
return NON != NOD;
System.out.println(" number of nickels is not equal to the number of dimes");
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stijn Rensen wrote:In regard to the method equals. It is custom in Java to start any method with a boolean as return tip with "is". In this case it would result in IsEquals.


Keep an eye on your case there, Stijn. Method names should always start with a lower-case letter, so his name in this case would be isEquals, not IsEquals.

As a general rule, it is a good idea to follow the JavaBeans standards for getters, setters, and boolean methods, which means prefixing booleans with is is good practice. It's not universally followed, though, notably with the Object.equals method (which is distinct from C. C.'s method because it has a different signature). Reasonable minds can differ, but I would advise generally against calling a method "equals" unless it actually is meant to override Object.equals. And that's a rather more complicated topic than, to a beginner, it is likely to appear.

C. C., in your code, your equals method appears to test for the quality of the two arguments passed to the method. There's nothing inherently wrong with that, but a more common design approach tends to test for the equality of one argument passed to the method, and a corresponding instance parameter for a given instance of the class containing your method definition. That is, if you have a class "MyClass," which contains an integer "count," you could test for equality of that value with a method called, "countEquals" like this:


Now, even that is kind of clunky, because you might as well have a getter that returns the value of count, which you can test from the code that would otherwise call countEquals:



Then again, even that violates a design guideline called "tell, don't ask," which (roughly) means that, instead of querying an instance and doing something depending on what that query tells you, you give the instance the information it needs for that instance to decide what to do. This helps put your code where it should be: in the classes that store the data you would other be querying from instances of other classes. Again, it's not inherently wrong to query the state of an instance. But, doing so tends to move your code out of the classes you are querying, and into what people (without much reverence) call a "God" class, which is one huge class with all your code in it, leaving all your other classes as really just data structures, with no code to speak of (except getters, setters, and is<Boolean> methods).

Here's an article about the concept.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C C Campbell wrote:Here is the code for my coin class coins.java

C C Campbell, your code shouldn't compile, can you see why?


 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Keep an eye on your case there, Stijn. Method names should always start with a lower-case letter, so his name in this case would be isEquals, not IsEquals.


Thanks for pointing that out. Though I really thought I had it spelled with lower case i in my reply. I guess I shouldn't be posting from my Ipad just before I go to sleep......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!