• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing variables - Interfaces  RSS feed

 
Mike Johnston
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I think you meant to override the getArea() function in the child classes. You named the methods getSize().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!