Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Thanked 0 Times in 0 Posts

    Comparing values of two objects

    I'm new to Java and just started studying it in university right now
    There is an excersie where we have to create a cylinder class, calculate its volume and surface area etc

    Part of the excersie is to compare the volumes of two cylinder objects in the class and return the one with the bigger volume

    The following is my code so far without the comparision method
    public class Cylinder

    private double radius;
    private double height;

    public Cylinder()
    radius = 0.0;
    height = 0.0;

    public Cylinder (double radius, double height)
    this.radius = radius;
    this.height = height;

    public double getRadius()
    return radius;

    public double getHeight()
    return height;

    public void printValues()
    System.out.println("Radius: " + radius + " Height: " + height);

    public double volume()
    double volume = Math.PI*Math.pow(radius,2)*height;
    return volume;


    public double surfaceArea()
    double surfaceArea = (2*Math.PI*Math.pow(radius,2))+(2*Math.PI*radius*height);
    return surfaceArea;


    can anyone explain to me how I can compare the volume values of two cylinders?
    I don't want solution just explanations and hints


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    I'd write a static method in this. Comparable and Comparator don't return objects, but you certainly could chain them so that they would be usable in a collection to sort for example.
    PHP Code:
    public static Cylinder getLargerCylinder(Cylinder c1Cylinder c2)
    c1.volume() >= c2.volume() ? c1 c2;    

    Simple as that. You can chain it to comparator.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts