Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## shapes project

I was assigned a project in which I was to create a series of classes to represent shapes, then create a Canvas class to represent the canvas. For the most part, this was pretty simple but I ran across a few problems. The first of these problems is in my Triangle class. In order to find the area of the triangle, I used the .Math library and functions, sqrt() and hypot() however upon compiling, I am told that these functions cannot be found, and yes, I did import the .Math library. Below is the code for the getArea() function I am getting the errors in.

Code:
```
public double getArea()
{
double len1, len2, len3, semip, area;

len1 = hypot( (double)(p1.x - p2.x), (double)(p1.y - p2.y) );
len2 = hypot( (double)(p2.x - p3.x), (double)(p2.y - p3.y) );
len3 = hypot( (double)(p1.x - p3.x), (double)(p1.y - p3.y) );

semip = (len1 + len2 + len3) / 2;

area = sqrt( semip*(( semip - len1 )*( semip - len2 )*( semip - len3 )) );

return area;
}```
My next problem is really just my incompetency in math I don't really get how to find the area of a Convex Polygon. My teacher told us to head to this site for reference but I'm still lost (may be a result of not sleeping for 30 hours straight hehe ) http://www.mathwords.com/a/area_convex_polygon.htm. But any help in that area would be awesome.

My last bit of trouble is with this Canvas class. In the last few functions of the class, we are told to make functions to return an ArrayList of a specific shape. I'm a little confused on how to check for the shape. Below is the code for that class.

Code:
```import java.awt.Color;
import java.util.ArrayList;

public class Canvas
{
//variables
private ArrayList<Shape> shapes;

public Canvas()
{
shapes = new ArrayList<Shape>();
}

public void add( Shape shape )
{
}

public Shape remove( int index )
{
Shape removed = shapes.get( index );
shapes.remove( index );

return removed;
}

public Shape get( int index )
{
return shapes.get( index );
}

public int size()
{
return shapes.size();
}

public ArrayList<Circle> getCircles()
{
ArrayList<Circle> circles = new ArrayList<Circle>();

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
if( shapes.get( ctr ) instanceof Circle )
{
}
}

return circles;
}

public ArrayList<Rectangle> getRectangles()
{
ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
if( shapes.get( ctr ) instanceof Rectangle )
{
}
}

return rectangles;
}

public ArrayList<Triangle> getTriangles()
{
ArrayList<Triangle> triangles = new ArrayList<Triangle>();

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
if( shapes.get( ctr ) instanceof Triangle )
{
}
}

return triangles;
}

public ArrayList<ConvexPolygon> getConvexPolygons()
{
ArrayList<ConvexPolygon> convexPolygons = new ArrayList<ConvexPolygon>();

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
if( shapes.get( ctr ) instanceof ConvexPolygon )
{
}
}

return convexPolygons;
}

public ArrayList<Shape> getShapesByColor( Color color )
{
ArrayList<Shape> colors = new ArrayList<Shape>();

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
if( shapes.get( ctr ).getColor instanceof Color )
{
}
}

return colors;
}

public double getAreaOfAllShapes()
{
double area;

for( int ctr = 0; ctr < shapes.size(); ctr++ )
{
area += shapes.get( ctr ).getArea();
}

return area;
}

}```

• Ok, I figured out the area of a Convex Polygon. But I'm still getting a compiler error saying "cannot find symbol method hypot(double,double)" and "cannot find symbol method sqrt(double)". I even double-checked my JDK to make sure I had the latest one and I do. And I think there are some problems with the Canvas.java class I posted because the test driver I was given is not running properly.

• Originally Posted by uniquity
But I'm still getting a compiler error saying "cannot find symbol method hypot(double,double)" and "cannot find symbol method sqrt(double)". I even double-checked my JDK to make sure I had the latest one and I do. And I think there are some problems with the Canvas.java class I posted because the test driver I was given is not running properly.
are you using static imports?

• probably not hehe...i dont even know what that is

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•