University of Texas
CS305j, Introduction to Computing
Discussion Section Handout #4
Conditional Execution
1. Using conditional execution
Write a method that prints out commentary on the result of a football game. The
method takes two parameters, team one's score and team two's score.
The commentary is based on the absolute value of the difference between the
scores.
Instead of using the Math class' absolute value method calculate the absolute
value of the difference between the scores yourself.
absolute value
of difference commentary
0 "The game was a tie. A tie?!?!?"
1 - 6 "The game was a real barn burner."
7 - 14 "A close game."
15 - 30 "What a blowout."
> 30 "People paid money to see that?"
After the commentary print out the score and who won the game, team 1 or team 2.
(You don't know the names of the teams so just refer to them as team 1 and team
2.)
Write tests for you method in the main method of the program and run them.
2. Writing methods that return values
Write a method tax that takes a salary as a parameter and that returns the
amount of federal tax you would owe if you make that salary. The tax is
computed based on your tax bracket as described in the table below. You use the
first two columns to find the appropriate bracket. Once you know which row of
the table to use, start with the "flat amount" and add the "+%" of the amount
over the amount listed in the final column.
For example, if your income is $50,000, then you use the third row of the table
and compute the tax as $4,220 plus 25% of the amount over $30,650.
Over But not over Flat amount +% Of excess over
-------------------------------------------------------------
$0 $7,825 $0 10% $0
$7,825 $31,850 $782.50 15% $7,825
$31,850 $77,100 $4,386.25 25% $31,850
$77,100 unlimited $15,698.75 28% $77,100
You may assume that your method is passed a value of type double and you should
return a double. Don't worry about whether or not the tax works out to an even
amount of money (dollars and cents). Just compute the answer as a double using
the formula. You may assume that your method is passed a value greater than or
equal to 0.
Note, there are actually 2 more brackets beyond those listed, but we will keep
the problem simple.
3. Evaluating code.
Consider the following method
// assume x > 0
public static int mystery(int x){
int total = 0;
while( x > 0 ){
total++;
x = x / 3;
}
return total;
}
What is returned by the following method calls?
mystery(0)
mystery(1)
mystery(6)
mystery(9)
mystery(100)
mystery(1000)