Issue
I am trying to compare objects from an array to find out which object has the greatest volume. I have done this but when I run it it does an infinite loop and it only prints the information about the first line when it should be the 4th line cause that's the one with the greatest volume
Update: I fixed the printing of just the first line and looping but it is still printing tons of them and its repeating, all I want is for the 4th line info to print I am not sure why it's printing many of all of them. I also just realized it's printing all of the lines info 4 times for each
}
for (int j = 0; j < arr.length; j++){
for (int n = 0; n < arr.length; i++){
if (arr[j].compareTo(arr[n]) > 0){
System.out.println("The width is " + array[j].width();
System.out.println("The height is " + array[j].height();
System.out.println("The length of is " + array[j].length();
System.out.println("The volume of the largest box is "+ arr[j].getVol());
}
}
}
Solution
In both of your for loops you never advance the j
or n
variables. This for (int j = 0; j < arr.length; i++)
should be for (int j = 0; j < arr.length; j++)
note the last variable change.
The reason you get the first values printed over and over is because you use array[0]
, array[1]
, and array[2]
in your print statement every iteration of the loop. You should instead of specifically be using arr[j].getWidth()
, arr[j].getHeight()
, and arr[j].getLength()
, note how we use arr[j]...
not a fixed number arr[0]
.
Also, using nested loops is not a great way to compare items. This would work just fine by looping through the array once:
//Sample values for testing
Box[] arr = new Box[] {new Box(10, 11, 12), new Box(10, 9, 12), new Box(10, 11, 8), new Box(11, 11, 6), new Box(17, 10, 5)};
//Set biggest volume to first element in the array
Box biggestVolume = arr[0];
//iterate the array
for (int j = 0; j < arr.length; j++)
{
if (arr[j].compareTo(biggestVolume) > 0)
biggestVolume = arr[j];
}
System.out.println("The largest box width is " + biggestVolume.getWidth());
System.out.println("The largest box height is " + biggestVolume.getHeight());
System.out.println("The largest box length of is " + biggestVolume.getLength());
System.out.println("The volume of the largest box is " + biggestVolume.getVol());
And the class remains largely unchanged other than some getter methods:
//class
public class Box implements Comparable
{
private double length;
private double height;
private double width;
Box(double l, double h, double w){
length = l;
height = h;
width = w;
}
double getVol(){
return width * height * length;}
double getLength(){
return length;}
double getHeight(){
return height;}
double getWidth(){
return width;}
@Override
public int compareTo(Object o){
Box b = (Box) o;
double s = getVol();
double s2 = b.getVol();
if (s < s2){
return -1;
}
if (s > s2){
return 1;
}
else{
return 0;
}
}
}
Edited answer to use your compareTo method.
Answered By - sorifiend
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.