java - java的设置方法不起作用

标签 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

最佳答案

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

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

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

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]

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

的操作
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而不是 w)。

也许您的讲师明确希望您重写#toString() 方法,并且在重写的方法内,您将拥有打印矩形 的逻辑。如果是这种情况,您当然只需将逻辑从 #printRectangle() 移动到重写的 #toString() 方法,这将允许您使用System.out.println(box1)(当然,替换之前采样的#toString()方法)。

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

如果您选择覆盖#toString(),则不应在逻辑中使用System.out.println,而应构建一个字符串,您将使用该字符串在 #toString() 逻辑末尾返回。你可以看看StringBuilder为此。

关于java - java的设置方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49623084/

相关文章:

c++ - 从成员变量中借用值?

r - 有没有办法运行包含在字符串对象(在 R 中)中的代码?

Javascript:在通过其他属性选择的 DOM 对象上运行对象方法

java - 查找整数数组中的最大数字

java - 使用正则表达式将字符串数据的键和值格式化为 json 键值对

java - 用于收集和排序的Jar推荐(合并排序)

java - 使用线程对象作为 HashMap 中的键

java - 如何提取自定义格式的JSON数据?

c# - 声明 C# 属性

javascript - 调用mootools类中的方法