This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to create a liquid class

 
Joshua Flowers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new. So I am trying to create a liquid class that holds an int Density, and a Color color.

unfortunetly, everytime I try to run it I get this error



I really have no idea what I am doing wrong...

here is my code



hope to get some help
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable density is an int - a primitive type. You're trying to call methods (equals() and compareTo()) on an int - but you can't call methods on primitive types.

Use ==, < or > to compare the values of int variables.

And welcome to JavaRanch!
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class violates the contract of equals and hashCode in two ways:
1) equals should return false if the object is of an incompatible class, not throw a ClassCastException as your class will. Add a check:
2) You need to implement hashCode as well. The easiest solution:
For more information, read Object.equals(Object) and Object.hashCode().
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:return densitity

That's what the Italian lady did when her implant turned solid.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful, or I shall have to move this thread to MD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic