0

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
kahlzun
  • 11
  • 2
  • 5
    You can define multiple constructs with different sets of arguments. So in addition to the one you already have, define one that takes only 3 int values (and it's easily implemented by using `this(...)` to delegate to the other constructor). – Joachim Sauer Mar 14 '22 at 13:00
  • that indeed did the trick. Usually the system throws errors if you have multiple of the same thing, so i didnt even think of this. Thanks much! – kahlzun Mar 14 '22 at 13:03
  • @JoachimSauer Hey, is there a reason for answering in a comment instead of an answer? I see many people do this and I find it annoying since the question can not be marked as answered, and if someone comes with the same question they might not notice your comment and miss what they were here for... – Corentin Colas des Francs Mar 14 '22 at 13:16
  • @CorentinColasdesFrancs: probably not a **good** reason, but for me it's that I don't have time to find the proper duplicate that this should be closed as (as I'm certain there's already a good answer to pretty much exactly this question somewhere on the site) and didn't feel like just moving on without offering *anything*. Finding the duplicate would have been the correct action here (as the duplicate would likely also provide information about all the possible follow-up questions that might pop up). – Joachim Sauer Mar 14 '22 at 13:23

0 Answers0