# programming help!!??

• 04-13-2011, 12:55 AM
lestweremember
programming help!!??
Hi I keep receiving errors every time I try to compile the following:

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

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

}
}

• 04-13-2011, 12:58 AM
Junky
This forum is for Java developers not psychics. If you get error messages then how about including the EXACT messages in your post.
• 04-13-2011, 01:29 AM
ra4king
And also, please use the code tags:
[ code] code goes right here [ /code ] without the spaces
And format with spaces (preferably 3-4 spaces for a tab)
• 04-13-2011, 01:37 AM
lestweremember
sorry bout that,
"Geometry.java:18: cannot find symbol
symbol : variable height
location: class Geometry
return (float) ( base * height * 0.5 );
^
1 error"

I don't know how/where to declare "height"

Code:

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

``` 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 ); } }```
it's supposed to be a geometry calculator:

1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit

Enter your choice ( 1- 4):
Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu.

I truly appreciate it.
• 04-13-2011, 01:43 AM
Junky
Code:

`public static float areaRectangle(float area){`
If the method is supposed to calculate the area, why are you passing it in as a parameter?
Code:

```public static float areaTriangle(float base){     if( base < 0 )         System.out.println("Can not have negative numbers");     return (float) ( base * height * 0.5 ); }```
:confused: You can handle the base but have no idea how to handle the height?
• 04-13-2011, 01:44 AM
Junky
Code:

```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 ==> "); System.out.println("You Entered, 3. Calculate the Area of a Triangle"); System.out.print("Enter the radius of the circle ==> ");```
Also, your print statements do not make sense.
• 04-13-2011, 01:58 AM
ra4king
For "height", make it another parameter, so the user types in 2 doubles
• 04-13-2011, 02:05 AM
Junky
Quote:

Originally Posted by ra4king
For "height", make it another parameter, so the user types in 2 doubles

Really? Do we have to spell it out for them? How about expecting them to think for themselves. They should be able to work it out for themselves from my nudge.
• 04-13-2011, 02:09 AM
ra4king
Oops hehe :)