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
- Declare additional variables named cubeSideLength, cubeVolume, mass, acceleration, and force of type double.
- Prompt the user for cubeSideLength, by printing "Length of cube? " using System.out.print() directly after you prompted the user for the triangle values.
- Read the number entered by the user into the cubeSideLength variable using the Scanner method nextDouble().
- Compute the volume of the cube based on the following formula: volume = Math.pow(cubeSideLength, 3).
The call Math.pow(), utilizes the exponential functionality that is defined in the Math class of Java.
- Print the volume to the console using System.out.printf(), with 5 digits after the decimal point in the given format below.
Part Two
- Prompt the user for the mass by printing "Mass? ".
- Read the number entered in the console into the mass variable.
- Prompt the user for the acceleration by printing "Acceleration? ".
- Read the number entered in the console into the acceleration variable.
- Compute the force based on the following formula: force = mass * acceleration.
- Print the force to the console using System.out.printf(), with 1 digit after the decimal point and using the format below.
- 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?
- The sample output below shows the required format of console output. Remember to include a new line after the last line of output.
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.
Grading Criteria
- 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)
- Additional Tests
The last 3 tests use arbitrary values for each length,
mass and acceleration.
- test9: checks fourth line of output with the triangle area. (10 points)
- test10: checks fifth line of output with the cube volume. (10 points)
- test11: checks eighth line of output with the force. (10 points)
- Final grading includes all tests.
Submit P2.java to Checkin.
© 2018 CS150 Colorado State University. All Rights Reserved.