not sure if my code is right, if it is not sure how to code Test box.
first, this is my homework or project for class. I don't ask anyone to do it for me, I just want help, not sure I really understand what I'm doing.
I have to make a class, Box, that accepts 3 ints for length, width and height. 3 constructors for 1, 2 or 3 ints. if 1 output a line with length x was created, if 2 a rectangle with lengths and width y created, etc. I was just wondering if I did this right, I'm stuck trying to write BoxTest to check that it works, but here is what I have so far.
Thank you
//Unit 5
//IT258
//Christian Silva
//I attest that this is my work
//This is the beginning of the Box class
public class Box
{
private int length;
private int width;
private int height;
public static void Box(int x)
{
int length = x;
int width = 0;
int height = 0;
System.out.print("Line created");
}
public static void Box(int x, int y)
{
int length = x;
int width = y;
int height = 0;
System.out.print("Rectangle created");
}
public static void Box(int x, int y, int z)
{
int length = x;
int width = y;
int height = z;
System.out.print("Box created");
}
}
Re: not sure if my code is right, if it is not sure how to code Test box.
When posting code, please use the highlight tags to preserve formatting.
Does this compile? Can you call its methods? Do the methods print out what you expect them to?
Re: not sure if my code is right, if it is not sure how to code Test box.
it does compile, I'm not sure because I'm really confused with java and i don't think I even need those println statements. this class defines box and the test box i need to write creates 3 instances, 1 for each constructor
Re: not sure if my code is right, if it is not sure how to code Test box.
I don't see any constructors. You have 3 static functions with a return type of void called Box, but no constructors.
Recommended reading: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)