I am working on learning Java, and have been given an assignment to write a class which recieves a set of variables from another class, and spits out a formatted text. The example file gives the variables twice.
I have figured out how to do most of this independently, however there is one component which eludes me. In the example as mentioned the (call?) is repeated twice, however the first time it is missing a variable which is in the second one:
Box a = new Box(width, height, depth);
Box 2 = new Box(width, height, depth, builder);
if i build it to expect the three ints, it throws an error about the string. If i build it to include the string, it throws an error:
"Box(int, int, int, java.lang.String) in 'Box' cannot be applied to '(int, int, int)'
In the example output, it lists the first one as outputting "null" for the builder section.
I do not know how to make it accept both inputs, and am open to any suggestions. Output code:
int width = 33;
int height = 28;
int depth = 58;
String builder = "Lori ";
Box a = new Box(width,height,depth);
System.out.println(a.printMe());
Box b = new Box(width,height, depth,builder);
System.out.println(b.printMe());
my code:
public class Box {
private int width_x;
private int height_y;
private int depth_z;
private String name;
// constructor?
public Box(int width,int height,int depth,String builder) {
width_x = width;
height_y = height;
depth_z = depth;
name = builder;
// output test
System.out.println(width+" "+height+" "+depth+builder);
System.out.println(width_x +" "+ height_y +" "+ depth_z +name);
}
public String printMe() {
System.out.println(width_x +" "+ height_y +" "+ depth_z +name);
return ("Width: "+ width_x +"\n Height: "+ height_y +"\n Depth: "+ depth_z +"\n Built by "+name);
}
}
And the expected output is:
Width: 33
Height: 28
Depth: 58
Built by null
Width: 33
Height: 28
Depth: 58
Built by Lori