IntelliJ Java IDE
The moose likes Beginning Java and the fly likes Comparing variables - Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Comparing variables - Interfaces" Watch "Comparing variables - Interfaces" New topic
Author

Comparing variables - Interfaces

Mike Johnston
Greenhorn

Joined: Jan 17, 2012
Posts: 11
Hi guys,

Basically what I have to do is compare areas of a circle, rectangle, and square but I'm unsure how to solve it. Every thing I try java seems to complain about haha.



I have started the static method but am not sure how to compare them because of the change in the second variable.



If someone could help me out it would be appreciated!

Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
You need to provide more details and at least make some attempt at writing the code so folks here can help.

The brute force method would be to write a sameArea() function for each pair of parameter types (i.e., Rectangle, Rectangle Rectangle, Circle, ...etc). Function overriding would ensure that the correct method is called when you invoke sameArea based on the arguments to the method invocation.

I assume you have learnt Polymorphism and Inheritance? If so, try to figure out how Polymorphism could help you in this situation. What do Rectangle, Circle and Square have in common?

PS - Your method declaration is very wrong. Where are your brackets and parameters for the method? What do your Circle/Rectangle classes look like? Have you written any methods for those classes?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
The object-oriented way to do it is to create an interface which includes a way to get its area. The get method returning a double does not allow anybody to alter the object, so you can make the object immutable if you so wish. Make your circle square and triangle classes implement that interface, then you can apply it to a method taking two objects of that interface as parameters. You can easily change my example to fit your requirements.
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Campbell, I think you meant to override the getArea() function in the child classes. You named the methods getSize().
 
IntelliJ Java IDE
 
subject: Comparing variables - Interfaces
 
Threads others viewed
ques from overloading
Spring factory not creating new objects
cant figure out whats wrong with my absract class
Calling methods using dynamic binding
its a classic! help with area of shapes!
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture