Selections: The IF Statement
The IF statement is a code structure that controls the flow of execution in your program. Below are several exercises for the IF statement.
Subtraction Quiz
You are to create a program called `SubtractionQuiz’ to teach a first grade child to learn subtractions.
-
The program randomly generates two single-digit integers
number1
andnumber2
withnumber1 >= number2
and displays a question to the student using the two numbers in the format likeWhat is 9 – 2?
where we assume number1
is 9 and number2
is 2.
- After the student enter the answer, the program displays whether the answer is correct.
To generate a single digit random number, you can use Math.random()
in the way below,
(int)(Math.random() * 10)
where Math.random()
generates a pseudo-random number between 0 and 1 where 0 is inclusive
while 1 exclusive.
Your program’s output should be exactly like the two examples below.
Example 1.
$ java SubtractionQuiz
What is 3 - 2? 1
Great! 1 is correct answer!
Example 2.
What is 8 - 3? 9
Oops! 9 is incorrect.
Body Mass Index
Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters, i.e,
\[I = \frac{W}{H^2}\]where \(I\) is BMI, \(W\) the weight in kilograms, and \(H\) the height in meters.
The interpretation of BMI for people 16 years or older is as follows:
BMI | Interpretation |
---|---|
BMI < 18.5 | Underweight |
18.5 <= BMI < 25.0 | Normal |
25.0 <= BMI < 30.0 | Overweight |
30.0 <= BMI | Obese |
You are to write a Java program called BodyMassIndexEvaluator
that reads a patient’s body weight
in pounds and height in feet and inches from the standard input, computes the BMI, and display the BMI
and its interoperation in the format in the example below,
$ java BodyMassIndexEvaluator
Enter the patient's weight in pounds: 180
Enter feet for the patient's height: 5
Enter inches for the patient's height: 11
BMI Calculation and Interpretation
Patient's BMI: 25.1
Interpretation: Overweight
Federal Income Tax
The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. For each filing status, there are tax brackets. The dollars of an income falls in the bracket is charged a tax per dollar based on a rate called the marginal tax rate.
The following table illustrates the four filing status and the tax brackets for year 2021-2022.
To under how the tax is calculated, let’s take a look at the following example:
How much does an individual pay if her/his (adjusted net) income is $48,000 under the single filing status?
tax = 9950 * 10% + (40525 – 9950) * 12% + (48000 – 40525) * 22%
With what we know now, it is doable to write a program that computes the federal income tax for all 4 filing status and all income brackets; however, it will be cumbersome. In this exercise, you are to write a programm
called FederalIncomeTaxForSingles
that
- prompts the user to enter the income from the standard input
- computes the federal income tax for all brackets
- displays the income and the tax on the standard output. For tax, round down to the whole dollar.
A sample run of the program is as follows,
$ java FederalIncomeTaxForSingles
Federal Tax Calculation for Singles
Enter adjusted net income ($): 48000
For adjusted income of 48000, the federal income tax is $6308.
which provides a reference as the expected output format.