Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

Understanding order of operations Be familiar with some methods in Math Class Be able to generate random number by using Random class Know how to format the output by using DecimalFormat class Coding Guidelines for All Labs/Assignments (You will be graded on this)

What this Lab Is About:

Understanding order of operations
Be familiar with some methods in Math Class
Be able to generate random number by using Random class
Know how to format the output by using DecimalFormat class
Coding Guidelines for All Labs/Assignments (You will be graded on this)

Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
Keep identifiers to a reasonably short length.
Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
Use white space to make your program more readable.
Use comments properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs.

  1. Lab Description

For this Lab, you will have to write a java program that generates a random integer radius (r) and height (h) for a cylinder in the range 1 to 10, inclusive, and then computes the volume and surface area of the cylinder according to the following formular. Note: you need to format the output in such a way that it contains exactly 3 digits to the right hand side of the decimal point.

Volume = ¦Ðr2h

Surface Area = 2¦Ðrh (Note: the surface area here refers to the surrounding part, it does not count the two bases)

1.1 Step 1: Getting Started

Create a class called Lab3. Use the same setup for setting up your class and main method as you did in previous labs. Be sure to name your file Lab3.java (again, no empty spaces and special characters are allowed, follow the naming conventions).

For documentation purpose, at the beginning of each programming lab/assignment, you must have a comment block with the following information inside:

/————————————————————- // AUTHOR: your name // ASU ID: your 10 digits ASU ID // FILENAME: title of the source file // SPECIFICATION: description of the program // TIME SPENT: how long it took you to complete the lab //———————————————————–/

1.2 Step 2: Import all necessary packages

When we examine this programming task, we see that we will need to:

Use Random class to generaqte random numbers
Use Math class methods to do computation
Use DecimalFormat class to format the output
Random class is inside java.util package; Math class is inside java.lang package and DecimalFormat class is inside java.text package. Except for java.lang package, we will need to import the other two packages explicitly before we can use them.

import java.util.Random;
import java.text.DecimalFormat;

1.3 Step 3: Declaring Variables

When we examine this programming task, we see that we will need to declare two variables radius and height of int type to hold the cylinder’s radius and height respectively. We also need to declare volume and area of double type to hold the computing result of cylider’s volume and surface area.

1.4 Step 4: Create a Random object and Generate Random Numbers

Next, we will need to generate two random numbers in the range [1, 10] and assign them to above declared variable radius and height. To create any random numbers, first we need to create a Random object as follows:
Random rand = new Random();

Note above rand is just a variable’s name, you can name it something else. We then can use nextInt() method in Random class. If we pass it with a single integer value i(i > 1), such as int x = rand.nextInt(i), it will return a value that’s in the range from 0 to i-1(inclusive) and assign it to x. In case we want to generte random number in the range 1 to i, we will need to write as:

int x = rand.nextInt(i) + 1;

Follow above example, generate two random number in the range 1 to 10 and assign them to variables radius and height.

1.5 Step 5: Create a DecimalFormat object

In this step, we will need to create a DecimalFormat object and will use it later to format the output so that volume and area contains exactly 3 decimal digits. We can do this by creating a pattern such as:

DecimalFormat fmt = new DecimalFormat(“0.000”);

1.6 Step 6: Compute and output the Cylinder’s Volume and Area

In this step, we will use the formular provided on top to compute the cylinder’s volume and area. Here we will use Math.PI (a constant) and Math.pow() function to do the computation. For example,

double y = Math.pow(x, z); //is equivalent to y = xz

Read the lecture note #7 posted on Blackboard for omore methonds in Math class. After we successfully compute the volume and surface area of the cylinder, we then show the output on screen, such as:

System.out.println(“Volume:\t\t” + fmt.format(volume));

  1. Sample Run of the Program

For this program there’s no input from user part, the program will randomly generate two numbers and show the output, for example in following format:

Radius: 1
Height: 7
Volume: 21.991
Surface Area: 43.982

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"