CS 150 - Spring 2018
Lab 3 - Variable and Scanner Introduction, Formatted Printing, and the Start of P2
Wednesday, January 31, 2018
Lab 2 Regrade: If you need a regrade for last week's lab, please open Lab 2 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time.
Objectives of this Lab
- Write a Java program that accepts user input,
- assigns the input to specific variables, and
- learn about printing formatted output to the console.
Starting the Assignment
- Create a new Java project using the (File → New → Project → Java Project) and name it P2.
- Right click on P2 in the Package Explorer tab and choose (New → Class).
- Each class in Java has an associated file of the same name. Name this class P2 (Eclipse will add ".java" automatically).
- Have Eclipse add a main method to your program by checking the box next to
public static void main(String[] args)
.
- After creating the P2 class, your P2 project should contain a file named P2.java in the src folder.
Instructions
An outline of the code for P2 is shown below. You may copy and paste the comments into your main method if it will help you. All of the Java code you write should be contained in the main method. For this assignment, you must follow the directions below exactly:
- Copy the comment header below and personalize it if you have not set up a default comment header.
- Following this, import the Scanner library using
import java.util.Scanner;
.
All variables and further code should reside in the main method!
- Declare variables named triangleBase, triangleHeight, and triangleArea of type double.
- Create a Scanner object to read console input, as shown in the code below.
- Prompt the user for the triangle base, by printing "Base of triangle? " using System.out.print().
- Read the number entered by the user into the triangleBase variable using the Scanner method nextDouble().
- Prompt the user for the triangle height, by printing "Height of triangle? " using System.out.print().
- Read the number entered by the user into the triangleHeight variable using the Scanner method nextDouble().
- Compute the area of the triangle based on the following formula: triangleArea = 1.0/2.0 * triangleBase * triangleHeight.
- Print the area to the console using System.out.printf(), with 5 digits after the decimal point, as shown in the code below. Additionally, the format of your program output should exactly match the sample output below.
- You can now run your program by clicking the green "Play" shaped button on the toolbar.
- Please run your programs using the green play circle ("Run"), not the green circle within a square ("Launch").
- Note that the output will appear in the Console window at the bottom or side of your Eclipse window.
- You should see the program print your first prompt to the console. At this point your program is waiting for you to enter a value in to the console. Once you do so and press enter, the program will continue its execution.
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 geometric formula.
// Instantiate scanner.
Scanner keyboardInput = new Scanner(System.in);
// Prompt and read base and height of triangle from keyboard.
// Calculate area.
// Print area to console.
System.out.printf("The triangle area is %.5f.\n", triangleArea);
// Close scanner.
keyboardInput.close();
} // end main
} // end class P2
Sample Output
User input is shown in blue.
Base of triangle? 1.23
Height of triangle? 2.34
The triangle area is 1.43910.
- Log in to Canvas and open the Lab 3 attendance quiz on a department machine.
- Next, answer the first question of the quiz. Then, raise your hand
and show the TA that your output matches the sample output.
- Remember to take the quiz for the attendance point even if you do not finish the lab.
© 2018 CS150 Colorado State University. All Rights Reserved.