# CS 150 - Spring 2018

## Lab 4 - More Practice with Variables, the Scanner Class, and Console Printing

#### Wednesday, February 7, 2018

Lab 3 Regrade: If you need a regrade for last week's lab, please open Lab 3 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. Additionally, if you forgot to take the quiz last week for the single attendance point, your maximum grade for Lab 3 is now 3 out of 4.

#### Objectives of this Lab

1. Write a Java program that accepts user input,
2. assigns the input to specific variables,
3. performs mathematical calculations on those variables, and
4. practice printing formatted output to the console.

#### Starting the Assignment

• Create a new Java Project using the (File → New → Project → Java Project) and name it Lab4.
• Right click on Lab4 in the Package Explorer tab and choose (New → Class).
• Each class in Java has an associated file of the same name. Name this class Lab4 (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 Lab4 class, your Lab4 project should contain a file named Lab4.java in the src folder.

#### Instructions

All of the Java code you write should be contained in the main method. For today's lab, your TA will guide you through part one, then you will complete part two on your own. Additionally, you must follow the directions below exactly:

Part One
1. Import the Scanner library into your program.
2. Declare two variables of type double named myDouble and myDoubleSquareRoot.
3. Create a Scanner object to read from the keyboard.
4. Next, prompt the user for a double value by printing "Enter a decimal value: ".
5. Read the value entered by the user into myDouble using the Scanner method nextDouble().
6. Then calculate the square root of myDouble using Math.sqrt() and assign the value to myDoubleSquareRoot.
7. The call Math.sqrt(), utilizes the square root method that is defined in the Math class of Java.
8. Print the square root value to 2 decimal places after the prompt "Square root = ".
10. Now print the result to 4 decimal places after the prompt "Square root + 9.99 = ".
11. Lastly, print myDoubleSquareRoot casted to an int after the following prompt "Square root casted to int: ".
12. To finish part one, print one blank line to the console.
13. You can now run your program by clicking the green "Play" shaped button on the toolbar.
14. Please run your programs using the green play circle ("Run"), not the green circle within a square ("Launch").
15. Note that the output will appear in the Console window at the bottom or side of your Eclipse window.
16. 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.

17. Part Two
18. Declare two integers named iOne and iTwo.
19. Prompt the user for an integer value by printing "Enter an integer for iOne: ".
20. Read the value entered by the user into iOne using the appropriate Scanner method.
21. Prompt the user for a second integer value by printing "Enter an integer for iTwo (>=3): ".
22. Read the value entered by the user into iTwo using the appropriate Scanner method.
23. Next print out the value of iOne combined with the post-increment operator as well as the value of iTwo combined with the post-decrement operator. These values should follow the prompt "Post operator results: iOne = ", then concatenate the post-increment of iOne, then concatenate " and iTwo = ", followed by the post-decrement of iTwo.
24. Now print out the value of iOne combined with the pre-increment operator and the value of iTwo combined with the pre-decrement operator. These values should follow the prompt "Pre operator results: iOne = " as well as include " and iTwo = " between the values.
25. Next print the quotient and remainder of iOne by iTwo. Perform the division and modulo operations within the parentheses of println in order to avoid creating new variables. There is nothing incorrect about creating new variables to hold the results, we are just performing the operations in line for practice.
26. To print the result of division, start by printing "Division of ", then concatenate iOne, then concatenate " and ", followed by iTwo, " = ", and finally calculate the quotient.
27. To print the result of modulo, start by printing "Modulo of ", then concatenate iOne, then concantenate " and ", followed by iTwo, " = ", and finally calculate the remainder.
29. Note: Why did the prompt for iTwo ask users to enter a value greater than or equal to 3? See for yourself what happens if a user enters the value 2 for iTwo.

#### Program Structure

```import java.util.Scanner;

// Assignment: Lab4
// Author: Jess Cobb
// Date: 07 February 2018
// Class: CS150
// Email: jesscobb@rams.colostate.edu

public class Lab4 {

public static void main(String[] args) {
// Declare two doubles named myDoubleSquareRoot and create a Scanner.

// Write prompt to console and assign user input to myDouble.

// Calculate the square root of that number and print to console with 2 decimal points.

// Using the compound add operator, add 9.99 to myDoubleSquareRoot and print to 4 decimal places.

// Cast myDoubleSquareRoot to an int and print to console.

// Declare two integers named iOne and iTwo.

// Prompt user and initialize integers.

// Print out the post-increment of iOne and the post-decrement of iTwo.

// Print out the pre-increment of iOne and the pre-decrement of iTwo.

// Next print iOne/iTwo and iOne%iTwo.

// Close Scanner.

} // end main

} // end Lab4
```

#### Sample Output

User input is shown in blue.

```Enter a decimal value: 15.235
Square root = 3.90
Square root + 9.99 = 13.8932
Square root casted to int: 13

Enter an integer for iOne: 4
Enter an integer for iTwo (>=3): 9
Post operator results: iOne = 4 and iTwo = 9
Pre operator results: iOne = 6 and iTwo = 7
Division of 6 and 7 = 0
Modulo of 6 and 7 = 6
```

• Log in to Canvas and open the Lab 4 Initial attendance quiz on a department machine.
• Next, answer the first question of the quiz. Then, raise your hand and show the TA your program output.
• Remember to take the quiz for the attendance point even if you do not finish the lab.