Login Register






Confused? Need help filter_list
Author
Message
Confused? Need help #1
So for my assignment it needs to look like this
[Image: 6ega0wo.jpg]

heres my code for the assignment
Client:
Spoiler:
Code:
// 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 ) { // to make sure validation is working, // try each of the following commented statements, one at a time //Rectangle crash1 = new Rectangle( 0, 5 ); //Rectangle crash1 = new Rectangle( 4, 0 ); // Rectangle crash1 = new Rectangle( 0, 5, 'O', '^' ); // Rectangle crash1 = new Rectangle( 4, 0, 'O', '^' ); Rectangle box1 = new Rectangle( 4, 5 ); box1.setIndent(-1); //box1.setLength(0); //box1.setWidth(0); 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 ); } }

And then the class
Spoiler:
Code:
//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 im getting is its not adding the indents and the random Rectangle@2ff4f00f
[Image: AxKhZXs.png]

Reply

RE: Confused? Need help #2
I figured out the answer to my question for anyone that was wondering I forgot the toString() method and moved the printing process in the toString() logic
code:
Spoiler:
Code:
public String toString(){ 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++; } } return name; }

Reply