CS 150, Fall 2017
Programming Assignment - P2
Scientific Formulas
Due - September 12th, 2017 at 6:00pm
Late - September 13th, 2017 at 8:00am
Objectives of this Assignment
- To write a Java program to implement formulas,
- to learn how to read input from the user via the console,
- to declare variables and use them in expressions, and
- to 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. 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 Java project in Eclipse called P2.
- Create a new class in the P2 project and let Eclipse create a main method in P2.java.
- Copy the comment block below and personalize it if you have not set up a default comment header.
All variables and code should reside in the main method!
- Declare variables named triangleSideLength, cubeSideLength, triangleArea, and cubeVolume of type double.
- Create a Scanner object to read console input, as shown in the code below.
- Prompt the user for triangleSideLength, by printing "Length of triangle? " using System.out.print().
- Read the number entered by the user into the triangleSideLength variable using the Scanner method nextDouble().
- Prompt the user for cubeSideLength, by printing "Length of cube? " using System.out.print().
- Read the number entered by the user into the cubeSideLength variable using the Scanner method nextDouble().
- Compute the area of the triangle based on the following formula: area = (Math.sqrt(3)/4) * triangleSideLength * triangleSideLength.
The call Math.sqrt(), utilizes the square root functionality that is defined in the Math class of Java.
- Print the area to the console using System.out.printf(), with 5 digits after the decimal point, as shown in the code below.
- Compute the volume of the cube based on the following formula: volume = Math.pow(cubeSideLength, 3).
- Print the volume to the console using System.out.printf(), with 5 digits after the decimal point.
- Declare variables named mass, acceleration, and force of type double.
- 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().
- Prompt the user for the acceleration, by printing "Acceleration? " using System.out.print().
- Read the number entered by the user into the acceleration variable using the Scanner method nextDouble().
- 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.
- Hint: If System.out.printf("The triangle area is %.5f.\n", triangleArea); 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
// P2 Assignment
// Author: Russ Wakefield
// Date: 8/21/2017
// Class: CS150
// 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 keyboardInput = new Scanner(System.in);
// Prompt and read length of triangle and length of cube from keyboard
// Calculate area and volume
// Print area and volume to console
System.out.printf("The triangle area is %.5f.\n", triangleArea);
// Declare variables for calculating force
// Prompt and read mass and acceleration from keyboard
// Compute the force using the formula
// Print force to console
// Close scanner
keyboard.close();
} // end main
} // end class P2
Sample Output
Your program should print seven lines, including the first and second, where the user enters
the two lengths, the third and fourth, which print the results of the formulas
that compute the area and volume. On the fifth and sixth lines, the user
enters the mass and acceleration, and on the seventh line the result of the force formula
is printed. User input is shown in blue.
Length of triangle? 1.0
Length of cube? 1.3
The triangle area is 0.43301.
The cube volume is 2.19700.
Mass? 1.0
Acceleration? 2.25
The force is 2.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.
- Preliminary Tests
Preliminary tests use the value of 1.0 for the triangle side length, 1.3 for the cube side length, 1.0 for mass, and 2.25 for acceleration.
- testCompile: checks that program compiles. (0 points)
- test1: checks first line of output with the prompt and triangle side length entered by user. (5 points)
- test2: checks second line of output with the prompt and cube side length enter by user. (5 points)
- test3: checks third line of output with the triangle area. (10 points)
- test4: checks fourth line of output with the cube 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 prompt and acceleration entered by user. (10 points)
- test7: checks seventh line of output with the force. (10 points)
- Final Tests
Final tests use arbitrary values for each length, mass and acceleration.
- test8: checks third line of output with the triangle area. (15 points)
- test9: checks fourth line of output with the cube volume. (15 points)
- test10: checks seventh line of output with the force. (10 points)
- Final grading includes the preliminary tests.
Submit P2.java to Checkin.