Question: Why wont this code compile for java? //1.Main import java.io.*; import java.util.*; public class MainProg { public static ArrayList boxes = new ArrayList (); public
Why wont this code compile for java?
//1.Main
import java.io.*;
import java.util.*;
public class MainProg {
public static ArrayList
public static ArrayList
public static void main(String[] args){
File inputFile = new File("inputFile.txt");
Scanner sc;
try {
sc = new Scanner(inputFile);
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist. Kindly create file");
return;
}
while(sc.hasNextLine()){
String inputLine = sc.nextLine();
String [] inputVals = inputLine.split(" ");
/*BoxType dimensions*/
if(inputVals.length==3){
BoxType box = new BoxType();
box.setWidth(Double.parseDouble(inputVals[0]));
box.setHeigth(Double.parseDouble(inputVals[1]));
box.setLength(Double.parseDouble(inputVals[2]));
boxes.add(box);
}
/*BasketBall radius*/
else{
BasketBall bb = new BasketBall();
bb.setRadius(Double.parseDouble(inputVals[0]));
basketBalls.add(bb);
}
}
/*Traversing through boxes and basketballs*/
int numberOfBasketBallAbleToFit = 0;
ArrayList
for(BasketBall bb: basketBalls){
int idx = 0;
int counter=0;
for(BoxType bt:boxes){
if(bt.fitsInBoxType(bb)){
bb.setFitInAnyBox(true);
BoxType boxAtIdx = boxes.get(idx);
if(boxAtIdx.getVolume()>bt.getVolume())
idx=counter;
}
counter+=1;
}
BoxType b = boxes.get(idx);
if(bb.isFitInAnyBox()){
numberOfBasketBallAbleToFit+=1;
b.incrementNumberOfBasketBall();
System.out.println("The sizes of the smallest suitable box, width: "+b.getWidth()+" height: "+b.getHeigth()+" length: "+b.getLength());
System.out.println("Vol of BoxType: "+b.getVolume()+" Vol of BasketBall: "+bb.getVolume()+" EmptySpaces: "+b.emptySpace(bb));
}
else{
radiiList.add(bb.getRadius());
System.out.println("Box not available for this basketball");
System.out.println("The vol of the basketBall is: "+bb.getVolume());
}
}
System.out.println("The total number of basketballs that were able to fit in the available boxes are: "+numberOfBasketBallAbleToFit);
System.out.println("The following radius of basketballs were not being able to fit in any box");
for(Double item: radiiList){
System.out.print(item+" ");
}
System.out.println();
System.out.println("The boxes which were not used at all");
for(BoxType b: boxes){
if(b.getNumberOfBasketballAcquired()==0){
System.out.println("width: "+b.getWidth()+" height: "+b.getHeigth()+" length: "+b.getLength());
}
}
sc.close();
}
}
//2. Basketball
public class BasketBall {
private final static double PI = 3.14;
private double radius;
private boolean isFitInAnyBox = false;
public boolean isFitInAnyBox() {
return isFitInAnyBox;
}
public void setFitInAnyBox(boolean isFitInAnyBox) {
this.isFitInAnyBox = isFitInAnyBox;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getVolume(){
return PI * this.radius * this.radius * this.radius;
}
}
//3. BoxType
public class BoxType {
private double width;
private double heigth;
private double length;
private int numberOfBasketballAcquired=0;
/*Setters and getters of the private fields*/
public BoxType(){
}
public BoxType(double width, double height, double length){
this.width = width;
this.heigth = height;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeigth() {
return heigth;
}
public void setHeigth(double heigth) {
this.heigth = heigth;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public int getNumberOfBasketballAcquired() {
return numberOfBasketballAcquired;
}
public void setNumberOfBasketballAcquired(int numberOfBasketballAcquired) {
this.numberOfBasketballAcquired = numberOfBasketballAcquired;
}
public double getVolume(){
return this.width * this.heigth * this.length;
}
public double emptySpace(BasketBall bb){
if(this.fitsInBoxType(bb))
return this.getVolume() - bb.getVolume();
else
return this.getVolume();
}
public boolean fitsInBoxType(BasketBall bb){
double r = bb.getRadius();
double d = 2.000000*r;
if(this.width >= d && this.heigth >= d && this.length >= d){
return true;
}
return false;
}
public void incrementNumberOfBasketBall(){
numberOfBasketballAcquired+=1;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
