0

Write a JAVA program that calculates the surface area of a cylinder, C= 2( π r2) + 2 π r h, where r is the radius and h is the height of the cylinder. Allow the user to enter in a radius and a height. Format the output to three decimal places. Use the constant PI and the method pow() from the Math class.

This is what I've done so far but it can not run. Can anyone help me ?

import Java.util.Scanner;

public class Ex5 {

    public static void main (String[] args)
    {
      Scanner input=new Scanner(System.in);
      double radius,height,area;
      System.out.print("Please enter the radius of the Cylinder");
      radius = input.nextDouble();
      System.out.print("Please enter the height of the Cylinder");
      height = input.nextDouble();
      area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
      System.out.println("The surface of the cylinder" + area);  
    }
}
3
  • 10
    Please add the error messages you're getting. I see several things that look like typos, but I'd rather teach you to understand the errors than answer the question for you.
    – atk
    Mar 20, 2010 at 1:04
  • Thanks for your concern. Can you help me to have the correct answer for this one. I can understand by myself if I have the correct answer. Thanks a lot.
    – LC.
    Mar 20, 2010 at 1:17
  • 3
    @LC - I think you are wrong. IMO, you will learn how to program better if you do the hard work of finding and fixing your own mistakes than if you are simply spoon-fed the right answer. Programing is about understanding what you are doing, not just learning right answers.
    – Stephen C
    Mar 20, 2010 at 3:02

5 Answers 5

3

If this is the source code verbatim, then there are quite a few issues here.

First, The line:

2*Math.PIMath.POW

You forgot to put an asterisk between PI and Math, so the compiler sees PIMath as a symbol. It might be good to put parentheses to see this:

2*(Math.PI)*(Math.pow(radius, 2))

Note that Math.PI is a statically referenced constant. Math.pow, on the other hand, is a method.

All identifiers in Java are case sensitive. Math.pow exists, Math.POW does not.

Also, I've noticed that you are not using the height variable at all, so your formula calculation is incorrect.

2

It might just be a typo, but you seem to have either copied your code wrong, or typed it in wrong. you have the line

area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);

Notice the section

*Math.PIMath

you need to have a * in between PI and Math.

Also for future reference, did you know that you can add this line to the top of your code:

import static java.lang.Math.*;

Once done, you can change that line to:

area = (4*PI*radius)+2*PI*POW(radius,2);

Then again,some people may not like this. But I think it makes for nicer code to directly import a few lines like that.

1

As a matter of personal taste, I tend to add parens to related statements. It makes code more readable, and can help with debugging later.

I believe that the error is in the line:

area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);

I would change it to:

area = (2 * Math.PI * Math.pow(radius, 2)) + (2 * Math.PI * radius * height);

There is a typo in the original: Java is case-sensitive, and thus Math.POW() is different, programmically, than Math.pow(). Also, you cannot multiply by concatenating the commands; rather you must put an asterisk between them.

That should get it to compile.

1
  • I think he needs to add the *height to the second part of the expression or this cylinder will be a two-sided disk.
    – Uri
    Mar 20, 2010 at 1:25
1

You can spare yourself the need to use Math.pow() by substituting radius * radius. Even better, factor 2πr out of the formula for the area of a Cylinder. For example, A = 2πr2 + 2πrh = 2πr(r + h).

double area = 2 * Math.PI * radius * (radius + height);
0

What most likely is the problem is the part in the code where area is being calculated. Programming is very literal, so when there is no space between Math.PI and Math.POW(radius, 2) this causes an error. To the human eye they seem like two different expressions, but the compiler reads it as one since it was not told otherwise. Simply adding a * between Math.PI and Math.POW(radius, 2) should solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.