2009-12-01 26 views
0

我已被赋予创建一个定义2分的类的任务。然后创建一个定义矢量的类。然后创建一个定义矩形的类(4个向量)。在此任务之前,我被赋予创建点和矢量类以计算矢量长度的任务。我在该作业上有100%的分数,所以我知道我可以使用该代码来帮助我创建这个矩形类。在java中创建一个带有向量和点的矩形?

在当前任务中,任务是创建一个矩形类,然后计算其周长和面积。我花了一段时间创建我的矩形类,但每次我认为它看起来很完美,它会引发一堆编译错误。

反正这是我以前的代码,即时通讯使用帮助我的矩形类:

Point类:

public class Point { 

private double x; 
private double y; 

public Point(){ 
x=0.0; 
y=0.0; 
} 
public Point(double a, double b){ 
x=a; 
y=b; 
} 
public double getX(){return x;} 
public double getY(){return y;} 
} 

Vector类:

public class Vector { 

private Point p = new Point(); 
private Point q = new Point(); 


public Vector(Point a, Point b){ 
p=a; 
q=b; 
} 
public double giveLength (){ 
double xDiff=q.getX() - p.getX(); 
double yDiff=q.getY() - p.getY(); 
return Math.sqrt((xDiff*xDiff)+(yDiff*yDiff)); 
} 

public double giveLength2(){ 
double x2Diff = p.getX2() - q.getX2(); 
double y2Diff = p.getY2() - q.getY2(); 
return Math.sqrt((x2Diff*x2Diff)+(y2Diff*y2Diff)); 
} 
} 

Assignment7类:

import java.util.*; 
import java.math.*; 
import java.io.*; 

class Assignment7 { 
public static void main(String[] args)throws Exception{ 

double X1; 
double Y1; 
double X2; 
double Y2; 

Point P1; 
Point P2; 
Vector V; 

Scanner in = new Scanner(System.in); 
System.out.println("Please enter a filename:"); 
String filename = in.nextLine(); 

File inputFile = new File(filename); 
Scanner reader = new Scanner(inputFile);  


while (reader.hasNext()){ 

X1 = reader.nextDouble(); 
Y1 = reader.nextDouble(); 
P1 = new Point(X1,Y1); 


X2 = reader.nextDouble(); 
Y2 = reader.nextDouble(); 
P2 = new Point(X2,Y2); 


V = new Vector (P1, P2); 

System.out.println("X1 " + X1 + " length is " + V.giveLength()); 

} 
} 
} 

输入文件的格式为:

x y 
x y 
x y 

下面是我当前矩形类是什么样子,但它扔了很多的构造函数的错误。

class Rectangle{ 

private Vector w = new Vector(); 
private Vector x = new Vector(); 
private Vector y = new Vector(); 
private Vector z = new Vector(); 

public Rectangle(Vector a, Vector b, Vector c, Vector d){ 
w=a; 
x=b; 
y=c; 
z=d; 
} 

public double givePerimeter(){ 
    double perimeter = ((w.giveLength() + x.giveLength2())* 2); 
    return perimeter; 
} 

public double giveArea(){ 
    double area = (w.giveLength() * y.giveLength2()); 
    return area; 
} 


} 

感谢您的帮助!

+1

家庭作业应标记'家庭作业'标签。 – 2009-12-01 20:38:42

+0

好的,已做标记:) – 2009-12-01 20:41:35

+0

啊对不起。谢谢。 – cal 2009-12-01 20:42:31

回答

2

您尝试在这里初始化向量4:

private Vector w = new Vector(); 
private Vector x = new Vector(); 
private Vector y = new Vector(); 
private Vector z = new Vector(); 

但你没有一个向量构造不带参数!

尝试构造没有点的向量是没有意义的。你想要做的是首先在坐标中读取并设置你的4个点,然后一旦你的点被定义,从点(每个2个)开始构建4个向量。

因此......将这些private声明移到您的点已经设置的位置下方,并将一对点放入每组圆括号中。

+0

第二点。我现在遇到的错误消息是这样的: 找不到符号 符号:构造函数向量() location:class Vector private Vector w = new Vector(); 并且这是向量w,x,y和z的x4。 – cal 2009-12-01 20:48:25

+0

哦,这很容易。我会去更新我的答案... – 2009-12-01 20:51:27

+0

我只看到一个构造函数,它需要上面代码中的两点。 – 2009-12-01 20:51:59