Could you please write this program using java and without t
Could you please write this program using java and without the use of arraylists.
create a method that reads the text file provided.Separate the lines with three numbers into one array, and the lines with only one number into a single array. Then create two separate classes, one with the descriptions of a box(length, height, and width). And the other class named basketBall with only the attribute \"radius\".
Then in class boxType create an array that compares the diameter of basketball(or radius*2) with the length,width and height, of each box to see if its smaller than all three and printing the size of the box and the basketball in order to know what ball fitted in what box.
Your task is to find the smallest box-type that can fit a basketball inside it. The word “smallest” refers to a comparison in terms of the volume of the boxes. Your program must provide the following information as terminal output.
• For each of the basketballs, provide the best box-type (that is, the size of the smallest box that can fit the basketball). If there is no box-type that will be able to fit a basketball, print “Box not available for this basketball”.
• For each of the basketballs, provide the volume of the selected box-type, the volume of the basketball, and the volume of the empty space inside the box after placing the basketball. If your answer was “Box not available for this basketball” then just provide the volume of the basketball.
• How many basketballs were you able to fit in the available box-types?
• Provide a list of radii of the basketballs that do not fit in any box.
• Provide a list of box-types (sizes) that were not used at all.
Requirements:
• You must write and use two classes: BoxType and Basketball. They should be written in two java files: BoxType.java and Basketball.java. The BoxType class will contain the status of the box type (width, height, and length) and other necessary methods. Similarly, the Basketball class will contain the radius of a basketball and other necessary methods.
• You must create all the BoxType and Basketball objects in a separate class named MainProg.java that has the main method.
Your program must create and use an array of BoxType objects and an array of Basketball objects containing the box-types and basketball information provided in the input file.
• You are not allowed to write code to compute the volume of a box-type in the MainProg.java file. You must write the code to compute the volume of a box-type in the BoxType.java file.
The BoxType class must contain the following methods:
double getVolume() : The method will return the volume of boxType array.
boolean fitsInBoxType(Basketball b) : The method will return true if the basketball object b fits in a particular type of box, otherwise it will return false.
Caution: You will need to compare the diameter, not the radius, of the basketball with the width, height, and length of the box.
double emptySpace(Basketball b) : The method will return the volume of the empty space in the box after putting the basketball b in it.
If the basketball does not fit, the method will return the exact volume of the box-type since the basketball cannot occupy any space of the box.
• You are not allowed to write any code to compute the volume of a basketball in the MainProg.java file. You must write it in the Basketball.java file. The Basketball class must contain at least the following method:
double getVolume() : The method will return the volume of the basketball array. Consider that each of the basketball is a sphere with a fixed radius.
Example input file:
2.00 10.00 8.00
4.50 8.45 1.20
8.00 2.50 4.00
0.2 0.1 0.4
3.9 15.00 1.80
6.0 5.0 10.0
2
1
1.9
0.9
2.5
2.49
2.51
3.2
5.0
5.1
2.5
1.2
1.8
2.0
1.85
0.59
Solution
Hi,
Please see below the java classes and output.Please comment for any queries/feedbacks.
Thanks,
Anita
Basketball.java
public class Basketball {
private double radius;
public double getVolume() {
double volume = (4/3) * 3.14 * this.radius * this.radius * this.radius;
return volume;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
BoxType.java
public class BoxType {
private double width;
private double height;
private double length;
public double getVolume() {
return this.width * this.height *this.length;
}
public boolean fitsInBoxType(Basketball b){
double diameter = b.getRadius() *2;
if(diameter<this.width && diameter<this.height && diameter<this.length){
return true;
}
return false;
}
public double emptySpace(Basketball b){
double volumeBall=0;
double emptyVolume =0;
volumeBall = b.getVolume();
if(this.fitsInBoxType(b)){
emptyVolume = this.getVolume() - volumeBall;
}
else{
emptyVolume = this.getVolume();
}
return emptyVolume;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
MainProg.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class MainProg {
public static void main (String [] args){
BufferedReader br = null;
FileReader fr = null;
Basketball [] basketBallArray =new Basketball [16];
BoxType [] boxArray =new BoxType[6];
int noOfBoxes = boxArray.length;
int noOfBalls =basketBallArray.length;
StringTokenizer tokenizer;
int boxCount =0;
int ballCount=0;
BoxType smallestFitbox=null;
double smallestVolume = 0;
int notAvailableCount =0 ;
double [] radiusArr = new double [16] ;
try {
//reading the file named input.txt
fr = new FileReader(\"input.txt\");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
tokenizer = new StringTokenizer(sCurrentLine);
while(tokenizer.hasMoreTokens()){ //Checking if the currentLine has 3 tokens,x
if(3 == tokenizer.countTokens()){
//if yes create a Box object
BoxType box = new BoxType();
box.setWidth(Double.valueOf( tokenizer.nextToken()));
box.setHeight(Double.valueOf( tokenizer.nextToken()));
box.setLength(Double.valueOf( tokenizer.nextToken()));
boxArray[boxCount] =box;
boxCount++;
}
else{
//if no, create a allobject
Basketball ball= new Basketball();
ball.setRadius(Double.valueOf(tokenizer.nextToken()));
basketBallArray[ballCount] = ball;
ballCount ++;
}
}
}
//Looping each basket ball
for(int i =0;i<basketBallArray.length ; i++){
boolean anyBoxAvailable =false;
//looping each box type for the ball
for(int k=0;k<boxArray.length ; k++){
System.out.println(\"Ball :\"+basketBallArray[i].getRadius());
System.out.println(\"Box :\"+\"w: \"+boxArray[k].getWidth()+\" l: \"+boxArray[k].getLength()+\" h: \"+boxArray[k].getHeight());
if(boxArray[k].fitsInBoxType(basketBallArray[i])){ //Checking ig ballfits into box
if(0==smallestVolume){
smallestVolume = boxArray[k].getVolume();
smallestFitbox = boxArray[k];
}
else{
if(smallestVolume > boxArray[k].getVolume()){
smallestVolume = boxArray[k].getVolume();
smallestFitbox = boxArray[k];
}
}
System.out.println(\" Ball fits into Box \");
anyBoxAvailable = true;
}
System.out.println(\"Empty space available : \" + boxArray[k].emptySpace(basketBallArray[i]));
}
if(!anyBoxAvailable){
System.out.println(\"Box not available for this basketball \");
radiusArr [notAvailableCount] = basketBallArray[i].getRadius();//to get the radii of the balls for which no boxes ar eavailable
notAvailableCount = notAvailableCount+1; //to count the balls for which no boxes are available
}
else{
System.out.println(\"Smallest available box is : l = \"+smallestFitbox.getLength()+\" w = \"+smallestFitbox.getWidth()
+\"h = \"+smallestFitbox.getHeight());
}
}
int cnt = basketBallArray.length -notAvailableCount;
System.out.println(\" no of Basketballs that are fit intoany of the boxes :\"+cnt); // printing the no of Basketballs that are fit intoany of the boxes
System.out.println(\"List of radii of the basketballs that do not fit in any box : \"); //printing List of radii of the basketballs that do not fit in any box
for(int j=0;j<radiusArr.length;j++){
if(0 !=radiusArr[j]){
System.out.print(radiusArr[j]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
input.txt
2.00 10.00 8.00
4.50 8.45 1.20
8.00 2.50 4.00
0.2 0.1 0.4
3.9 15.00 1.80
6.0 5.0 10.0
2
1
1.9
0.9
2.5
2.49
2.51
3.2
5.0
5.1
2.5
1.2
1.8
2.0
1.85
0.59
Sample output:
Ball :2.0
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.0
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.0
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.0
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.0
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.0
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 274.88
Smallest available box is : l = 10.0 w = 6.0h = 5.0
Ball :1.0
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :1.0
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :1.0
Box :w: 8.0 l: 4.0 h: 2.5
Ball fits into Box
Empty space available : 76.86
Ball :1.0
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :1.0
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :1.0
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 296.86
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :1.9
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :1.9
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :1.9
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :1.9
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :1.9
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :1.9
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 278.46274
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :0.9
Box :w: 2.0 l: 8.0 h: 10.0
Ball fits into Box
Empty space available : 157.71094
Ball :0.9
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :0.9
Box :w: 8.0 l: 4.0 h: 2.5
Ball fits into Box
Empty space available : 77.71094
Ball :0.9
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :0.9
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :0.9
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 297.71094
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :2.5
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.5
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.5
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.5
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.5
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.5
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :2.49
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.49
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.49
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.49
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.49
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.49
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 251.52389813999997
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :2.51
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.51
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.51
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.51
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.51
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.51
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :3.2
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :3.2
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :3.2
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :3.2
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :3.2
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :3.2
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :5.0
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :5.0
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :5.0
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :5.0
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :5.0
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :5.0
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :5.1
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :5.1
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :5.1
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :5.1
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :5.1
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :5.1
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :2.5
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.5
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.5
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.5
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.5
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.5
Box :w: 6.0 l: 10.0 h: 5.0
Empty space available : 300.0
Box not available for this basketball
Ball :1.2
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :1.2
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :1.2
Box :w: 8.0 l: 4.0 h: 2.5
Ball fits into Box
Empty space available : 74.57408
Ball :1.2
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :1.2
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :1.2
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 294.57408
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :1.8
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :1.8
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :1.8
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :1.8
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :1.8
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :1.8
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 281.68752
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :2.0
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :2.0
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :2.0
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :2.0
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :2.0
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :2.0
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 274.88
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :1.85
Box :w: 2.0 l: 8.0 h: 10.0
Empty space available : 160.0
Ball :1.85
Box :w: 4.5 l: 1.2 h: 8.45
Empty space available : 45.629999999999995
Ball :1.85
Box :w: 8.0 l: 4.0 h: 2.5
Empty space available : 80.0
Ball :1.85
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :1.85
Box :w: 3.9 l: 1.8 h: 15.0
Empty space available : 105.3
Ball :1.85
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 280.1186975
Smallest available box is : l = 4.0 w = 8.0h = 2.5
Ball :0.59
Box :w: 2.0 l: 8.0 h: 10.0
Ball fits into Box
Empty space available : 159.35510994
Ball :0.59
Box :w: 4.5 l: 1.2 h: 8.45
Ball fits into Box
Empty space available : 44.985109939999994
Ball :0.59
Box :w: 8.0 l: 4.0 h: 2.5
Ball fits into Box
Empty space available : 79.35510994
Ball :0.59
Box :w: 0.2 l: 0.4 h: 0.1
Empty space available : 0.008000000000000002
Ball :0.59
Box :w: 3.9 l: 1.8 h: 15.0
Ball fits into Box
Empty space available : 104.65510994
Ball :0.59
Box :w: 6.0 l: 10.0 h: 5.0
Ball fits into Box
Empty space available : 299.35510994
Smallest available box is : l = 1.2 w = 4.5h = 8.45
no of Basketballs that werenot fit intoany of the boxes :10
List of radii of the basketballs that do not fit in any box :
2.52.513.25.05.12.5










