2

So for my assignment it needs to look like this: Assignment

The problem I am facing with this is the setIndent isn't setting the indent but when I mainly change int indent = 20; it will add the indent to the boxes. Also I am confused as to why Rectangle@6bdf28bb is appearing in my code.

Here is my code for the assignment.

Client

// Use this client to help test your Rectangle class for Lab13
// Download it into the same folder as your Rectangle.java file.
// Add other tests if you want to.
public class RectangleClient {
  public static void main( String[] args ) {

     Rectangle box1 = new Rectangle( 4, 5 );
     box1.setIndent(-1);

     System.out.println( box1 );
     Rectangle box2 = new Rectangle( 6, 12, '+', 'X' );
     box2.setIndent( 5 );
     System.out.println( box2 );
     Rectangle box3 = new Rectangle( 11, 20, '$', 'o' );
     box3.setIndent( 20 );
     System.out.println( box3 );
  }
}

Class

//Using rectangle class to test

public class Rectangle {
    public double length;
    public double width;
    public char fill = ' ';
    public char pen = '*';
    public int indent;

    //Set variables
    public void setLength(double len){
        if (len <= 0){
            throw new IllegalArgumentException("Invalid length for Rectangle object");
        }
        else{
            length = len;
        }
    }
    public void setWidth(double wid){
        if (wid <=0){
            throw new IllegalArgumentException("Invalid width for Rectangle object");
        }
        else{
            width = wid;
        }
    }
    public void setPen(char c){
        pen = c;
    }
    public void setFill(char c){
        fill = c;
    }
    public void setIndent(int n){
        if (n < 0){
            indent = 0;
        }
        else {
            indent = n;
        }
    }
    //Get variables
    public double getLength(){
        return length;
    }
    public double getWidth(){
        return width;
    }
    public double getIndent(){
        return indent;
    }

    //Main method
    public Rectangle (){
        int count = 0;
        String indents = "";
        String topBottom = "";
        String middle = "";
        String line = "";
        //Creates the indent string
        while (count<indent){
            indents = indents + " ";
            count++;
        }
        //Top boarder and bottom one
        count = 0;
        while (count<width){
            topBottom += pen;
            count++;
        }
        //Fill inside square
        width = width - 2;
        count = 0;
        while (count<width){
            middle += fill;
            count++;
        }
        //Prints square
        line = pen + middle + pen;
        count = 0;
        while (count<length){
            if (count == 0 || count == length - 1){
                System.out.println(indents + topBottom);
                count++;
            }
            else{
                System.out.println(indents + line);
                count++;
            }
        }
    }
    // using default or set fill and boarder      
    public Rectangle (double l, double w){
        int count = 0;
        String indents = "";
        String topBottom = "";
        String middle = "";
        String line = "";
        //Creates the indent string
        while (count<indent){
            indents = indents + " ";
            count++;
        }
        //Top boarder and bottom one
        count = 0;
        while (count<w){
            topBottom += pen;
            count++;
        }
        //Fill inside square
        w = w - 2;
        count = 0;
        while (count<w){
            middle += fill;
            count++;
        }
        //Prints square
        line = pen + middle + pen;
        count = 0;
        while (count<l){
            if (count == 0 || count == l - 1){
                System.out.println(indents + topBottom);
                count++;
            }
            else{
                System.out.println(indents + line);
                count++;
            }
        }
    }
    //To set values without using .setWidth etc  
    public Rectangle (double l, double w, char p, char f){
        int count = 0;
        String indents = "";
        String topBottom = "";
        String middle = "";
        String line = "";
        //Creates the indent string
        while (count<indent){
            indents += " ";
            count++;
        }
        //Top boarder and bottom one
        count = 0;
        while (count<w){
            topBottom += p;
            count++;
        }
        //Fill inside square
        w = w - 2;
        count = 0;
        while (count<w){
            middle += f;
            count++;
        }
        //Prints square
        line = indents + p + middle + p;
        topBottom = indents + topBottom;
        count = 0;
        while (count<l){
            if (count == 0 || count == l - 1){
                System.out.println(topBottom);
                count++;
            }
            else{
                System.out.println(line);
                count++;
            }
        }
    }
}

The error I'm getting is that it is not adding the indents and the random Rectangle@2ff4f00f

Error picture

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
  • 4
    *im confused as to why Rectangle@6bdf28bb* see https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Scary Wombat Apr 03 '18 at 05:51
  • Move your printing code to a method outside of your constructor and call it after you have set your variables – Scary Wombat Apr 03 '18 at 05:53
  • @ScaryWombat Thank you i was able to remove the Rectangle@6bdf28bb. if you are talking about this right here. Rectangle box3 = new Rectangle( 11, 20, '$', 'o' ); box3.setIndent( 20 ); for the method thats how my instructor had it and we had to match it up. unless he entered it wrong? – Jake Waggerby Apr 03 '18 at 06:08
  • No, the problem is that when you are calling `new Rectangle (....)` it is already printing the output. – Scary Wombat Apr 03 '18 at 06:14
  • What Scary Wombat is referring to, is, that you actually print your box to the console _before_ setting the indent value. Whenever you call `new Rectangle(...)`, you print the visual representation of the box to console, as the logic for the printing is _inside_ the constructor of Rectangle. So, move the logic that _prints_ anything to a method _outside_ of the Rectangle constructor, and then call it from your RectangleClient _after_ you've used `#setIndent`. – Kristin Apr 03 '18 at 06:14

1 Answers1

3

You want to move the printing process of your Rectangle outside of the constructor, otherwise it'll always print immediately upon the use of new Rectangle(...), before you're able to use Rectangle#setIndent(int).

You should instead set the Rectangle fields' values with your constructor, and then have a separate method for printing the Rectangle.

For instance, your constructor which is used to define a specific Rectangle with a custom width, length, pen and fill:

public Rectangle(double l, double w, char p, char f) {
    this.length = l;
    this.width = w;
    this.pen = p;
    this.fill = f;
}

This would set the Rectangle instance's fields to the values parsed as arguments when using new Rectangle(...). (Note, you might want to redo your other constructors to comply with this as well).

To visualize it, you could try add the following code to your Rectangle class

@Override
public String toString() {
    return getClass().getSimpleName() + "[w: " + width + "; l: " + length + 
            "; p: " + pen + "; f: " + fill + "; indent: " + indent + "]";
}

And then use the following in your RectangleClient

Rectangle box1 = new Rectangle(4, 5, '+', 'X');
System.out.println(box1);
box1.setIndent(50);
System.out.println(box1);

It should print

Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 0]
Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 50]

Since we removed the logic for printing the box from the constructor, we should add it somewhere else. With a separate method for printing the Rectangle, you could do something similar to

public void printRectangle() {
    int count = 0;
    String indents = "";
    String topBottom = "";
    String middle = "";
    String line = "";
    // Creates the indent string
    while (count < indent) {
        indents += " ";
        count++;
    }
    // Top boarder and bottom one
    count = 0;
    while (count < this.width) {
        topBottom += this.pen;
        count++;
    }
    // Fill inside square
    this.width = this.width - 2;
    count = 0;
    while (count < this.width) {
        middle += this.fill;
        count++;
    }
    // Prints square
    line = indents + this.pen + middle + this.pen;
    topBottom = indents + topBottom;
    count = 0;
    while (count < this.length) {
        if (count == 0 || count == this.length - 1) {
            System.out.println(topBottom);
            count++;
        } else {
            System.out.println(line);
            count++;
        }
    }
}

This is basically just the logic from your constructor, just with the exception that instead of using the local variables (passed as arguments), we use the Rectangle instance's field values instead (e.g. this.width instead of w).

Maybe your instructor explicitly wanted you to override the #toString() method, and inside the overridden method, you'd have your logic for printing the Rectangle. If that is the case, you'd of course just move the logic from the #printRectangle() to the overridden #toString() method, which would allow you to use System.out.println(box1) (replace the previous sampled #toString() method, of course).

@Override
public String toString() {
    // Logic from #printRectangle() here
}

If you choose to override #toString(), you should not use System.out.println in the logic, but rather build a string, which you'll return at the end of the #toString() logic. You could take a look at StringBuilder for this.

Kristin
  • 1,336
  • 7
  • 23
  • 1
    you just explained the toString() method better than my instructor did for me. Thank you so much – Jake Waggerby Apr 03 '18 at 07:14
  • Explaining `toString()` is hard... there are so many use cases and other ramifications. I'm still learning new stuff after many years. This is a good practical explanation. – JonathanDavidArndt Apr 12 '18 at 12:33