0% found this document useful (0 votes)
18 views7 pages

PA 303.4.1 - Practice Assignment Control Flow (Conditional) Statements

The document contains examples of Java code using conditional statements like if-else, switch statements, and calculating tax brackets using arrays.

Uploaded by

jewelcsebu045
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

PA 303.4.1 - Practice Assignment Control Flow (Conditional) Statements

The document contains examples of Java code using conditional statements like if-else, switch statements, and calculating tax brackets using arrays.

Uploaded by

jewelcsebu045
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

public static void main(String[] args){


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

public static void main(String[] args){


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

2.
public static void main(String[] args){
int x = 7;
if (x < 10) {
System.out.println("Less than 10");
} else {
System.out.println("Greater than 10");
}
}

public static void main(String[] args){


int x = 15;
if (x < 10) {
System.out.println("Less than 10");
} else {
System.out.println("Greater than 10");
}
}
3.
public static void main(String[] args){
int x = 15;
if (x < 10) {
System.out.println("Less than 10");
} else if (x >= 10 && x < 20) {
System.out.println("Between 10 and 20");
} else {
System.out.println("Greater than or equal to
20");
}
}

public static void main(String[] args){


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

4.
public static void main(String[] args){
int x = 15;

if (x >= 10 && x <= 20) {


System.out.println("In range");
} else {
System.out.println("Out of range");
}
}
public static void main(String[] args){
int x = 5;

if (x >= 10 && x <= 20) {


System.out.println("In range");
} else {
System.out.println("Out of range");
}
}

5.
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the score: ");
int score = scanner.nextInt();
scanner.close();

if (score < 0 || score > 100) {


System.out.println("Score out of range");
} else if (score >= 90 && score <= 100) {
System.out.println("A");
} else if (score >= 80 && score <= 89) {
System.out.println("B");
} else if (score >= 70 && score <= 79) {
System.out.println("C");
} else if (score >= 60 && score <= 69) {
System.out.println("D");
} else {
System.out.println("F");
}
}

6.
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter an integer between 1 and 7:
");
int number = scanner.nextInt();
scanner.close();

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;
}
}
7.
public static void main(String[] args) {

double[][] taxBrackets = {
{0, 8350, 33950, 82250, 171550, 372950},
// Single
{0, 16700, 67900, 137050, 208850, 372950},
// Married Filing Jointly
{0, 8350, 33950, 68525, 104425, 186475},
// Married Filing Separately
{0, 11950, 45500, 117450, 190200, 372950}
// Head of Household
};

double[] taxRates = {0.10, 0.15, 0.25, 0.28, 0.33,


0.35};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filing status (Single,
Married Filing Jointly, Married Filing Separately,
Head of Household): ");
String filingStatus = scanner.nextLine();
System.out.print("Enter income: ");
double income = scanner.nextDouble();
scanner.close();
int filingIndex = -1;
switch (filingStatus.toLowerCase()) {
case "single":
filingIndex = 0;
break;
case "married filing jointly":
filingIndex = 1;
break;
case "married filing separately":
filingIndex = 2;
break;
case "head of household":
filingIndex = 3;
break;
default:
System.out.println("Invalid filing
status.");
return;
}
double taxAmount = calculateTax(income,
taxBrackets[filingIndex], taxRates);
System.out.println("Tax amount: $" + taxAmount);
}

public static double calculateTax(double income,


double[] taxBrackets, double[] taxRates) {
double tax = 0;
double remainingIncome = income;
for (int i = 0; i < taxBrackets.length - 1; i++) {
if (remainingIncome <= taxBrackets[i + 1]) {
tax += remainingIncome * taxRates[i];
break;
} else {
tax += (taxBrackets[i + 1] -
taxBrackets[i]) * taxRates[i];
remainingIncome -= (taxBrackets[i + 1] -
taxBrackets[i]);
}
}
return tax;
}

You might also like