Colorado State University

Recitation R4 - Conditionals
Spring 2014

CS160: Foundations in Programming


The purpose of this lab is to learn about the conditionals in Java and when to use them: For this lab, first create a new project and class in Eclipse called R4. Check the box to have Eclipse add the main method for you. The following should be coded in the main method:

If Statements

  1. Instantiate a scanner.
  2. Prompt the user with "Enter an integer: ", using System.out.print().
  3. Use the scanner to read an integer from the user (the console) and store it into a variable.
  4. Write an if statement with a condition that is true when the number is negative, and false otherwise.
  5. When the number is negative, print a line with "Negative number: " and the number, and assign the negative of the number back to itself.
  6. When the number is Positive, print a line with "Positive number: " and the number.
  7. After the conditional, print a line with "Absolute value: " and the number.

If, Else-If Block Statements

  1. Prompt the user with "Enter a double: ", using System.out.print().
  2. Have your scanner take in a double and store it into a variable named salary.
  3. Create a new double named taxes. Leave it uninitialized
  4. Write an if/else block that satisfies the following tax brackets
  5. Print "Taxes: $" + taxes to the console.
  6. Create an object of type DecimalFormat, initialized with "0.00".
  7. Modify the printing of the taxes variable to use the formatter.
  8. Verify you see exactly two digits after the decimal point.
  9. Is it possible that taxes will not have a value by the time it gets printed?
    Write a comment explaining why, or why not.

Show your R4.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2014 CS160 Colorado State University. All Rights Reserved.