Confused? Need help 04-03-2018, 03:52 AM
#1
So for my assignment it needs to look like this
![[Image: 6ega0wo.jpg]](https://i.imgur.com/6ega0wo.jpg)
heres my code for the assignment
Client:
And then the class
the error im getting is its not adding the indents and the random Rectangle@2ff4f00f
![[Image: 6ega0wo.jpg]](https://i.imgur.com/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 );
}
}
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]](https://i.imgur.com/AxKhZXs.png)