I have a program assignment in class. I already understand the basics of overloading, but at some point I'm completely confused. How can I only infer from the method I'm trying to use? Ok, let me show you the code, than explain.
public class Box {
private int length, width, height;
public Box(int length){
this.length=length;
System.out.println("Line created with length of" + length + ".");
}
public Box(int length, int width){
this.length = length;
this.width = width;
System.out.println("Rectangle created with the length of " + length + " ");
System.out.println("and the width of " + width + ".");
}
public Box(int length, int width, int height){
this.length=length;
this.width=width;
this.height=height;
System.out.println("Box created with the length of " + length + ", ");
System.out.println("the width of " + width + ", ");
System.out.println("and the height of " + height +".");
}
}
class BoxTest {
public static void main(String[] args) {
Box BoxObject1 = new Box(1,0,0);
Box BoxObject2 = new Box(1,2,0);
Box BoxObject3 = new Box(1,2,3);
}
}
Ok now! How can I call the BoxTest class to output only what is given. For example, using Box BoxObject1, I want to output "Line created with length XX", and not the rest. For Box Box Object2, I want to output "A rectangle created with length XX and width XX". I am not sure what to add next for this. Any help would be greatly appreciated.
source
share