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

Thread: shapes project

  1. #1
    New Coder
    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 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 )
       {
       	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;
       } 
        
    }
    Thanks in advance for your help!
    Last edited by uniquity; 01-21-2009 at 05:40 AM.

  • #2
    New Coder
    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 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.

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by uniquity View Post
    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?
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #4
    New Coder
    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


  •  

    Posting Permissions

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