# CS 163/164, Spring 2018

## Scientific Formulas

#### Objectives of this Assignment

1. To write a Java program to implement formulas,
2. to learn how to read input from the user via the console,
3. to declare variables and use them in expressions, and
4. to learn about printing formatted output to the console.

#### Instructions

Write a Java program file called P2.java with a class name P2, an example of the code is shown below. All of the Java code you write should be contained in the main method. For this assignment, you must follow the directions below exactly:

• Create a project in Eclipse called P2.
• Create a class in the project called P2.
• Let Eclipse create a main method in P2.java.
• Copy the comment block below and personalize it
• All variables and code should reside in the main method!
• Declare variables named radius, circumference, area, and volume of type double.
• Create a Scanner object to read console input, as shown in the code below.
• Prompt the user for a radius, by printing "Radius? " using System.out.print().
• Read the number entered by the user into the radius variable using the Scanner method nextDouble().
• Compute the circumference of the circle based on the following formula: circumference = 2πr.
• The value r in all formulas is the radius.
• Print the circumference to the console using System.out.printf(), with 5 digits after the decimal point.
• Compute the area of the circle based on the following formula: area = πr2.
• Print the area to the console using System.out.printf(), with 5 digits after the decimal point.
• Compute the volume of the circle (sphere) based on the following formula: volume = 4/3*π*r3.
• Print the volume to the console using System.out.printf(), with 5 digits after the decimal point.
• You must use the value of Math.PI for π in all calculations!
• Declare variables named energy and mass of type double.
• Declare the variable speedOfLight of type double, initialized to 299792458.0.
• Prompt the user for the mass, by printing "Mass? " using System.out.print().
• Read the number entered by the user into the mass variable using the Scanner method nextDouble().
• Compute the energy based on the following formula: energy = mass(speedOfLight2).
• Print the energy and mass to the console using System.out.printf(), with 1 digit after the decimal point.
• The sample output below shows the desired format.

#### Program Structure

```// P2 Assignment Solution
// Author: Russ Wakefield
// Date:   1/21/2017
// Class:  CS163/164
// Email:  waker@colostate.edu

import java.util.Scanner;

public class P2 {

public static void main(String[] args) {

// Declare variables for geometric formulas

// Instantiate scanner
Scanner keyboard = new Scanner(System.in);

// Prompt and read radius from keyboard

// Calculate circumference, area, and volume

// Print circumference, area, and volume to console

// Declare variables for converting mass to energy

// Prompt and read mass from keyboard

// Compute the energy using the formula

// Print energy to console

// Close scanner
keyboard.close();
}
}
```

#### Sample Output

Your program should print six lines, including the first, where the user enters the radius, the second, third, and fourth, which print the results of the formulas that compute the circumference, area, and volume. On the fifth line, the user enters the mass, and on the sixth line the result of the energy conversion formula is printed. The user input is shown in blue.

```Radius? 1.0
The circumference is 6.28319.
The area is 3.14159.
The volume is 4.18879.
Mass? 1.0
The energy is 89875517873681760.0 joules.
```

#### Specifications

• Work on your own, as always.
• The name of the source code file must be exactly
` P2.java `
• Name the file exactly - upper and lower case matters!
• Comments at the top as shown above.
• Assignments should be implemented using Eclipse.
• Assignments should be implemented using Java, version 1.8.
• Make sure your code runs on machines in the CSB 120 lab.
• Turn in through the Checkin tab.
• Read the syllabus for the late policy.
• We will be checking programs for plagiarism, so please don't copy from anyone else.

#### Grading Criteria

• 100 points for perfect submission.
• 0 points for no submission, will not compile, submitted class file, etc.
• Preliminary Tests
Preliminary tests use the value of 1.0 for the radius and mass.
• testCompile: checks that program compiles. (0 points)
• test1: checks first line of output with the prompt and radius entered by user. (10 points)
• test2: checks second line of output with the circumference. (10 points)
• test3: checks third line of output with the area. (10 points)
• test4: checks fourth line of output with the volume. (10 points)
• test5: checks fifth line of output with the prompt and mass entered by user. (10 points)
• test6: checks sixth line of output with the energy. (10 points)
• Final Tests
Final tests use arbitrary values for the radius and mass.
• test7: checks second line of output with the circumference. (10 points)
• test8: checks third line of output with the area. (10 points)
• test9: checks fourth line of output with the volume. (10 points)
• test10: checks sixth line of output with the energy. (10 points)
• Final grading includes the preliminary tests.

Submit P2.java to Checkin.