s3a

December 4th, 2009, 08:03 PM

The first and second code boxes were written by my teacher so they are correct. The third one was worked on by me and seems to be correct but am just mentioning it in case it could be where to look for a problem. The fourth and last code box is what I am working on right now and I'm having trouble with it. My questions are included as comments in the fourth code box.

public class PointDemo

{

public static void main(String[] args)

{

Point p = new Point(); // create a point

System.out.println();

System.out.println(p); // Java does this: System.out.println(p.toString());

p.translate(2,3);

System.out.println();

System.out.println("p = " + p);

System.out.println();

System.out.println("length of p = " + p.length());

Point q = new Point(7, 3);

System.out.println();

System.out.println("distance between " + p + " and " + q + " is " + p.distance(q));

System.out.println();

System.out.println("dot product of " + p + " and " + q + " is " + p.dotProduct(q));

Point r = new Point(q);

System.out.println();

System.out.println("r = " + r);

System.out.println();

System.out.println("it is " + r.equals(q) + " that " + r + " and " + q + " are equal");

Point s = new Point(5);

System.out.println();

System.out.println("s = " + s);

if( s.equals(q) )

{

System.out.println();

System.out.println("it is true that " + s + " and " + q + " are equal");

}

else

{

System.out.println();

System.out.println("it is false that " + s + " and " + q + " are equal");

}

Point t = new Point(new Point(1,1));

System.out.println();

System.out.println("t = " + t);

t.move(p);

System.out.println();

System.out.println("t = " + t);

t.move(new Point(3,3));

System.out.println();

System.out.println("t = " + t);

Point u = t.getPoint();

System.out.println();

System.out.println("u = " + u);

}

}

public class TriangleDemo

{

public static void main(String[] args)

{

Triangle t = new Triangle();

System.out.println("Triangle t: " + t);

System.out.println("Perimeter of t: " + t.perimeter());

System.out.println("Area of t: " + t.area());

Point p1 = new Point(1,2);

Point p2 = new Point(2,1);

Point p3 = new Point(3,4);

Triangle r = new Triangle(p1, p2, p3);

System.out.println("Triangle r: " + r);

System.out.println("SideA of r: " + r.sideA());

System.out.println("SideB of r: " + r.sideB());

System.out.println("SideC of r: " + r.sideC());

Triangle s = new Triangle(t.getB(), t.getC(), t.getA());

if( s.equals(t) )

System.out.println("Triangles s and t are equal");

else

System.out.println("Triangles s and t are not equal");

if( s.equals(r) )

System.out.println("Triangles s and r are equal");

else

System.out.println("Triangles s and r are not equal");

}

}

public class Point

{

// Instance fields

private int x;

private int y;

// Mutators:

public void setX(int p)

{

this.x = p;

}

public void setY(int p)

{

this.y = p;

}

// Accessors:

public double getX()

{

return x;

}

public double getY()

{

return y;

}

/*

The default (no-arguments) constructor, constructing the point (1; 1).

Usage example: Point p = new Point(); // p = (1,1)

*/

public Point()

{

this.x = 1;

this.y = 1;

}

/*

A constructor that can take one value m, constructing the point (m;m).

Usage example: Point p = new Point(5); // p = (5,5)

*/

public Point(int x)

{

this.x = x;

this.y = x;

}

/*

A constructor that can take two values m and n, constructing the point (m; n).

Usage example: Point p = new Point(3,7); // p = (3,7)

*/

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

/*

A constructor that that takes a Point p as a parameter, constructing and initial-

izing a point with the same x and y coordinates as p.

Usage example: Point p = new Point(9,4);

Point q = new Point(p); // q = (9,4)

*/

public Point(Point p)

{

this.x = p.x;

this.y = p.y;

}

// Accessor method getPoint() that returns this point.

//public Point getPoint(int x, int y)

public Point getPoint()

{

//return this.Point; // I think this is one of the errors!!!!!!!!!!!!!!!!!!!

return new Point(this.x, this.y); // or equivalently, return new Point(x, y);

// or equivalently, return new Point(this);

}

/*

* A toString() method that returns the string (x, y), where x and y are the coordinate

* values of this point. (Recall that the this object is the same as the object calling a

* method.)

*/

//public String toString(Point p)

public String toString()

{

//this.x = p.x;

//this.y = p.y;

//String p1 = "( " + this.x + " , " + this.y + " )";

//return p1; // I think this is one of the errors!!!!!!!!!!!!!!!!!!!!!!

String result = "POINT (" + x + " , " + y + ")";

return result;

}

// A method length() that takes no parameters, and returns the length of this point:

public double length()

{

double distance = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));

return distance;

}

/*

* A method distance() that takes a Point p as a parameter and returns the distance

* between p and this point.

* sqrt((x1 − x2 )^2 + (y1 − y2 )^2).The distance between two points (x1 , y1 ) and (x2 , y2 ) is defined as

*/

public double distance(Point p)

{

double distance = Math.sqrt(Math.pow((this.x - p.x), 2) + Math.pow((this.y - p.y),2)); // Is the formula implemented properly?

return distance;

}

/* A method translate() that takes two double parameters deltaX and deltaY and

translates the current location of this point (x, y) to (x+deltaX, y+deltaY). */

public void translate(int deltaX, int deltaY)

{

this.x += deltaX;

this.y += deltaY;

}

/* A method move() that takes a Point p as a parameter, and moves this point to p. */

public void move(Point p)

{

//this.x = p.x; // WAS THIS CORRECT?

//this.y = p.y; // WAS THIS CORRECT?

p.x = this.x; // IS THIS CORRECT?

p.y = this.y; // IS THIS CORRECT?

}

/* A method dotProduct() that takes a Point p as a parameter and returns the dot

product of p and this point. The dot product of two points (x1; y1) and (x2; y2) is

defined as x1x2 + y1y2. */

public int dotProduct(Point p)

{

int dotProduct = (this.x * p.x + this.y * p.y); //what's the difference between this.x and p.x for example?

return dotProduct;

}

/*

* a method equals() that takes an Object obj as a parameter, and returns true if and

* only if Object obj is not null and is an instance of Point that has the same x and y

* coordinates as this object.

*/

public boolean equals(Object obj) // Figure this out!

{

boolean result;

Point q = (Point)obj;

if(obj == null)

result = false;

if(this.x == q.x && this.y == q.y)

result = true;

else

result = false;

return result;

}

}

public class Triangle

{

// Instance Fields

private Point A;

private Point B;

private Point C;

// Two constructors

// The default constructor, creating the triangles with vertices at (1,1), (5,5), and (1,5).

public Triangle()

{

Point A = getPoint(1,1); // Is this correct?

Point B = getPoint(5,5); // Is this correct?

Point C = getPoint(1,5); // is this correct?

}

/*

* A non-default constructor that can take three Point parameters p1, p2, and p3,

* initializing the vertices A, B, and C of the triangle under construction to p1, p2,

* and p3, respectively.

*/

public Triangle(Point p1, Point p2, Point p3)

{

Point A = new Point(p1); // Is this correct?

Point B = new Point(p2); // is this correct?

Point C = new Point(p3); // is this correct?

}

/*

* three accessors getA(), getB(), and getC() that return the vertices A, B, and C of this

* triangle, respectively.

* three mutator setA(), setB(), and setC() that each take a Point as a parameter, and

* set the vertices A, B, and C to the supplied point, respectively.

*/

public Point getA()

{

return Point(this.x, this.y); // Is this correct?

}

public int getB()

{

return Point(this.x, this.y); // Is this correct?

}

public int getC()

{

return Point(this.x, this.y); // Is this correct?

}

/*

* three mutator setA(), setB(), and setC() that each take a Point as a parameter, and

* set the vertices A, B, and C to the supplied point, respectively.

*/

public void setA(Point p)

{

Point A = p; // Is this correct?

}

public void setB(Point p)

{

Point B = p; // Is this correct?

}

public void setC(Point p)

{

Point C = p; // Is this correct?

}

// A method sideA() that returns the distance between the vertices B and C.

public double sideA()

{

double distance = Math.sqrt(Math.pow(((double)B - (double)C), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method sideB() that returns the distance between the vertices A and C.

public double sideB()

{

double distance = Math.sqrt(Math.pow(((double)A - (double)C), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method sideC() that returns the distance between the vertices A and B.

public double sideC()

{

double distance = Math.sqrt(Math.pow(((double)A - (double)B), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method perimeter() that returns the perimeter of this triangle.

public double perimeter()

{

double perimeter = sideA() + sideB() + sideC();

return perimeter;

}

// a method area() that returns the area of this triangle.

public double area()

{

double area = 5; // implement actual formula later

return area;

}

/* A toString() method that returns a String of the form (x1 , y1 ), (x2 , y2 ), (x3 , y3 ) where

(x1 , y1 ), (x2 , y2 ), and (x3 , y3 ) represent the coordinates of the vertices A, B, and C of this

triangle, respectively.*/

public String toString()

{

String result = "TRIANGLE" + "(" + getA() + ") (" + getB() + ")" + + "(" + getC() + ")";

return result;

}

/* A method equals() that takes an Object obj as a parameter, and returns true if and

only if obj is not null and is an instance of Triangle that has the same area and

perimeter as this object.

*/

public boolean equals(Object obj)

{

Triangle q = (Triangle)obj;

if(obj == null)

{

return true;

}

else

{

return false;

}

}

}

Any help would be GREATLY appreciated!

Thanks in advance!

public class PointDemo

{

public static void main(String[] args)

{

Point p = new Point(); // create a point

System.out.println();

System.out.println(p); // Java does this: System.out.println(p.toString());

p.translate(2,3);

System.out.println();

System.out.println("p = " + p);

System.out.println();

System.out.println("length of p = " + p.length());

Point q = new Point(7, 3);

System.out.println();

System.out.println("distance between " + p + " and " + q + " is " + p.distance(q));

System.out.println();

System.out.println("dot product of " + p + " and " + q + " is " + p.dotProduct(q));

Point r = new Point(q);

System.out.println();

System.out.println("r = " + r);

System.out.println();

System.out.println("it is " + r.equals(q) + " that " + r + " and " + q + " are equal");

Point s = new Point(5);

System.out.println();

System.out.println("s = " + s);

if( s.equals(q) )

{

System.out.println();

System.out.println("it is true that " + s + " and " + q + " are equal");

}

else

{

System.out.println();

System.out.println("it is false that " + s + " and " + q + " are equal");

}

Point t = new Point(new Point(1,1));

System.out.println();

System.out.println("t = " + t);

t.move(p);

System.out.println();

System.out.println("t = " + t);

t.move(new Point(3,3));

System.out.println();

System.out.println("t = " + t);

Point u = t.getPoint();

System.out.println();

System.out.println("u = " + u);

}

}

public class TriangleDemo

{

public static void main(String[] args)

{

Triangle t = new Triangle();

System.out.println("Triangle t: " + t);

System.out.println("Perimeter of t: " + t.perimeter());

System.out.println("Area of t: " + t.area());

Point p1 = new Point(1,2);

Point p2 = new Point(2,1);

Point p3 = new Point(3,4);

Triangle r = new Triangle(p1, p2, p3);

System.out.println("Triangle r: " + r);

System.out.println("SideA of r: " + r.sideA());

System.out.println("SideB of r: " + r.sideB());

System.out.println("SideC of r: " + r.sideC());

Triangle s = new Triangle(t.getB(), t.getC(), t.getA());

if( s.equals(t) )

System.out.println("Triangles s and t are equal");

else

System.out.println("Triangles s and t are not equal");

if( s.equals(r) )

System.out.println("Triangles s and r are equal");

else

System.out.println("Triangles s and r are not equal");

}

}

public class Point

{

// Instance fields

private int x;

private int y;

// Mutators:

public void setX(int p)

{

this.x = p;

}

public void setY(int p)

{

this.y = p;

}

// Accessors:

public double getX()

{

return x;

}

public double getY()

{

return y;

}

/*

The default (no-arguments) constructor, constructing the point (1; 1).

Usage example: Point p = new Point(); // p = (1,1)

*/

public Point()

{

this.x = 1;

this.y = 1;

}

/*

A constructor that can take one value m, constructing the point (m;m).

Usage example: Point p = new Point(5); // p = (5,5)

*/

public Point(int x)

{

this.x = x;

this.y = x;

}

/*

A constructor that can take two values m and n, constructing the point (m; n).

Usage example: Point p = new Point(3,7); // p = (3,7)

*/

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

/*

A constructor that that takes a Point p as a parameter, constructing and initial-

izing a point with the same x and y coordinates as p.

Usage example: Point p = new Point(9,4);

Point q = new Point(p); // q = (9,4)

*/

public Point(Point p)

{

this.x = p.x;

this.y = p.y;

}

// Accessor method getPoint() that returns this point.

//public Point getPoint(int x, int y)

public Point getPoint()

{

//return this.Point; // I think this is one of the errors!!!!!!!!!!!!!!!!!!!

return new Point(this.x, this.y); // or equivalently, return new Point(x, y);

// or equivalently, return new Point(this);

}

/*

* A toString() method that returns the string (x, y), where x and y are the coordinate

* values of this point. (Recall that the this object is the same as the object calling a

* method.)

*/

//public String toString(Point p)

public String toString()

{

//this.x = p.x;

//this.y = p.y;

//String p1 = "( " + this.x + " , " + this.y + " )";

//return p1; // I think this is one of the errors!!!!!!!!!!!!!!!!!!!!!!

String result = "POINT (" + x + " , " + y + ")";

return result;

}

// A method length() that takes no parameters, and returns the length of this point:

public double length()

{

double distance = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));

return distance;

}

/*

* A method distance() that takes a Point p as a parameter and returns the distance

* between p and this point.

* sqrt((x1 − x2 )^2 + (y1 − y2 )^2).The distance between two points (x1 , y1 ) and (x2 , y2 ) is defined as

*/

public double distance(Point p)

{

double distance = Math.sqrt(Math.pow((this.x - p.x), 2) + Math.pow((this.y - p.y),2)); // Is the formula implemented properly?

return distance;

}

/* A method translate() that takes two double parameters deltaX and deltaY and

translates the current location of this point (x, y) to (x+deltaX, y+deltaY). */

public void translate(int deltaX, int deltaY)

{

this.x += deltaX;

this.y += deltaY;

}

/* A method move() that takes a Point p as a parameter, and moves this point to p. */

public void move(Point p)

{

//this.x = p.x; // WAS THIS CORRECT?

//this.y = p.y; // WAS THIS CORRECT?

p.x = this.x; // IS THIS CORRECT?

p.y = this.y; // IS THIS CORRECT?

}

/* A method dotProduct() that takes a Point p as a parameter and returns the dot

product of p and this point. The dot product of two points (x1; y1) and (x2; y2) is

defined as x1x2 + y1y2. */

public int dotProduct(Point p)

{

int dotProduct = (this.x * p.x + this.y * p.y); //what's the difference between this.x and p.x for example?

return dotProduct;

}

/*

* a method equals() that takes an Object obj as a parameter, and returns true if and

* only if Object obj is not null and is an instance of Point that has the same x and y

* coordinates as this object.

*/

public boolean equals(Object obj) // Figure this out!

{

boolean result;

Point q = (Point)obj;

if(obj == null)

result = false;

if(this.x == q.x && this.y == q.y)

result = true;

else

result = false;

return result;

}

}

public class Triangle

{

// Instance Fields

private Point A;

private Point B;

private Point C;

// Two constructors

// The default constructor, creating the triangles with vertices at (1,1), (5,5), and (1,5).

public Triangle()

{

Point A = getPoint(1,1); // Is this correct?

Point B = getPoint(5,5); // Is this correct?

Point C = getPoint(1,5); // is this correct?

}

/*

* A non-default constructor that can take three Point parameters p1, p2, and p3,

* initializing the vertices A, B, and C of the triangle under construction to p1, p2,

* and p3, respectively.

*/

public Triangle(Point p1, Point p2, Point p3)

{

Point A = new Point(p1); // Is this correct?

Point B = new Point(p2); // is this correct?

Point C = new Point(p3); // is this correct?

}

/*

* three accessors getA(), getB(), and getC() that return the vertices A, B, and C of this

* triangle, respectively.

* three mutator setA(), setB(), and setC() that each take a Point as a parameter, and

* set the vertices A, B, and C to the supplied point, respectively.

*/

public Point getA()

{

return Point(this.x, this.y); // Is this correct?

}

public int getB()

{

return Point(this.x, this.y); // Is this correct?

}

public int getC()

{

return Point(this.x, this.y); // Is this correct?

}

/*

* three mutator setA(), setB(), and setC() that each take a Point as a parameter, and

* set the vertices A, B, and C to the supplied point, respectively.

*/

public void setA(Point p)

{

Point A = p; // Is this correct?

}

public void setB(Point p)

{

Point B = p; // Is this correct?

}

public void setC(Point p)

{

Point C = p; // Is this correct?

}

// A method sideA() that returns the distance between the vertices B and C.

public double sideA()

{

double distance = Math.sqrt(Math.pow(((double)B - (double)C), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method sideB() that returns the distance between the vertices A and C.

public double sideB()

{

double distance = Math.sqrt(Math.pow(((double)A - (double)C), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method sideC() that returns the distance between the vertices A and B.

public double sideC()

{

double distance = Math.sqrt(Math.pow(((double)A - (double)B), 2)); // Is this correct? (I typecasted in order to return distance as a double - the above is an attempt to use the distance formula "sqrt((x2-x1)^2 + (y2-y1)^2)")

return distance;

}

// A method perimeter() that returns the perimeter of this triangle.

public double perimeter()

{

double perimeter = sideA() + sideB() + sideC();

return perimeter;

}

// a method area() that returns the area of this triangle.

public double area()

{

double area = 5; // implement actual formula later

return area;

}

/* A toString() method that returns a String of the form (x1 , y1 ), (x2 , y2 ), (x3 , y3 ) where

(x1 , y1 ), (x2 , y2 ), and (x3 , y3 ) represent the coordinates of the vertices A, B, and C of this

triangle, respectively.*/

public String toString()

{

String result = "TRIANGLE" + "(" + getA() + ") (" + getB() + ")" + + "(" + getC() + ")";

return result;

}

/* A method equals() that takes an Object obj as a parameter, and returns true if and

only if obj is not null and is an instance of Triangle that has the same area and

perimeter as this object.

*/

public boolean equals(Object obj)

{

Triangle q = (Triangle)obj;

if(obj == null)

{

return true;

}

else

{

return false;

}

}

}

Any help would be GREATLY appreciated!

Thanks in advance!