Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4
Thread: shapes project

01212009, 02:45 AM #1
 Join Date
 Jan 2004
 Location
 CA
 Posts
 70
 Thanks
 3
 Thanked 0 Times in 0 Posts
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 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 ) { shapes.add( 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 ) { circles.add( shapes.get( ctr ) ); } } 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 ) { rectangles.add( shapes.get( ctr ) ); } } 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 ) { triangles.add( shapes.get( ctr ) ); } } 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 ) { convexPolygons.add( shapes.get( ctr ) ); } } 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 ) { colors.add( shapes.get( ctr ) ); } } return colors; } public double getAreaOfAllShapes() { double area; for( int ctr = 0; ctr < shapes.size(); ctr++ ) { area += shapes.get( ctr ).getArea(); } return area; } }
Last edited by uniquity; 01212009 at 05:40 AM.

01232009, 01:18 AM #2
 Join Date
 Jan 2004
 Location
 CA
 Posts
 70
 Thanks
 3
 Thanked 0 Times in 0 Posts
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 doublechecked 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.

01232009, 04:31 PM #3

01232009, 05:58 PM #4
 Join Date
 Jan 2004
 Location
 CA
 Posts
 70
 Thanks
 3
 Thanked 0 Times in 0 Posts
probably not hehe...i dont even know what that is