在Java中构建矩形的实验室

问题描述 投票:2回答:2

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

  1. 我被告知要构造一个名为box的空矩形。
  2. 弄清楚如何将盒子的宽度更改为50,将其高度更改为60,将左上角更改为[100,50]。 (您需要在框上调用适当的方法来实现此目的。)
  3. 弄清楚如何计算盒子的面积。您必须通过调用适当的方法来计算区域,以获得框的高度和宽度。
  4. 然后通过调用框上的方法打印返回的值,然后我们打印一条描述我们期望看到的值的消息。

我已经搜索了堆栈交换并找到了一些有用的信息,但我仍然无法弄清楚代码中的错误以及为什么它不起作用。我假设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
    }
}

任何帮助表示赞赏。我很高兴加入StackOverflow社区,希望有一天能够做出贡献。

java methods constructor rectangles
2个回答
0
投票

以下是代码的清理:

//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。


0
投票

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

  • 使用其中一个构造函数来设置尺寸。
  • 使用一些方法来读取每个维度(高度,宽度等)
  • 使用面积公式计算。
© www.soinside.com 2019 - 2024. All rights reserved.