# Circle calculator

• October 16th, 2005, 09:55 PM
rdeaton
Circle calculator
I need to design and code a Java program that calculates and prints the (D) diameter, the (C) circumference, or the (A) area of a circle, given the radius. The program inputs two data items: the calculation needed, and the radius of the circle. The program should continue to ask the user for values until the user enters 'Q' as the calculation type. The program should echo print the input data and prompt appropriately. Label the output and format to two decimal places. The 'N' option is for a new circle. Is there anyone that can help me??

Example:

Enter the calculation type (A, C, D, N, Q):

N

6.75

Enter the calculation type (A, C, D, N, Q):

A

The area of a circle with a radius of 6.75 is 143.14

D

The diameter of a circle with a radius of 6.75 is 13.50

Enter the calculation type (A, C, D, N, Q):

Q

Thank You.

I am new to Java and have no idea how to write the code for this to work right. Is there anyone that will take pity on a newbie? This is what I have so far. But I can't get it to work.

import java.util.Scanner; // uses class Scanner

public class circleCalculator
{
public static void main(String args[])

// create Scanner to obtain input
// prompt for input
{
Scanner input = new Scanner( System.in );
char option = '?'; // input character
String instring; // string
int area = 0;
int circumference = 0;
int diameter = 0;

do
{

System.out.println( "Enter calculation type (n = new, a = area, c = circumference, d = diameter, q = quit): " );
instring = input.next();
option = instring.charAt(0);

if ( option == 'N' || option == 'n' )
{
System.out.println( "Enter the radius of the circle: " );
instring = input.next();
} // end if statement
while ( radius > 0 )
{
if ( option == 'A' || option == 'a' )
System.out.printf( "The area of the circle is: %f.2\n", area );

if ( option = 'C' || option = 'c' )
circumference = (2 * 3.14159) * (double) radius;
System.out.printf( "The area of the circle is: %f.2\n", circumference );

if ( option == 'D' || option == 'd' )
diameter = 2 * (double) radius;
System.out.printf( "The diameter of the circle is: %f.2\n", diameter );
} // end while statement

} while ( option == 'Q' && option == 'q' );
System.out.println( "Thank You!" );

} // end main
• October 17th, 2005, 12:18 AM
gothic_type
Ok, I'm not going to do your assignment for you, but I will give you some pointers in the right direction:

1) Use either tabs or spaces to indent your code so that it doesn't all start at the left hand side of the page. Example:

Code:

```public static void main(String [] args) { System.out.print("This code is an example"); System.out.print(" of not using indents"); System.out.println(" and therefore should not be followed!"); }```
Code:

```public static void main(String [] args) {   System.out.print("This code is an example");   System.out.println(" of using indents.");   if(true)   {     System.out.println("Always indent your code after anywhere you would normally have {");     System.out.println("For Example, an if statement or a while loop");   } }```
2) Remember to use the "code" tags. This means that before any code that you post, type "code" (without the quotes) in between "[" and "]" (i.e. [ code ] without the spaces). After your code have [ /code ] (again without the spaces).

Ok, now here's some pointers for the code that you submitted:

Personally I would have used a switch statement on option, but since you might not have covered that yet, I'll ignore that.

Code:

```import java.util.Scanner; // uses class Scanner public class circleCalculator {   public static void main(String args[])   // create Scanner to obtain input   // prompt for input   {     Scanner input = new Scanner( System.in );     char option = '?'; // input character     String instring; // string     int radius = 0; // radius entered by user     int area = 0;     int circumference = 0;     int diameter = 0;     do     {       System.out.println( "Enter calculation type (n = new, a = area, c = circumference, d = diameter, q = quit): " );       instring = input.next();       option = instring.charAt(0); // 1) You could replace this with                                                         // option = instring.toLower().charAt(0);                                                         // and then you don't need to check for upper                                                         // and lower case characters below.       if ( option == 'N' || option == 'n' )       {         System.out.println( "Enter the radius of the circle: " );         instring = input.next();         radius = instring.Int();       } // end if statement       // 2) I wouldn't have a while loop here because what you have said is:       // "while the value of radius is greater than 0, execute the code between       // the braces" This means that once a user enters a value greater than       // zero, the program will just loop through this section outputting the       // same value over and over.       while ( radius > 0 )       {               // 3) Make sure you remember your braces in if statements because               // if you forget them your code can have a completely different meaning         if ( option == 'A' || option == 'a' )         {           area = (double) (radius * radius) * 3.14159;                     // 4) I can't be certain of this because I've never used the printf           // method in Java, but from what I could see, you should have           // %.2f NOT %f.2 But then again, I can't be certain of this.           System.out.printf( "The area of the circle is: %f.2\n", area );         }         if ( option = 'C' || option = 'c' )         {           circumference = (2 * 3.14159) * (double) radius;           System.out.printf( "The area of the circle is: %f.2\n", circumference );         }         if ( option == 'D' || option == 'd' )         {           diameter = 2 * (double) radius;           System.out.printf( "The diameter of the circle is: %f.2\n", diameter );         }       } // end while statement     } while ( option == 'Q' && option == 'q' );         System.out.println( "Thank You!" );         } // end main } // You were missing a } here, but that's a minor thing```
Notes on the above code:

1) "option = instring.toLower().charAt(0)" means convert the String, "instring", to lower case, then take the first character (the character at 0) and store it in option

2) Instead of having all the code in that section loop, what I would do is as soon as you take in radius, check to see if it's in the correct format (greater than zero) then do the next part:
Code:

```do {   System.out.print("Please enter radius:-");   instring = input.next();   radius  = Integer.parseInt(instring);   if(radius&lt;=0)     System.out.print("The radius must be greater than 0. "); } while(radius&lt;=0)```
What the above code does is keeps looping until the correct input has been entered.

3) You forgot your { and } after your if statements. When you don't put them in, the compiler assumes that only the statement after the if statement is to be executed inside the if statement. This means that your program would have outputted the area, circumference and diameter no matter what the user entered except that they would only receive the correct value for the one that they had asked for (since the other calculations would not have been performed).

There were some other things a bit dodgy with your program, but it's not my place to write it for you. Hope the above tips are some kind of help.

ac