# Geometry Class program

Page 1 of 1

## 1 Replies - 12646 Views - Last Post: 01 March 2010 - 05:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=159169&amp;s=4e1fb01eb55ebafa382c579382a51b00&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 weemanpow3

Reputation: 0
• 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 ) {
System.out.println("Can not have negative numbers");
}
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(" 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");
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

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• 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.