# How to create generic method for comparing two objects [on hold]

59
July 28, 2019, at 3:20 PM

Okay. Write a program that has a Class 2D class that contains methods for calculating volume and surface shape. Then create classes Rectangle, Circle and Triangle that inherit the Class 2D class and implement its methods.For Rectangle, Circle, and Triangle class to declare necessary attributes for calculating volume and surface. Then make a Class ShapeUtill. The class ShapeUtill should contain and implement static methods for Comparison of two forms of volume and surface (total 2 methods). Methods should receive generic types like parameters that must be classes inherited by Form 2D

Before my main problem, I tried to implements(overridng) method from one class to another and I did that in this way, is good or?

`````` @Override
public void areaAndVolumeOfCircle(Circle c) {
double areaCircle = Math.pow(c.getR(), 2) * Math.PI;
double volumeCircle = 2 * c.getR() * Math.PI;
}
``````

So my main problem is, how to implements two generic methods to compare area and volume of two shapes(circle, rectangular or triangle).

In my class Shape2D i have this methods:

``````    public void areaAndVolumeOfCircle(Circle c) {
double areaCircle = Math.pow(c.getR(), 2) * Math.PI;
double volumeCircle = 2 * c.getR() * Math.PI;
}
public void areaAndVolumeOfRectangual(Rectangular r) {
double areaRectangle = r.getA() * r.getB();
double volumeRectangle = (2 * r.getA()) + (2 * r.getB());
}
public void pIObimTrougla(Triangle t) {
double areaTriangle = t.getA() + t.getB() + t.getC();
double volumeTriangle = (t.getB() * t.getH() / 2);
}
``````

Also, class `Circle, Rectangular, Triangle` extends class `Shape2D`.

So I want to implements two static generic method in new class `ShapeUtil` to compare two objects.

I tried that in this way, but doesnt work(type Circle does not take paramaters)

``````   public static <T, I> void compare(Cicle<T, I> c, Rectangular<T, I> r) {
}
``````

Thank you, Im here if I need to explain something better!

I think you should create an abstract class `Shape2D` which will be super class to all your figures(Circle, Rectangles etc.). You create a method `compareArea` and an abstract method `area`.

``````abstract class Shape2D {
double area = 0;
abstract double area();
int compareArea(Shape2D that) {
return Double.compare(this.area, that.area);
}
}
class Circle extends Shape2D {
public Circle(float r) {
area = area();
}
@Override
public double area() {
return area;
}
}
class Rectangle extends Shape2D {
private float side1, side2;
Rectangle(float side1, float side2) {
this.side1 = side1;
this.side2 = side2;
area = area();
}
@Override
public double area() {
area = side1 * side2;
return area;
}
}
``````

I think you should read some java book to know how Java and OOP work, it will save you a lot of time not thinking about doing stuff which is not possible such as doing this with generics.

First of all my solution is trying to reach a high cohesion with low coupling. I really can't understand why you have methods which don't return anything and calculate two different things (area and volume)

Have a class which compares two objects that belongs to the same hierarchy sounds weird and I'm not understanding why Circle has generics.

Having said that this design provides you a way to compare objects pushing the logic to class avoiding

``````abstract class Shape2D {
abstract double area();
boolean isBiggerThan(Shape2D shape) {
return this.area() > shape.area();
//or volume, or create a method hasBiggerAreaThan and hasBiggerVolumeThan
}
}

class Rectangle extends Shape2D {
/*Create constructor of rectangle*/
@Override
double area() {
return this.height * this.width;
}
}
``````
POPULAR ONLINE

#### How to print table column values counts in the welcome.blade.php ### Algorithm for finding the outer layer of inner matrix inside a matrix

I am trying to solve the following algorithm, and I got stuckI hope you could help

61 ### Does >> operator have replacement in Java? [on hold]

I am converting a C++ code to JavaI am stuck with the C++'s >> assignment operator replacement

80 ### JavaFX MediaPlayer crashes JRE completely

I ve implemented a JavaFX Application for showing jpg and mp4 files using ImageView and MediaPlayer

101 ### With spring-amqp, what's the best way to send a message to rabbitmq from inside a PublisherReturn callback?

I'm using spring-amqp:21

92