1 Replies - 7327 Views - Last Post: 01 March 2010 - 05:31 PM Rate Topic: -----

#1 weemanpow3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-September 09

Geometry Class program

Posted 01 March 2010 - 05:08 PM

I am stuck on my assignment and don't know how to fix it. I think I am messing up on the Geometry class portion. Im just having a hard time having it do calculations. Any help is appreciated. Thanks

Assignment Problem:
Spoiler





public class Geometry {

	public static float areaCircle( float radius ) {
		if( radius < 0 )
			System.out.println("Can not have negative numbers");
		return (float) (Math.PI * ( radius * radius ));
	}
	public static float areaRectangle(float area){
		if( area < 0 )
			System.out.println("Can not have negative numbers");
		return (float) ( area * area );
		
	}
	public static float areaTriangle(float base){
		if( base < 0 )
			System.out.println("Can not have negative numbers");
		return (float) ( base * height * 0.5 );
	}
}



import java.util.Scanner;

class GeometryTester {

	public static void main( String [] args ) {
		Scanner sc = new Scanner( System.in );
		boolean run = true;
		do {
			System.out.println("Please select from the following menu... ");
			System.out.println(" 1. Calculate the Area of a Circle");
			System.out.println(" 2. Calculate the Area of a Rectangle");
			System.out.println(" 3. Calculate the Area of a Triangle");
			System.out.println(" 4. Quit");
			System.out.print("Please make your selection ==> ");
			String selection = sc.nextLine();
			int choice = Integer.parseInt( selection );

		switch( choice ) {
		case 1 :
			System.out.println("You Entered, 1. Calculate the Area of a Circle");
			System.out.print("Enter the radius of the circle ==> ");
		float c = Float.parseFloat( sc.nextLine() );
			System.out.println( "The area is " + Geometry.areaCircle( c ) );
		break;
		case 2:
			System.out.println("You Entered, 2. Calculate the Area of a Rectangle");
			System.out.print("Enter the Length of the circle ==> ");
			System.out.print("Enter the Width of the circle ==> ");
		float r = Float.parseFloat( sc.nextLine() );
			System.out.println( "The area is " + Geometry.areaRectangle( r ) );
		break;
		case 3:
			System.out.println("You Entered, 3. Calculate the Area of a Triangle");
			System.out.print("Enter the radius of the circle ==> ");
		float t = Float.parseFloat( sc.nextLine() );
			System.out.println( "The area is " + Geometry.areaTriangle( t ) );
		case 4:
			run = false;
		break;
		default:
			System.out.println( "you must enter 1,2,3,or 4");
			}
	}   while( run = true );
			System.out.println("Thanks for using our program");
	}
}

This post has been edited by weemanpow3: 01 March 2010 - 05:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Geometry Class program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: Geometry Class program

Posted 01 March 2010 - 05:31 PM

I see in your areaTriangle() method, you reference a variable height, but I don't see it declared in your Geometry class nor is it a param in the method.
 public static float areaTriangle(float base){ 
                if( base < 0 ) 
                        System.out.println("Can not have negative numbers"); 
                return (float) ( base * height * 0.5 ); 
        } 



Also, even with the if statements checking to make sure the params are greater than 0 in each method, they will still return the value of the area passed. If you think about it for circle and rectangle, negative radius or area params will just multiply out to positive. However, the triangle area will not multiply out the negatives b/c you don't multiply it by another number.

Now in order to improve upon your idea of validation (as you are on the right track, and good job for looking ahead!), you should validate user input before passing it to the area methods. Something like so:
case 1:
   if(c < 0){ //so if c < 0, tell the user such
      System.out.println("Invalid param");  
      break;  //and break out of the switch statement
   }

   //otherwise, the area will print
   System.out.println(Geometry.areaTriangle(c));
   break; //then you will break out of this case



Also, you might find this helpful that Scanner has a nextFloat() method, which works exactly like the next() method, except it returns a float, not a String. Note that if the next token isn't a float, it will throw an exception, but so will parseFloat() if you pass a String.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1