Write a program that fill two arrays rad[10] and len[10] by random generator (nu
ID: 3546421 • Letter: W
Question
Write a program that fill two arrays rad[10] and len[10] by random generator (numbers between 0 and 100). rad[] array represents the (radius) of Cylinder, and array len[] represents length of the cylinder. Then using the two arrays, compute the volume of the cylinder and put the result in another array vol[10]. Then find the maximum volume and print it and its radius and length.
Sample Run:
The radiuses are: 2 99 29 4 1 78 23 56 3 6
The lengths are: 2 3 2 9 6 5 4 2 6 55
The volumes are: 25.12 92325.42 5281.48 452.16 18.84
95518.80 6644.24 19694.08 169.56 6217.20
Explanation / Answer
import java.util.Random;
public class cylinder {
static float vol(int rad, int len){
return (float)3.14*rad*rad*len;
}
public static void main(String[] args) {
int rad[]=new int[10];
int len[]=new int[10];
float vol[] = new float[10];
float max=0;
int maxrad=0;
int maxlen=0;
Random rand = new Random();
for(int i=0;i<10;i++){
rad[i] = rand.nextInt(100);
len[i] = rand.nextInt(100);
vol[i] = vol(rad[i],len[i]);
if(vol[i]>max){
max=vol[i];
maxrad=rad[i];
maxlen=len[i];
}
}
System.out.print("The radiuses are:");
for(int i:rad){
System.out.print(" "+i);
}
System.out.print(" ");
System.out.print("The lengths are:");
for(int i:len){
System.out.print(" "+i);
}
System.out.print(" ");
System.out.print("The volumes are:");
for(float i:vol){
System.out.print(" "+i);
}
System.out.print(" ");
System.out.println("Maximum volume is= " + max + "and its radius=" +maxrad + ",length="+maxlen);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.