I have tried to figure this out and am having major problems with math pow. Any help would be great...

This project deals with reading input from the user, storing the input into variables and displaying those variables. This program is an introductory project into the basic data types of int, double and the String class.
The following identifiers will be used:
int diameter, height;
double circumference, areaOfCircle, cylVolume;
String name;

You will utilize the Scanner class plus the print() and println() methods of
System.out. You will also define a double named constant whose value is 3.1415926.
Prompt the use for their name and using the nextLine() method, read the name of the
user.

Once the name of the user has been read, greet the user. Proceed to get the diameter and
height of some aluminum can (cylinder) using the nextInt() method. You MUST
read the data in this order so that the automatic grading program supplies data in the
correct order.

Calculate the circumference of the base of the can, the area of the base (using the pow() method of the Math class) and the volume of the cylinder. Display the radius, diameter, circumference, area of the base and volume of the cylinder.

Your output should resemble (user provided values in bold):
Hello, Chalandria. You have two more answers to provide.
Enter the integer diameter of a cylinder: 5
Enter the integer height of the cylinder: 8
The diameter is 5.
The height is 8.
The circumference is 15.707963.
The area of the base is 19.63495375.
The volume of the cylinder is 157.07963.

5
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by coil

The math.pow() function is simple to use. It accepts 2 arguments:
math.pow(double a,double b)...where a is the base number and b is the exponent. So, for example, to write 3 squared in java, you use the math.pow() method as follows:

Math.pow( 3.0 , 2.0 );//returns 9 (3*3)
//4 to the power of 5:
Math.pow( 4.0 , 5.0 );//returns 1024...etc

Now, you could use int's instead of doubles, but you would then need to cast the Math.pow() method to return int values, because it returns double value by default.

Edited by Akill10: n/a

This works as intended however I'm not sure if it is done correctly as instructed per the assignment. Again any help would be great. Thanks

import java.util.*;

public class p2
{
static Scanner kb = new Scanner(System.in);

public static void main(String[] args)
{

int diameter, height;
double circumference, areaOfCircle, cylVolume;
String name;

name = kb.next();

System.out.println("Hello, " + name + " , You have two more answers to
provide.");

System.out.println("Enter the interger diameter of a cylinder: ");
diameter = kb.nextInt();

System.out.println("Enter the interger height of a cylinder: ");
height = kb.nextInt();

System.out.println("The radius is " + diameter * 0.5 + ".");

System.out.println("The diameter is " + diameter + ".");

areaOfCircle = (diameter * 0.5) * (diameter * 0.5);

System.out.println("The height is " + height + ".");

System.out.println("The circumference is " + diameter * 3.1415926 +
".");

System.out.println("The area is " + areaOfCircle * 3.1415926 + ".");

cylVolume = areaOfCircle * 3.1415926 * height;

System.out.println("The volume of the cylinder is " + cylVolume +
".");

}
}

1. It doesn't look like you are using (or need) Math.pow, so why was that the title of your original post?
2. The assignment says to use a named constant for the value 3.1415926. You are not doing that in your code.
3. The assignment says to use nextLine method when prompting the user for her/his name. You are not doing that in your code.

1. It doesn't look like you are using (or need) Math.pow, so why was that the title of your original post?
2. The assignment says to use a named constant for the value 3.1415926. You are not doing that in your code.
3. The assignment says to use nextLine method when prompting the user for her/his name. You are not doing that in your code.

Youll have to excuse me I am really new to programming so I just wrote it how I could figure it out. I don't know how to do what is mentioned which is why I am asking for help.
1. I am guessing I need to incorporate Math.pow in but I just don't know how.
2. Again I don't know how to use or make a named constant for PI
3. And well again I am new to all this programming that I dont know how to do the nextLine method when prompting the user.
I have only had 2 days of class to cover what is being asked which is why I came here for help with java.

A constant in any programming language is a value you don't intend to change and thus want it to be 'constant'. The speed of light would be an example. For a constant variable, use the keyword final.

final double PI = 3.14 //or however much precision you want

The final keyword locks the variable from changing by anyone.

You should study the Math Documentation for Java. It is extensive and will go over any math functionality you may need.

You need to study the Documentation for the Scanner Class to be informed on proper usage of nextLine().

Use the Java Docs, they are your friend.

Edited by brandonrunyon: n/a

For circles, $$A=\pi r^{2}$$. Make a constant named PI (see above post) and then square r with Math.pow().

The pow() method takes two parameters, a base and the exponent. Thus, Math.pow(a,b) = a^b.

Edited by coil: n/a