0

I need a method or way to store all values(r,h,area,volume) in a array for the cylinder class below. The array should contain the values in in ascending order.This is the code below to find area and volume.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
     ArrayList<Double> shapeProperties = new ArrayList<Double>();


    double tc, r, h;
    System.out.println("Enter # of test cases");
    tc = sc.nextInt();

    for (int i = 0; i < tc; i++) {
        System.out.println("Enter base radius");
        r = sc.nextInt();
        System.out.println("Enter height");
        h = sc.nextInt();
        if (tc > 10 || r > 10 || h > 10) {
            System.out.println("Please enter a value under 10");
            break;
        } else {
            System.out.println("Radius r= " + r);
            System.out.println("Height h= " + h);
            System.out.println("Volume = " + getVolume(r, h));
            System.out.println("Area = " + getArea(r, h));
        
            ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
            

        }

    }
}
static ArrayList<Double> sortPropertiesInArray(double r, double h, double area, double volume) {
    ArrayList<Double> shapeProperties = new ArrayList<Double>();
    shapeProperties.add(r);
    shapeProperties.add(h);
    shapeProperties.add(area);
    shapeProperties.add(volume);

    Collections.sort(shapeProperties);

    return shapeProperties;
}


public static double getVolume(double r, double h) {
    return Math.PI * r * r * h;
}

public static double getArea(double r, double h) {
    return 2 * Math.PI * r * (r * h);
}

}

TechDev
  • 11
  • 3
  • Please add more detail. Do you want an array of `r, h, area, volume` in an ascending order? Or do you want an array of multiple `r, h, area, volume` tuples (representing different shapes in an ascending order and if so, ascending order of what, `volume`, `area` or others? – cSharp Apr 21 '22 at 02:55
  • yes array of r,h,area,volume in ascending area for only cylinder I have in code. so just one shape. ascending from lowest value to highest – TechDev Apr 21 '22 at 02:57

1 Answers1

0

Something like below should work:

ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);

Collections.sort(shapeProperties);

If you want to encapsulate this into a function, you could:

ArrayList<Double> sortPropertiesInArray (double r, double h, double area, double volume) {
    ArrayList<Double> shapeProperties = new ArrayList<Double>();
    shapeProperties.add(r);
    shapeProperties.add(h);
    shapeProperties.add(area);
    shapeProperties.add(volume);

    Collections.sort(shapeProperties);

    return shapeProperties;
}

and call it as such: ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));

Example output: image for the above code working as intended.

cSharp
  • 2,884
  • 1
  • 6
  • 23
  • would I create a new method or add that to main method? I rather have it in a method . thanks for your help. – TechDev Apr 21 '22 at 03:14
  • Depends on how you want to use it. Either way should work. – cSharp Apr 21 '22 at 04:31
  • im having issue using it with a method. area and volume are not being taken as they are methods not variables like r and h. – TechDev Apr 21 '22 at 04:40
  • they do seem to return a `double`, so as long as you set the parameters as `double`s, it should work, I think. I've updated the answer to reflect this. – cSharp Apr 21 '22 at 04:50
  • updated the code up above in question . still don't seem to work – TechDev Apr 21 '22 at 05:37
  • What error are you getting, on which line? – cSharp Apr 21 '22 at 05:40
  • no errors. its just not printing the array – TechDev Apr 21 '22 at 05:41
  • You get `properties` in your code using the method, but where are you printing it? – cSharp Apr 21 '22 at 05:42
  • if I do sysout with the method name.its only printing the r and h not the area and volume – TechDev Apr 21 '22 at 05:46
  • im still learning if you can show me what im doing wrong. – TechDev Apr 21 '22 at 05:47
  • I'm trying to figure out what's wrong. Could you update your code to reflect where you are printing as well? Your current question has no line where you print `properties`. And try to compare with the image I attached in my answer and see what's different. – cSharp Apr 21 '22 at 05:52
  • got it. appreciate it. will I be able to change the array list to a regular array? – TechDev Apr 21 '22 at 06:00
  • [You could](https://stackoverflow.com/questions/2843366/how-to-add-new-elements-to-an-array), but unless you **really** need it, I recommend against it. `ArrayList` features many, many more functionalities. – cSharp Apr 21 '22 at 06:06
  • ArrayList myProperties = sortPropertiesInArray (r, h, getArea(r, h), getvolume(r,h)) ; ----is this creating a new object or assigning values from method to my properties . – TechDev Apr 21 '22 at 06:32
  • `sortPropertiesInArray` method creates and returns a new `ArrayList` as denoted by the method's type, which gets assigned to the `myProperties` on the left side of the assignment operation. – cSharp Apr 21 '22 at 06:37