我已被賦予創建一個定義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