# CS 150, Spring 2018

## Scientific Formulas

#### Objectives of this Assignment

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

#### Instructions

Create a Java program called P2 with a class named P2, which after created should contain a file named P2.java in the src folder. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the main method. For this assignment, you must follow the directions below exactly:

Part One
1. Declare additional variables named cubeSideLength, cubeVolume, mass, acceleration, and force of type double.
2. Prompt the user for cubeSideLength, by printing "Length of cube? " using System.out.print() directly after you prompted the user for the triangle values.
3. Read the number entered by the user into the cubeSideLength variable using the Scanner method nextDouble().
4. Compute the volume of the cube based on the following formula: volume = Math.pow(cubeSideLength, 3).
5. The call Math.pow(), utilizes the exponential functionality that is defined in the Math class of Java.
6. Print the volume to the console using System.out.printf(), with 5 digits after the decimal point in the given format below.

7. Part Two
8. Prompt the user for the mass by printing "Mass? ".
9. Read the number entered in the console into the mass variable.
10. Prompt the user for the acceleration by printing "Acceleration? ".
11. Read the number entered in the console into the acceleration variable.
12. Compute the force based on the following formula: force = mass * acceleration.
13. Print the force to the console using System.out.printf(), with 1 digit after the decimal point and using the format below.
14. Hint: If System.out.printf("The triangle area is %.5f.\n", area); is a statement that prints the area with 5 digits after the decimal point,
how might we print the force with 1 digit after the decimal point?
15. The sample output below shows the required format of console output. Remember to include a new line after the last line of output.
16. #### Program Structure

```// Assignment: P2
// Author: Russ Wakefield
// Date:   1/31/2018
// Class:  CS150
// Email:  waker@colostate.edu

import java.util.Scanner;

public class P2 {

public static void main(String[] args) {

// Declare variables for formulas.

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

// Prompt and read triangle values and length of cube from keyboard.

// Calculate triangle area and cube volume.

// Print area and volume to console.
System.out.printf("The triangle area is %.5f.\n", triangleArea);

// Prompt and read mass and acceleration from keyboard.

// Compute the force.

// Print force to console.

// Close scanner.
keyboardInput.close();
} // end main
} // end class P2
```

#### Sample Output

Your program should print eight lines, including the first three, where the user enters the input values, the fourth and fifth, which print the results of the formulas that compute the area and volume. On the sixth and seventh lines, the user enters the mass and acceleration, and on the eighth line the result of the force formula is printed. User input is shown in blue.

```Base of triangle? 1.23
Height of triangle? 2.34
Length of cube? 4.44
The triangle area is 1.43910.
The cube volume is 87.52838.
Mass? 5.5
Acceleration? 6.6
The force is 36.3 newtons.
```

#### 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.

• 100 points for perfect submission.
• 0 points for no submission, will not compile, submitted class file, etc.
• Initial Tests
Initial tests use the input shown above.
• testCompile: checks that program compiles. (0 points)
• test1: checks first line of output with the prompt and triangle base entered by user. (5 points)
• test2: checks the second line of output with the prompt and the triangle height entered by user. (5 points)
• test3: checks third line of output with the prompt and cube side length enter by user. (10 points)
• test4: checks fourth line of output with the triangle area. (10 points)
• test5: checks fifth line of output with the cube volume. (10 points)
• test6: checks sixth line of output with the prompt and mass entered by user. (10 points)
• test7: checks seventh line of output with the prompt and acceleration entered by user. (10 points)
• test8: checks eighth line of output with the force. (10 points)