如何在Java中创建一个Rectangle类?

huangapple go评论89阅读模式
英文:

How to create a Rectangle class in Java?

问题

class Point {
   int x, y;

   Point() {
      x = y = 0;
   }

   Point(int x0, int y0) {
      x = x0;
      y = y0;
   }

   public String toString() {
      return "(" + x + "," + y + ")";
   }
}

class Rectangle {
   Point p1, p2;

   Rectangle(int x1, int y1, int x2, int y2) {
     x1 = p1.x;
     y1 = p1.y;
     x2 = p2.x;
     y2 = p2.y;
   }
   Rectangle(Point p1, Point p2) {
      this.p1 = p1;
      this.p2 = p2;
   }

   public int area() {
     int width = p2.x - p1.x;
     int height = p2.y - p1.y;
     int area = width * height;
     return area;
   }
   public int perimeter() {
     int side1 = p2.x - p1.x;
     int side2 = p2.y - p1.y;
     int perimeter = side1 + side2 + side1 + side2;
     return perimeter;
   }
   public boolean pointInside(Point p) {
     if ((p.x >= p1.x && p.x <= p2.x) && (p.y >= p1.y && p.y <= p2.y)) {
       return true;
     } else {
       return false;
     }
   }
}

class TestRectangle {
   public static void main(String[] args) {
      Point a = new Point(1, 1);
      Point b = new Point(2, 2);
      Point c = new Point(3, 4);
      Point d = new Point(8, 2);

      Rectangle yellow = new Rectangle(a, c);
      Rectangle orange = new Rectangle(2, 3, 9, 6);
      Rectangle green = new Rectangle(3, 4, 4, 5);
      Rectangle blue = new Rectangle(5, 1, 6, 5);
      Rectangle red = new Rectangle(7, 3, 9, 5);

      System.out.println("Perimeter of the yellow rectangle = " + yellow.perimeter()); // 10
      System.out.println("Perimeter of the orange rectangle= " + orange.perimeter()); // 20

      System.out.println("Area of the yellow rectangle = " + yellow.area()); // 6
      System.out.println("Area of the orange rectangle = " + orange.area()); // 21

      System.out.println("Point B inside yellow? " + yellow.pointInside(b)); // true
      System.out.println("Point D inside yello? " + yellow.pointInside(d)); // false
   }
}
英文:

I have this problem where I am supposed to write this program where using a Point class to create points, then create a Rectangle class with methods like area,perimeter, and pointInside... My code is always resulting in a Null Pointer Exception and I don't know why.

class Point {
int x, y;
Point() {
x = y = 0;
}
Point(int x0, int y0) {
x = x0;
y = y0;
}
public String toString() {
return &quot;(&quot; + x + &quot;,&quot; + y + &quot;)&quot;;
}
}
class Rectangle {
Point p1,p2;
Rectangle(int x1,int y1,int x2,int y2)
{
x1 = p1.x;
y1 = p1.y;
x2 = p2.x;
y2 = p2.y;
}
Rectangle(Point p1,Point p2)
{
this.p1 = p1;
this.p2 = p2;
}
public int area ()
{
int width = p2.x - p1.x;
int height = p2.y - p1.y;
int area = width * height;
return area;
}
public int perimeter()
{
int side1 = p2.x - p1.x;
int side2 = p2.y - p1.y;
int perimeter = side1 + side2 + side1 + side2;
return perimeter;
}
public boolean pointInside(Point p)
{
if ((p.x &gt;= p1.x &amp;&amp; p.x &lt;= p2.x) &amp;&amp; (p.y &gt;= p1.y &amp;&amp; p.y &lt;= p2.y))
{
return true;
} else {
return false;
}
}
}
class TestRectangle {
public static void main(String[] args) {
Point a = new Point(1,1);
Point b = new Point(2,2);
Point c = new Point(3,4);
Point d = new Point(8,2);
Rectangle yellow  = new Rectangle(a, c);
Rectangle orange  = new Rectangle(2, 3, 9, 6);
Rectangle green    = new Rectangle(3, 4, 4, 5);
Rectangle blue    = new Rectangle(5, 1, 6, 5);
Rectangle red = new Rectangle(7, 3, 9, 5);
System.out.println(&quot;Perimeter of the yellow rectangle = &quot; + yellow.perimeter()); // 10
System.out.println(&quot;Perimeter of the orange rectangle= &quot; + orange.perimeter()); // 20
System.out.println(&quot;Area of the yellow rectangle = &quot; + yellow.area()); // 6
System.out.println(&quot;Area of the orange rectangle = &quot; + orange.area()); // 21
System.out.println(&quot;Point B inside yellow? &quot; + yellow.pointInside(b)); // true
System.out.println(&quot;Point D inside yello? &quot; + yellow.pointInside(d)); // false
}
}

答案1

得分: 1

当某个地方调用这个构造函数时:

Rectangle(int x1, int y1, int x2, int y2) {
  x1 = p1.x;
  y1 = p1.y;
  x2 = p2.x;
  y2 = p2.y;
}

你的变量 p1p2 并没有被初始化为任何值,在这之前,你只是声明了这两个字段,实际上并没有将它们设置为任何值。它们默认是空的。

你需要做的是:

Rectangle(int x1, int y1, int x2, int y2) {
  p1 = new Point(x1, y1);
  p2 = new Point(x2, y2);
}

我也不清楚为什么你在原始代码中修改构造函数的参数。

英文:

When something calls this constructor

Rectangle(int x1,int y1,int x2,int y2)
{
x1 = p1.x;
y1 = p1.y;
x2 = p2.x;
y2 = p2.y;
}

your variables p1 and p2 haven't been initialized to anything, since before that, all you do is declare those two fields, not actually set them to anything. They are null by default.

What you need to do is

Rectangle(int x1, int y1, int x2, int y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}

I also have no idea why you're modifying the arguments to the constructor in the original.

huangapple
  • 本文由 发表于 2020年4月8日 06:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61090447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定