我已被赋予创建一个定义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;
}
}
感谢您的帮助!
家庭作业应标记'家庭作业'标签。 – 2009-12-01 20:38:42
好的,已做标记:) – 2009-12-01 20:41:35
啊对不起。谢谢。 – cal 2009-12-01 20:42:31