I made a SimpleGeometricObject class, a Point class that finds the distance betw
ID: 2247205 • Letter: I
Question
I made a SimpleGeometricObject class, a Point class that finds the distance between two points, and a TriangleFromSimpleGeometricObject class that is a subclass to find the area and perimeter of a triangle given three points. However, my test program isn't working. I'm getting this response after running it: Exception in thread "main" java.lang.NullPointerException
at TriangleFromSimpleGeometricObject.<init>(TriangleFromSimpleGeometricObject.java:42)
at TestTriangle.main(TestTriangle.java:18).
Here is Point:
public class Point {
private double x;
private double y;
public Point ()
{
}
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getDistance(Point PointB)
{
double x2 = PointB.getX();
double y2 = PointB.getY();
double part1 = (x2 - x)*(x2 - x);
double part2 = (y2 - y)*(y2 - y);
return Math.sqrt((part1) + (part2));
}
// Point Point1 = new Point(1,1);
// Point Point2 = new Point(0,0);
// System.out.println(Point1.getDistance(Point2));
}
Here is TriangleFromSimpleGeometricObject:
public class TriangleFromSimpleGeometricObject
extends SimpleGeometricObject {
private Point A;
private Point B;
private Point C;
public TriangleFromSimpleGeometricObject()
{
}
public TriangleFromSimpleGeometricObject(Point a, Point b, Point c)
{
this.A = a;
this.B = b;
this.C = c;
}
//* Return point A
public Point getA()
{
return A;
}
//* Return point B
public Point getB()
{
return B;
}
//* Return point C
public Point getC()
{
return C;
}
//* Get side lengths
public double getDistance(Point a, Point b)
{
}
double LengthA = A.getDistance(B);
double LengthB = B.getDistance(C);
double LengthC = C.getDistance(A);
//*getArea
public double getArea()
{
double Semi = (LengthA + LengthB + LengthC)/2;
double Area = Math.sqrt((Semi)*(Semi - LengthA)*(Semi - LengthB)*(Semi - LengthC));
return Area;
}
//*Get Perimeter
public double getPerimeter()
{
double Perimeter = LengthA + LengthB + LengthC;
return Perimeter;
}
public String toString()
{
return "The triangle was created on " + getDateCreated() + " with the points " + A + " " + B + " "+ C;
}
}
Here is TestTriangle:
public class TestTriangle
{
public static void main(String[] args)
{
//*(1,3), (-2,-2), (3,-1)
double x1 = 1;
double y1 = 3;
double x2 = -2;
double y2 = -2;
double x3 = 3;
double y3 = -1;
Point A = new Point(x1, y1);
Point B = new Point(x2, y2);
Point C = new Point(x3, y3);
TriangleFromSimpleGeometricObject triangle = new TriangleFromSimpleGeometricObject(A, B, C);
System.out.println(triangle.toString());
System.out.println("The area is " + triangle.getArea() + ".");
System.out.println("The perimeter is " + triangle.getPerimeter() + ".");
}
}
Thanks!
.
Explanation / Answer
You are getting errors because
1) TriangleFromSimpleGeometricObject.<init>(TriangleFromSimpleGeometricObject.java:42)
//* Get side lengths
public double getDistance(Point a, Point b)
{
}
This is the code present at the line 42. Here the method is returing double but there is no return statement of double variable or value.
Solution:
Do all the required calculations required using the two objects a and b. Then return some double value will resolves this problem.
2. TestTriangle.main(TestTriangle.java:18).
System.out.println(triangle.toString());
This is line present on line 10. Here you are trying to invoke the toString method.
Solution:
To execute the toString code just include only the object name. So the statement should be like
System.out.println(triangle);
**Again in the toString method of TriangleFromSimpleGeometricObject class you have included a method called getDateCreated which is not implemented in any class. Make sure that you implement this method.
**Comment for any furthur queries
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.