In: Computer Science
in java
Code Problem 2
Write a program that determines the income tax rates for a state. Tax rates are based on the salary according to the following table:
0 – 25000 Dollars 10%
25001 - 50000 Dollars 15%
50001 - 75000 Dollars 20%
Over 75000 Dollars 35%
Write the program so that it asks the user how many taxpayers should be processed and use the number of taxpayers to control the end of the program. For each taxpayer, you will need to input the taxpayer ID and salary. Use a while loop to validate the taxpayer ID to make sure that it is a number that does not exceed 6 digits in length. Display the following message for an invalid taxpayer ID length: Invalid taxpayerID. Please enter a number that is < 7 digits. Display the taxpayer ID, salary, tax percent, and taxes owed for each taxpayer processed within the loop. Taxes owed is calculated by multiplying the salary * tax % (depending on how you write the program you may need to divide the tax % by 100) when calculating the taxes owed. Use a printf command to display the output in 2 columns and make sure that the percents are formatted with 1 decimal place and the salary and taxes owed are formatted with 2 decimal places. When the program ends, a message of Earn money. Pay taxes. Enjoy life! should be displayed. To help you test your program, below is should run with 4 test cases and no invalid taxpayer IDs:
Be sure to include comments at the top of your program to indicate your name and the name of the program and date.
Please enter the number of taxpayers you want to process.
4
Please enter the taxpayer ID (123456, 234567, 999999 etc.) for taxpayer 1
123456
Please enter the taxpayer salary:
25000
Taxpayer ID 123456
Salary 25000.00
Tax % 10.0
Taxes Owed 2500.00
Please enter the taxpayer ID (123456, 234567, 999999 etc.) for taxpayer 2
234567
Please enter the taxpayer salary:
50000
Taxpayer ID 234567
Salary 50000.00
Tax % 15.0
Taxes Owed 7500.00
Please enter the taxpayer ID (123456, 234567, 999999 etc.) for taxpayer 3
345543
Please enter the taxpayer salary:
75000
Taxpayer ID 345543
Salary 75000.00
Tax % 20.0
Taxes Owed 15000.00
Please enter the taxpayer ID (123456, 234567, 999999 etc.) for taxpayer 4
654321
Please enter the taxpayer salary:
75001
Taxpayer ID 654321
Salary 75001.00
Tax % 35.0
Taxes Owed 26250.35
Earn money. Pay taxes. Enjoy life!
Code:
import java.util.Scanner;
class tax
{
public static void main(String[] args)
{
int n;
Scanner scnr=new
Scanner(System.in);
System.out.println("Please enter
the number of taxpayers you want to process.");
n=scnr.nextInt();
/*Reading no of taxpayers*/
for (int i=1;i<=4 ;i++ )
{
System.out.println("\nPlease enter the taxpayer ID (123456, 234567,
999999 etc.) for taxpayer "+i);
int
taxpayerId=scnr.nextInt();
/*Reading
taxpayer id*/
while(taxpayerId>999999)
{
/*if grater than 6 digits we ask him to enter a
number less than 7 digits*/
System.out.println("Please enter a number <7
digits.");
taxpayerId=scnr.nextInt();
}
System.out.println("Please enter the taxpayer salary:");
double
salary=scnr.nextDouble();
/*Reading
salary*/
System.out.printf("\nTaxpayer ID\t\t%d",taxpayerId);
System.out.printf("\nSalary\t\t\t%.2f",salary);
double
tax;
/*Printing name
and salary*/
if(salary<=25000.0)
{
tax=(salary*0.10);
System.out.print("\nTax%\t\t\t10.0");
/*If less than 25000 we print tax as 10%*/
}
else
if(salary<=50000.0)
{
tax=(salary*0.15);
System.out.print("\nTax%\t\t\t15.0");
/*If less than 50000 we print tax as 15%*/
}
else
if(salary<=75000.0)
{
tax=(salary*0.20);
System.out.print("\nTax%\t\t\t20.0");
/*If less than 75000 we print tax as 20%*/
}
else
{
tax=(salary*0.35);
System.out.print("\nTax%\t\t\t35.0");
/*If greater than 75000 we print tax as
35%*/
}
System.out.printf("\nTaxes Owed\t\t%.2f",tax);
/*Printing
tax*/
}
System.out.println("\nEarn money.
Pay taxes. Enjoy life!");
}
}
Output:
Indentation: