Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Java Assignment ( Help )

The question is : Write a method for a class called BallPacker that prompts for three ints (one at a time) representing the dimensions of a box and a float that represents the radius of a ball. Then compute the number of balls that would fit in the box. Display the results in the Java console. Be sure to test your code with many different box and ball sizes. Assume that the balls are only stacked on top of each other neatly (shown bellow).

So far I have this :

import java.util.*;

public class BallPacker {
public static void main(String[] args) {

Scanner myscanner = new Scanner (System.in);
int h, w, l;
float r;
double x, d, k, p;

int boxVolume;
double sphereVol;

int counter = 0;
double dec;

System.out.print("length (l): ");
l = myscanner.nextInt();

System.out.print("width (w): ");
w = myscanner.nextInt();

System.out.print("height (h): ");
h = myscanner.nextInt();

System.out.print("Ball Radius (r): ");
r = myscanner.nextFloat();


boxVolume = l*w*h;
//System.out.println("VOLUME: " + boxVolume);

k = 1.33;
p = 3.14;
d = Math.pow(r,3);

sphereVol = k*p*d;
x = Math.ceil(sphereVol);

System.out.println("Processing ...");
System.out.println("Might take few minutes depending on the entered dimensions.");
//while (((boxVolume - x) != 0) || ((boxVolume - x) < 0)){

if(boxVolume>(boxVolume-x)){
counter++;
}

/*if((boxVolume - x)>0){
counter++;
}*/
else{
System.out.println("COUNTING PROCESS COMPLETE!");
// break;
}
// }

System.out.println(counter + " balls can be placed inside that box you specified");
}
}

But it doessn't really work...
Can some 1 help me? edit the code?
Thanks
[2017 byte] By [lookingat120] at [2007-11-11 8:50:26]
# 1 Re: Java Assignment ( Help )
We're not here to do your homework for you. We'll help, IF you ask a specific question. What's not working? Does it compile? Is the output different than expect? what?
Phaelax at 2007-11-11 22:34:13 >