Colorado State University

Recitation R4 - Conditionals
Fall 2013

CS160: Foundations in Programming


The purpose of this lab is to learn about the conditionals in Java: and when to use them.

If Statements

  1. Create a new 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.
  2. Instantiate a scanner.
  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 conditional evaluating to true when the number from the user is negative.
    - The statement should turn the negative number positive and store it back in the variable.
  5. Print the number to the console.

If, Else-If Block Statements

  1. Have your scanner take in a double and store it into a variable named salary.
  2. Create a new double named taxes. Leave it uninitialized
  3. Write an if/else block that satisfies the following tax brackets
  4. Print taxes to the console
  5. Is it possible that taxes will not have a value by the time it gets printed?
    Write a comment explaining why, or why not.

Switch Statements

  1. Have your scanner take in a String and store it into a variable.
  2. Write a switch statement, that switches on the first character of the string, with at least these cases: - Don't forget your break statements!
  3. Add a comment explaining why someone might use a switch rather than an if-else.

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

© 2013 CS160 Colorado State University. All Rights Reserved.