tags:

views:

80

answers:

1

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!

+2  A: 

You try to initialize 4 vectors here:

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

But you don't have a Vector constructor with no arguments!

There's no sense in trying to construct vectors without points. What you want to do is first read in the coordinates and set up your 4 points, then construct the 4 vectors from the points (well, 2 each) once your points are defined.

So... move these private declarations to underneath where your points are already set up, and put a pair of points into each set of parentheses.

Carl Smotricz
Ah thanks ill look into that second point. The current error messages im getting is of the sort:cannot find symbolsymbol: constructor Vector()location: class Vectorprivate Vector w = new Vector();and this is x4 for the vectors w, x, y and z.
cal
Oh, that makes it pretty easy. I'll go update my answer...
Carl Smotricz
I only see a constructor for vector that takes the two points in your code above.
John D.
hmm i dont really understand what you are saying Carl. Are you saying to move my private declarations to the main class?
cal
No, I don't mean they shouldn't be private or roughly in the same area you have them. What I mean is that those 4 declarations should appear in your code *after* you have some points to initialize the vectors with.
Carl Smotricz
Near the bottom of "assignment 7" you create a vector from 2 points and print it out. Instead, you should read in 4 points and then create those 4 vectors.
Carl Smotricz
If you want you can leave your 4 `private` declarations where they are, but do what you did with `V`: Declare them but don't initialize them!
Carl Smotricz