If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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

New circle, Enter radius:

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.

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.

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.