Hi, i have been given an assignment to create a class that defines 2 points. Then create a class that defines a vector. Then create a class that defines a rectangle (4 vectors). Prior to this assignment i was given the task to create a point and vector class to calculate the length of a vector. I had 100% marks on that assignment so i know i can use that code to help me create this rectangle class.
In this current assignment the task is to create a rectangle class and then calculate its perimeter and area. I have spent a while creating my rectangle class but every time i think it looks perfect it throws up a load of compiling errors.
Anyway this is my previous code which im using to help me for the rectangle class:
Point class:
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 class:
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 class:
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() );
}
}
}
The input file is in the format:
x y
x y
x y
Below is what my current rectangle class looks like but its throwing up lots of constructor errors.
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;
}
}
Thanks for helping in advance!