I'm trying to complete a Java lab exercise that asks to display the volumes of objects (cylinders) in an array. Whenever I try to print the array the output always seems to be the last object in the array, when I'd like them all to print.
public class Cylinder implements Comparable<Cylinder> {
public static double radius;
public static double height;
public static String name;
public Cylinder(double radius, double height, String name){
this.radius = radius;
this.height = height;
this.name = name;
}
@Override
public int compareTo(Cylinder obj) {
Cylinder other = obj;
int result;
if (this.volume() > other.volume()){
result = -1;
}
else if (this.volume() < other.volume()){
result = 1;
}
else {
result = 0;
}
return result;
}
public double volume(){
double volume = Math.pow(radius, 2.0)*Math.PI*height;
return volume;
}
public double surface(){
double surface = (4.0*Math.PI)*Math.pow(radius, 2.0);
return surface;
}
public String toString(){
return "Name: " + name + ", radius: " + radius + ", height: " + height ;
}
}
import java.util.Arrays;
public class TestSolids {
public static void testCylinderSort(Cylinder[] cylinders){
for(int i = 0; i < cylinders.length; i++){
double volume = cylinders[i].volume();
System.out.println("Volume of Cylinder " + (i+1) + " " + volume);
}
}
public static void main(String[] args){
final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };
System.out.println(Arrays.toString(CYLINDERS));
testCylinderSort(CYLINDERS);
}
}
[Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0]
Volume of Cylinder 1 1077.566280181299
Volume of Cylinder 2 1077.566280181299
Volume of Cylinder 3 1077.566280181299
The output shows that I can print the different indexes of the array, but for some reason, they all reference the last element of the array and I can't figure out why that is. If anyone could tell me what's going on here and how I can print all the array objects I'd be very thankful.
You variables declared are static.
You need to remove the static as the static belongs to the class and not a member variable. Thus every time you initialize the values or radius for the cylinders, the following cylinder values always will overwrite the values of the previous ones.
use :
public double radius;
public double height;
public String name;
PS:
You might want to make them private and make public getter and setter methods for better code
Your instance variables are declared as static
.
public static double radius;
public static double height;
public static String name;
Static variables are global for that class.
So when you create three objects
final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };
The radius
, height
and name
variables are continuously replaced. Consequently the values of these variables are the last values set.
new Cylinder(7, 7, "three")
This is what you perceive as the "iteration always returning the last element".
What's actually happening is that different objects are returned, but they have the same set of variables (at class level), instead of having a set of variables per object (at object level).
The fix is to remove the static
keyword.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.