设置java无法正常工作的方法

时间:2018-04-03 05:49:30

标签: java class methods

因此,对于我的作业,它需要如下所示:Assignment

我面临的问题是setIndent没有设置缩进,但是当我主要更改int indent = 20;时,它会将缩进添加到框中。另外,我很困惑为什么Rectangle@6bdf28bb出现在我的代码中。

这是我的作业代码。

客户端

// 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 );
  }
}

//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++;
            }
        }
    }
}

我得到的错误是它没有添加缩进和随机Rectangle@2ff4f00f

Error picture

1 个答案:

答案 0 :(得分:3)

您希望将Rectangle的打印过程移到构造函数之外,否则在使用new Rectangle(...)之后,它将始终立即打印,然后才能使用Rectangle#setIndent(int) }。

您应该使用构造函数设置Rectangle字段'值,然后使用单独的方法打印Rectangle

例如,您的构造函数用于定义具有自定义宽度,长度,笔和填充的特定Rectangle

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

这会将Rectangle实例的字段设置为使用new Rectangle(...)时解析为参数的值。 (注意,您可能希望重做其他构造函数以符合这一点。)

要想象它,您可以尝试将以下代码添加到Rectangle

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

然后在RectangleClient

中使用以下内容
Rectangle box1 = new Rectangle(4, 5, '+', 'X');
System.out.println(box1);
box1.setIndent(50);
System.out.println(box1);

应打印

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

由于我们从构造函数中删除了打印框的逻辑,因此我们应该将其添加到其他位置。使用单独的方法打印Rectangle,您可以执行与

类似的操作
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++;
        }
    }
}

这基本上只是构造函数的逻辑,只是除了使用局部变量(作为参数传递)之外,我们使用Rectangle实例的字段值代替(例如this.width而不是{{1 }})。

也许您的教师明确要求您覆盖w方法,并且在重写方法中,您将拥有打印#toString()的逻辑。如果是这种情况,您当然只需将逻辑从Rectangle移动到重写的#printRectangle()方法,这样就可以使用#toString()(替换之前的采样{{} 1}}方法,当然)。

System.out.println(box1)

如果您选择覆盖#toString(),则不应在逻辑中使用@Override public String toString() { // Logic from #printRectangle() here } ,而是构建一个字符串,您将在#toString()逻辑的末尾返回该字符串。你可以看一下StringBuilder