'Im stuck, I would like to create a method to store all the values(volume, area,r,h) into an array in a ascending order. See code I have so far below
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);
}
}
Solution 1:[1]
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));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

