CSU Banner

CS 150, Spring 2018

Programming Assignment - P2

Scientific Formulas

Due - February 6th, 2018 at 6:00pm

Late - February 7th, 2018 at 8:00am

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.


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.
        } // 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.


    Grading Criteria

    Submit P2.java to Checkin.

    © 2018 CS150 Colorado State University. All Rights Reserved.
    CS Banner
    CS Building