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
- Instantiate a scanner.
- Prompt the user with "Enter an integer: ", using System.out.print().
- Use the scanner to read an integer from the user (the console) and store it into a variable.
- Write an if statement with a condition that is true when the number is negative, and false otherwise.
- When the number is negative, print a line with "Negative number: " and the number,
and assign the negative of the number back to itself.
- When the number is Positive, print a line with "Positive number: " and the number.
- After the conditional, print a line with "Absolute value: " and the number.
If, Else-If Block Statements
- Prompt the user with "Enter a double: ", using System.out.print().
- Have your scanner take in a double and store it into a variable named
salary
.
- Create a new double named
taxes
. Leave it uninitialized
- Write an if/else block that satisfies the following tax brackets
- when 0 ≤
salary
< $1,000 then:
you pay no taxes.
- when $1,000 ≤
salary
< $10,000 then:
you pay 15% of your salary in taxes.
- when $10,000 ≤
salary
< $50,000 then:
you pay 23% of your salary in taxes.
- when $50,000 ≤
salary
< $100,000 then:
you pay 28% of your salary in taxes.
- otherwise if your salary is ≥ $100,000 then:
you pay a flat $200,000 tax.
- Print
"Taxes: $" + taxes
to the console.
- Create an object of type DecimalFormat, initialized with "0.00".
- Modify the printing of the
taxes
variable to use the formatter.
- Verify you see exactly two digits after the decimal point.
- 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.