英文:
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 "(" + 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
}
}
答案1
得分: 1
当某个地方调用这个构造函数时:
Rectangle(int x1, int y1, int x2, int y2) {
x1 = p1.x;
y1 = p1.y;
x2 = p2.x;
y2 = p2.y;
}
你的变量 p1
和 p2
并没有被初始化为任何值,在这之前,你只是声明了这两个字段,实际上并没有将它们设置为任何值。它们默认是空的。
你需要做的是:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论