在 Java 中构建矩形的实验

Lab constructing a rectangle in Java

我是编程新手,最近开始攻读计算机科学学位。由于我儿子生病,我昨天错过了 CS-140/Java 的实验,我在家里完成实验作业时遇到了一些麻烦。

  1. 有人告诉我构造一个名为 box 的空矩形。

  2. 找出如何将框的宽度更改为 50,将其高度更改为 60,将左上角更改为 [100,50]。 (您需要在 box 上调用适当的方法来实现此目的。)

  3. 找出如何计算方框的面积。您必须通过调用适当的方法来计算面积来获取框的高度和宽度。

  4. 然后打印通过在 box 上调用方法返回的值,然后我们打印一条描述我们期望看到的值的消息。

我已经通过堆栈交换进行了搜索并找到了一些有用的信息,但我仍然无法找出代码中的错误以及为什么它不起作用。我假设 63 个错误来自开头的大括号之类的东西,要么是缺少大括号,要么是大括号太多。

我有 63 个错误,到目前为止我输入的代码如下:

 import java.awt.Rectangle;
public class Rectangle {
    public double x;
    public double y;
    public double width, height;

    public Rectangle(double x, double y, double w, double h) {{
           this.x = x;
           this.y = y;
           width = w;
           height = h;
   }
   public static void main(String[] args) {
       Rectangle box = new Rectangle (100, 50, 50, 60);
       System.out.println(box);
    }
       System.out.println("Part 1:");
       System.out.println("-------");
       System.out.println("CREATING AN EMPTY Rectangle, SETTING ITS width TO 50, " +
                          "ITS height TO 60, AND ITS TOP LEFT CORNER TO (100, 50)");
       // Code for Part 1 goes here


        }

       System.out.println("Part 2:");
       System.out.println("-------");
       System.out.println("TESTING THE PERIMETER OF THE RECTANGLE "
                        + "CREATED IN PART 1 ABOVE");
       // Code for Part 2 goes here.  Use the same Rectangle
       // you used Part1

       // define a String reference called river and initialize it
       // to Mississippi.  Read the API to figure out which method 
       // to use to get the desired effect.
       System.out.println("Part 3:");
       System.out.println("-------");
       System.out.println("INITIALIZING river to Mississippi AND REPLACING "
                        + "EACH i to ! AND EACH s TO $");
       // code for Part 3 goes here


       System.out.println("Part 4:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A StringBuilder OBJECT INITIALIZED "
                        + "TO desserts AND REVERSING IT");
       // code for Part 4 goes here


       System.out.println("Part 5:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A Random OBJECT TO PLAY LOTTERY BY GENERATING ");
       System.out.println("6 RANDOM INTEGERS BETWEEN 1 and 49 (BOTH INCLUSIVE)");
       //code for Part 5 goes here

       System.out.println("Part 6:");
       System.out.println("-------");
       System.out.println("ADDING ONE DAY TO 2/28/2019, 2/28/2020, 2/28/2021 " +
                          "AND 2/28/2022 AND PRINTING THEM");
       // code for Part 6 goes here
    }
}

感谢任何帮助。我很高兴加入 Whosebug 社区,并希望有一天能够做出贡献。

所有这些问题的答案都可以在 Rectangle 的 Java 文档中找到。我不想用勺子喂食,所以这是我的一些观察。

  • 使用构造函数之一设置尺寸。
  • 使用一些方法读取每个尺寸(高度、宽度等)
  • 使用面积公式计算。

这是您的代码清理:

//import java.awt.Rectangle; // you probably autocompleted this line, since you created your own Rectangle class for console outputs
public class Rectangle {
    public double x;
    public double y;
    public double width, height;

    public Rectangle(double x, double y, double w, double h) {
           this.x = x;
           this.y = y;
           width = w;
           height = h;
   }
   public static void main(String[] args) {
       Rectangle box = new Rectangle (100, 50, 50, 60);
       System.out.println(box);

       System.out.println("Part 1:");
       System.out.println("-------");
       System.out.println("CREATING AN EMPTY Rectangle, SETTING ITS width TO 50, " +
                          "ITS height TO 60, AND ITS TOP LEFT CORNER TO (100, 50)");
       // Code for Part 1 goes here




       System.out.println("Part 2:");
       System.out.println("-------");
       System.out.println("TESTING THE PERIMETER OF THE RECTANGLE "
                        + "CREATED IN PART 1 ABOVE");
       // Code for Part 2 goes here.  Use the same Rectangle
       // you used Part1

       // define a String reference called river and initialize it
       // to Mississippi.  Read the API to figure out which method 
       // to use to get the desired effect.
       System.out.println("Part 3:");
       System.out.println("-------");
       System.out.println("INITIALIZING river to Mississippi AND REPLACING "
                        + "EACH i to ! AND EACH s TO $");
       // code for Part 3 goes here


       System.out.println("Part 4:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A StringBuilder OBJECT INITIALIZED "
                        + "TO desserts AND REVERSING IT");
       // code for Part 4 goes here


       System.out.println("Part 5:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A Random OBJECT TO PLAY LOTTERY BY GENERATING ");
       System.out.println("6 RANDOM INTEGERS BETWEEN 1 and 49 (BOTH INCLUSIVE)");
       //code for Part 5 goes here

       System.out.println("Part 6:");
       System.out.println("-------");
       System.out.println("ADDING ONE DAY TO 2/28/2019, 2/28/2020, 2/28/2021 " +
                          "AND 2/28/2022 AND PRINTING THEM");
       // code for Part 6 goes here
   }
}

作业如下:

      //assignment 1
      Rectangle box = new Rectangle(0,0,0,0);
      //assignment 2
      box.width = 50;
      box.height = 60;
      box.x = 100;
      box.y = 150;
      //assignment 3
      int area = box.height*box.width;
      //assignment 4
      System.out.println("Area: "+area);

我相信您可以将其集成到您自己的代码中,并制作适当的方法以及 getter 和 setter(如果需要)。