CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    geometry calculator

    I need help my program won't compile correctly, I've been working on it for
    4 days now, and i am feeling very stressed/burned out:

    import java.util.Scanner;

    class GeometryCalculator {

    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");
    }
    }


    and

    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 );

    }
    }


    Thank you in advance

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: geometry calculator

    Post the code in code tags and post the compiler messages you are getting.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: geometry calculator

    Code:
    		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 ) );
    
    
    		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 ) );
    Check my bolding - are you serious?

    No wonder you're confused - you'll confuse the users as well as yourself with prompts like that!

    Remember, a rectangle has a length and a width. You need both to calculate its area. A triangle has a base and a height, you need both to calculate its area.

    The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
    T. Cargill
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured