我目前正在开发一个名为Rectangle项目的项目,其中我应该在Java上执行以下操作:
制作以下方法:
还可以创建一个方法来确定两个矩形是否相交并返回一个新的交集矩形。在ObjectDemo程序中测试以下矩形的所有方法:
我需要在星期一之前完成这项工作但是我一直遇到很多错误,例如无法识别的变量等等,而且我不确定如何修复它们。请告诉我!
我有三个文件:Point,RectangleTest和Rectangle。
以下是他们的代码:
点代码:
public class Point
{
//Class variables
private int xCoord; //Private (instead of Public) because we are going to use this class in the other file
//We don't want people changing the values unless we let them
private int yCoord; //Variables are not in a function so will maintain their value
//Constructor
Point()
{
xCoord = 0;
yCoord = 0;
}
//Constructor
Point(int startX, int startY)
{
xCoord = startX;
yCoord = startY;
}
public int getX()
{
return xCoord;
}
public int getY()
{
return yCoord;
}
public void setX(int newX)
{
xCoord = newX;
}
public void setY(int newY)
{
yCoord = newY;
}
public void move(int moveX, int moveY)
{
xCoord+=moveX;
yCoord+=moveY;
}
Point(Point p)
{
xCoord = p.getX();
yCoord = p.getY();
}
}
RectangleTest代码:
public class RectangleTest
{
public static void main(String [] args)
{
Rectangle A = new Rectangle(0,0,10,20);
Rectangle B = new Rectangle(5,5,15,15);
Rectangle C = new Rectangle(20,12,10,20);
//Move rectangles
A.moveby(5,10);
B.moveby(-5,-5);
C.moveby(-20,0);
int areaA = A.getarea;
System.out.println("The area of rectangle A is " +areaA);
int areaB = B.getarea;
System.out.println("The area of rectangle B is " +areaB);
int areaC = C.getarea;
System.out.println("The area of rectangle C is " +areaC);
Rectanlge iAB = A.intersect(B);
Rectangle iAC = A.intersect(C);
Rectangle iBC = B.intersect(C);
if(iab != null)
{
System.out.println("The area of intersection rectangle iab = " +iAB.area());
}
if(iac != null)
{
System.out.println("The area of intersection rectangle iac = " +iAC.area());
}
if (ibc != null)
{
System.out.println("The area of intersection area ibc = " +iBC.area());
}
}
}
矩形代码:
public class Rectangle
{
Point origin;
int height;
int width;
//Constructor for rectangle object
Public Rectangle(int startX, int startY, int startW, int startH)
{
origin = new Point (startX, startY);
width = startW;
height = startH;
}
//Set origin point for NEW rectangle origins
//FIX
public void setOrigin(int newX, int newY)
{
origin.setX(newX);
origin.setY(newY);
}
public int moveBy(int moveX, int moveY)
{
origin.move(moveX, moveY);
}
public int getArea()
{
int recArea = height*width;
return recArea;
}
public Rectangle intersect(Rectangle testR)
{
int meTRX = origin.getX() + width;
int meTRY = origin.getY() + height;
int testTRX = testR.origin.getX() + width;
int testTRY = testR.origin.getY() + height;
//Boolean to get iTRX
if(meTRX>testTRX)
{
int iTRX = testTRX;
}
else
{
int iTRX = meTRX;
}
//Boolean to get iTRY
if(meTRY>testTRY)
{
int iTRY = testTRY;
}
else
{
int iTRY = meTRY;
}
//Boolean to get iBLX
if(testBLX>meBLX)
{
int iBLX = testBLX;
}
else
{
int iBLX = meBLX;
}
//Boolean to get iBLY
if(testBLY>meBLY)
{
int iBLY = testBLY;
}
else
{
int iBLY = meBLY;
}
//Testing for whether or not there is an intersection rectangle
if(iTRX-iBLX<0 || iTRY-iBLY<0)
{
return null;
}
int iH = iTRY - iBLY;
int iW = iTRX - iBLX;
int intersectArea = iH * iW;
}
}
请指出任何问题!我对编程很陌生,所以我经常犯很多简单的错误。此外,如果没有新引入的命令或任何内容,我将不胜感激,因为我的老师非常严格地这样做。
谢谢!
P.S。我将不胜感激任何关于代码改进的额外知识或信息(一般情况下)。谢谢!
答案 0 :(得分:0)
几个问题:
public int moveBy(int moveX, int moveY)
中,您应该将其更改为public void moveBy(int moveX, int moveY)
int testBLX = 0;
以及类似的其他来避免编译错误。答案 1 :(得分:0)
在Rectangle
课程中:
Public Rectangle(int startX, int startY, int startW, int startH)
,但实际上您需要public Rectangle(int startX, int startY, int startW, int startH)
。在Java中,关键字始终以小写字母开头。public int moveBy(int moveX, int moveY)
的原点的方法有int
作为返回类型,因此编译器希望您返回一个整数值。我想您根本不想返回任何内容,因此您可以将返回类型更改为void
。public Rectangle intersect(Rectangle testR)
中,您只在if / else语句的范围内声明变量(iTRX,iTRY,iBLX,iBLY),例如int iTRX = testTRX;
,这意味着在每个if / else之后声明这些变量不再可用。要了解有关变量的不同范围的更多信息,请执行以下操作:Variable scopes 在RectangleTest
课程中:
每个区域的面积是多少?测试它们是否与其他两个相交以及交叉区域是什么。
一些一般性线索:
Point
中提供了java.awt.Point
类。你不必重新发明轮子。