Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not printing my expected result  RSS feed

 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, this is a rather quick question. For the most part, I am getting my expected result printed out, however, it is only printing my thickness not my radius. For example, my current results are:
Disk radius: 10.0
Disk surface area: 314.0
Disk volume: 6.28
Objects are not equal.
Disk has thickness 0.05
Disk has thickness 0.07

When I am expecting is:

Disk radius: 10.0
Disk surface area: 314.0
Disk volume: 6.28
Objects are not equal.
The disk has radius 12.0 and thickness 0.05.
The disk has radius 12.0 and thickness 0.07.

As you can see, it is a relatively small error but I am not sure where it is coming from or what the error is exactly.
Here is my code:


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm not sure why your question involved expecting output that you hadn't written the code for (?).
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes more sense. You know, I think thought I had already, but I was actually just looking at my circle class which has something written for radius. My bad, sorry about that.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write your own definition of π: use Math.PI instead.
Your equals() method is incorrect; it doesn't permit null as a parameter, nor a parameter of a different type, both of which equals() ought to accept. You also have some poor style writing if (something) return true; else return false; ←Link telling you what you shou‍ld do. You also must overide hashCode whenever you override equals(). You can read all about it in the Java™ Tutorials and the Object documentation. There are two ways to test whether your object is of the correct type: instanceof and getClass(). If you are in a final class, then it doesn't matter which you use, otherwise instanceof is probably much better, but there can be nasty problems if you override equals() again in a subclass. I suggest the following simple implementations:-If you had more than one field or the field were a reference type, I would do it differently.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find all sorts of problems about Disc extends Circle. For a start, a disc is not a special kind of circle (nor is a circle a simplified kind of disc). Yes, I know there are books which show that sort of example, but it still doesn't implement an IS‑A relationship.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!