Control Flow

Objective

In this assignment, you will create a Java program. Requirements are given below. This assignment will test your knowledge of Control Flow (Conditional) Statements in Java.

Instructions

When you are developing console applications using Java, you must read input from users through the console. To read a string from the console as input in Java applications, you can use the Scanner class along with System.in.

  1. Write a program that declares 1 integer variable x, and then assigns 7 to it. Write an if statement to print out “Less than 10” if x is less than 10. Change x to equal 15, and notice the result (console should not display anything).

  2. Write a program that declares 1 integer variable x, and then assigns 7 to it. Write an if-else statement that prints out “Less than 10” if x is less than 10, and “Greater than 10” if x is greater than 10. Change x to 15 and notice the result.

  3. Write a program that declares 1 integer variable x, and then assigns 15 to it. Write an if-else-if statement that prints out “Less than 10” if x is less than 10; “Between 10 and 20” if x is greater than 10 but less than 20, and “Greater than or equal to 20” if x is greater than or equal to 20. Change x to 50 and notice the result.

  4. Write a program that declares 1 integer variable x, and then assigns 15 to it. Write an if-else statement that prints “Out of range” if the number is less than 10 or greater than 20 and prints “In range” if the number is between 10 and 20 (including equal to 10 or 20). Change x to 5 and notice the result.

  5. Write a program that uses if-else-if statements to print out grades A, B, C, D, F, according to the following criteria:

    • A: 90-100

    • B: 80-89

    • C: 70-79

    • D: 60-69

    • F: <60 Use the Scanner class to accept a number score from the user to determine the letter grade. Print out “Score out of range” if the score is less than 0 or greater than 100.

  6. Write a program that accepts an integer between 1 and 7 from the user. Use a switch statement to print out the corresponding weekday. Print “Out of range” if the number is less than 1 or greater than 7. Do not forget to include “break” statements in each of your cases.

  7. Create a program that lets the users input their filing status and income. Display how much tax the user would have to pay according to status and income. The U.S. federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: Single, Married Filing Jointly, Married Filing Separately, and Head of Household. The tax rates for 2009 are shown below:

Marginal Tax RateSingleMarried Filing Jointly or Qualifving Widow(er)Married Filing SeparatelyHead of Household

10%

$0 - $8.350

$0 - $16,700

$0 - $8.350

$0-$11,950

15%

$8.351 - $33.950

$16,701 - $67,900

$8.351 - $33,950

$11,951- $45,500

25%

$33,951 - $82,250

$67,901-$137,050

$33,951-$68,525

$45,501 - $117.450

28%

$82,251- $171.550

$137.051 - $208,850

$68.526 - $104,425

$117,451 - $190,200

33%

$171,551 - $372.950

$208.851 - $372.950

$104,426 - $186,475

$190,201 - $372,950

35%

$372,951+

$372,951+

$186,476+

$372.951+

package PracticeAssignment;

import java.util.Scanner;

public class ControlFlow {
    public static void main(String[] args) {
//        int x = 7;
//        x = 15;
//        if (x < 10) {
//            System.out.println("Less than 10");
//        }

//        int x = 7;
//        x = 15;
//        if (x < 10) {
//            System.out.println("Less than 10");
//        } else {
//            System.out.println("Greater than 10");
//        }

//        int x = 15;
//        x = 50;
//        if (x < 10) {
//            System.out.println("Less than 10");
//        } else if (x >= 20) {
//            System.out.println("Greater than or equal to 20");
//        } else {
//            System.out.println("Between 10 and 20");
//        }

//        int x = 15;
//        x = 5;
//        if (x < 10 || x > 20) {
//            System.out.println("Out of range");
//        } else {
//            System.out.println("In range");
//        }

//        Scanner input = new Scanner(System.in);
//        System.out.print("Enter your score: ");
//        int score = input.nextInt();
//        if (score < 0 || score > 100) {
//            System.out.println("Score out of range");
//        } else if (score >= 90) {
//            System.out.println("A");
//        } else if (score >= 80) {
//            System.out.println("B");
//        } else if (score >= 70) {
//            System.out.println("C");
//        } else if (score >= 60) {
//            System.out.println("D");
//        } else {
//            System.out.println("F");
//        }

//        Scanner input = new Scanner(System.in);
//        System.out.print("Enter a number between 1 and 7: ");
//        int number = input.nextInt();
//        switch (number) {
//            case 1:
//                System.out.println("Monday");
//                break;
//            case 2:
//                System.out.println("Tuesday");
//                break;
//            case 3:
//                System.out.println("Wednesday");
//                break;
//            case 4:
//                System.out.println("Thursday");
//                break;
//            case 5:
//                System.out.println("Friday");
//                break;
//            case 6:
//                System.out.println("Saturday");
//                break;
//            case 7:
//                System.out.println("Sunday");
//                break;
//            default:
//                System.out.println("Out of range");
//                break;
//        }

        Scanner input = new Scanner(System.in);

        System.out.print("Enter your filing status (Single, Married Filing Jointly, Married Filing Separately, Head of Household): ");
        String status = input.nextLine();

        System.out.print("Enter your taxable income: ");
        double income = input.nextDouble();

        double tax = 0.0;

        if (status.equals("Single")) {
            if (income <= 8350) {
                tax = income * 0.10;
            } else if (income <= 33950) {
                tax = 8350 * 0.10 + (income - 8350) * 0.15;
            } else if (income <= 82250) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
            } else if (income <= 171550) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
            } else if (income <= 372950) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
            } else {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
            }
        } else if (status.equals("Married Filing Jointly")) {
            if (income <= 16700) {
                tax = income * 0.10;
            } else if (income <= 67900) {
                tax = 16700 * 0.10 + (income - 16700) * 0.15;
            } else if (income <= 137050) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
            } else if (income <= 208850) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
            } else if (income <= 372950) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
            } else {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
            }
        } else if (status.equals("Married Filing Separately")) {
            if (income <= 8350) {
                tax = income * 0.10;
            } else if (income <= 33950) {
                tax = 8350 * 0.10 + (income - 8350) * 0.15;
            } else if (income <= 68525) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
            } else if (income <= 104425) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
            } else if (income <= 186475) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
            } else {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
            }
        } else if (status.equals("Head of Household")) {
            if (income <= 11950) {
                tax = income * 0.10;
            } else if (income <= 45500) {
                tax = 11950 * 0.10 + (income - 11950) * 0.15;
            } else if (income <= 117450) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
            } else if (income <= 190200) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
            } else if (income <= 372950) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
            } else {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
            }
        }

        System.out.printf("Tax amount: $ %.2f", tax);
    }
}

Last updated