Reputation:
I have to ask the user to input the dimensions of two boxes then calulate the surface area and volume of two boxes. I am done with everything but i keep getting the error: constructor Box in class Box cannot be applied to given types:
My main class BoxCalc:
package boxcalc;
import cs1.Keyboard;
public class BoxCalc
{
public static void main(String[] args)
{
int width, length, height, volume1, volume2, surfacearea1, surfacearea2;
Box b1 = new Box();
Box b2 = new Box();
b1.getClass();
b2.getClass();
// Ask the user the demensions for Box 1.
System.out.println ("Box 1");
System.out.print ("Enter a value for the height of Box 1: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 1: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 1: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 1.
volume1 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Ask the user the demensions for Box 2.
System.out.println ("Box 2");
System.out.print ("Enter a value for the height of Box 2: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 2: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 2: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 2.
volume2 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Determain which Box has greater volume.
if (volume1 > volume2)
{
System.out.println("Box 1's volume is greater!");
}
else
if(volume2 > volume1)
{
System.out.println("Box 2's volume is greater!");
}
if(volume1 == volume2)
{
System.out.println("Box 1's volume is same as Box 2's!");
}
// Display all the information.
System.out.println();
System.out.println(b1);
System.out.println();
System.out.println(b2);
}
}
My class Box:
package boxcalc;
public class Box
{
private int width, length, height, volume, surfacearea;
// Defining the width, length, and height.
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
// Method to calulate the volume.
public void findVolume(int volume)
{
volume = length * height * width;
}
// Method to calulate the surface area.
public void findSurfaceArea(int surfacearea)
{
surfacearea = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
}
// Use toString method to display all the info above.
public String toString()
{
return ("Length: " + length + "\n" + "Width: " + width + "\n" +
"Height: " + height + "\n" + "Volume: " + volume +"\n" +
"Surface area: " + surfacearea + "\n");
}
}
Upvotes: 0
Views: 1992
Reputation: 5092
your box constructor:
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
but you creating box object with no parameter:
Box b1 = new Box();
Box b2 = new Box();
it should be:
after you ask user for the dimension of box 1, put this:
Box b1 = new Box(width, length, height);
after you ask user for the dimension of box 2, put this:
Box b2 = new Box(width, length, height);
Upvotes: 2
Reputation: 285403
You are calling the Box constructor with no parameters: Box b1 = new Box();
, and in the class itself it is defined as requiring to have multiple (three) numeric parameters: public Box (int boxwidth, int boxlength, int boxheight)
. So the compiler is rightfully complaining that something is amiss.
Upvotes: 1
Reputation: 62052
As it stands, your program isn't object-oriented in the slightest.
There was no point in posting your Box
class, as your main program doesn't make use of it in any significant way.
You're determining which box is bigger in a structured programming manner, by declaring enough variables in main to do all of the logic.
You should get all the dimensions for box1
, then do this:
Box b1 = new Box(width, height, length);
Then do the same for box2
, and again:
Box b2 = new Box(width,height,length);
Then, in your if
statements, instead of volume1 > volume2
, you should instead be doing something more like this:
if(b1.getVolume() > b2.getVolume())
Where getVolume()
should be a method of your Box
class that looks like this:
public int getVolume() {
return volume;
}
(Although, best practices might say don't even include a volume
variable in the Box
class, and simple do return (width * height * length);
The same principles should be applied to other aspects of your program.
As a note, by doing it this way, your main program only needs 3 int
variables: width
, height
, length
. Once you send b1
's dimension into the constructor, the b1
variables knows its dimensions, and you can reuse these variables for taking in b2
's dimensions. You don't need volume1
, volume2
, surfacearea1
, or surfacearea2
variables, because a complete Box
class would allow you to get these Box
properties via getVolume()
and getSurfaceArea()
.
Upvotes: 1