Assignment 02

Task

Print the requested details of two cones to the screen.

Textbook: 4.3
New concepts: expressions, precedence, type conversion; Math methods and constants.

Steps

Write a program that starts with a radius (r) and a height (h) and prints out the following details for a right circular cone:

Use only double variables (not ints). For π, use the pre-defined Java constant Math.PI. You may create additional variables as needed. However all output/results should change appropriately when only the initial value of the radius and/or height variable is changed. So, as in A01a, the values assigned to your radius and height variables will each occur only once in your code.

Print out the radius and height and the result of each of the above calculations. Print one result per line. Be sure to label what each value is in your output. Thus, your output should be formatted something like this:

For a cone with radius 6.0 and height 8.0:
Slant height = 10.0
Volume = 301.59289474462014
Lateral surface area = 188.49555921538757
Total surface area = 301.59289474462014

Then repeat the calculations and output for another cone of radius 7.5 and height 10.0. (Put a blank line between your two cones so your output is easy to read.) You may reuse your existing variables or else create new ones. However, again, the literals 7.5 and 10 should each occur only once in your code.

What to Submit

Upload your UsernameA02.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Variables
You use a variable, not a literal, for both the radius and the height in all your calculations and printing (0.6). You display the value of the radius and height of each cone before you do the rest of the calculations for that cone (0.4). All output and results should still be correct if only the initial value of radius or height variable is changed.
2 - Calculations
Program displays the correct results of the 4 cone calculations for each cone (so with r=6.0 and h=8.0, and then with r=7.5 and h=10.0), properly labelled, one per line. (0.25 each)

FAQs

So wait--the program I'm submitting is not going to have the same output as shown above?
The first half of your output should look something like that shown above. But then you'll also print the dimensions of a second cone, using the same format.
How do I use the value of π?
Use Math.PI and it will give you a very precise version of π. So, for example:
  double circumference = 2 * Math.PI * radius;
where radius is a variable you already declared and initialized before this line.
How do I compute the square root (√)?
Use Math.sqrt(). So, to take the square root of 9, you could do something like this:
  double answer = Math.sqrt(9);
You could also replace the literal 9 here with a variable or a longer expression.
How do I compute powers, such as r3?
There is a way to do it using the Math.pow() function, such as in double rSquared = Math.pow(radius, 2). However, it's not really necessary for this assignment. You can just compute it "manually" if you want: radius * radius.
My volume calculation isn't coming out correctly!
Be careful of integer math. Remember that System.out.println(1 / 3); prints out 0, not 0.3333333. So you want to use double (floating point) literals, rather than int (integer) literals here.