This program will use your knowledge of input and output, formatting, relational and logical operators, if-else statements, int/double/char/String variables, constants, and avoiding runtime errors.
You are to write a program named Program2.java. It will calculate per capita (per person) income and state income tax. Your prompts and output should be formatted exactly like the examples below. Your program will do the following:
1.Prompt the user for their name.
2.Get the name from the user and store it.
3.Prompt the user for the salary, commissions, and bonus (no commas or dollar sign).
4.Get the salary, commissions, and bonus and store them.
5.Prompt the user to input the number of dependents.
6.Get the number of dependents from the user and store it.
7.Instead of a prompt, display a menu, which lists various states that the user might be from, according to the example below. BE SURE THAT THE MENU LISTS THE STATES IN THE SAME ORDER AS THE EXAMPLES BELOW.
8.Get the state from the user (actually, get the letter on the menu that corresponds to the state, see notes below). The user should be able to type in either the lower or upper case letter and have it mean the same thing. If the letter that is input does not correspond to a state, print Illegal letter for state and then stop the program by using the single statement return; after the statement that prints the error.
9.Skip a line before starting the output and print the users name.
10.Calculate and print the total income (salary + commissions + bonus). Indent it and everything below by one tab.
11.Print the number of dependents.
12.Calculate and print the adjusted income. Take the total income and subtract 1000 for each dependent. However, if the adjusted income comes out to be negative, set it to 0 instead.
13.Calculate and print the tax (adjusted income * tax rate for the state). The tax is as follows:
Arizona 6%
Florida, Iowa 4%
California 8%
Hawaii, Nevada 7%
14.Calculate and print the per capita income (total income / number of dependents).
Notes:
You will want to read in a character in order to get the letter from the menu. However, the Scanner class does not have a method (operation) that will read in a single character. So this is how you can do it (assume you have defined a Scanner named kb):
letter = kb.next().charAt(0);
(Note that this puts together 2 statements: the kb.next() returns the next token or word and then the .charAt(0) tells that token to return its first character. Here .next() is used instead of .nextLine() because .next() will skip over any white space (including newlines) so it does not have to worry about a previous ENTER that might have been left on the input stream.)
The variables that will hold money should be doubles (because of decimals). However, they will not automatically print out like money. After your program is working, make the following changes to have them print out like money.
1.Put the following line first in your program (before the public class Program2)
import java.text.NumberFormat;
2.Just before you want to start printing things as money, put the following line:
NumberFormat money = NumberFormat.getCurrencyInstance();
3.Then, every time you want to print out a money variable, you can first format it to look like money. Instead of using the variable in the print statement, use money.format(< the variable>). For example, if you have a variable called totalIncome, you can change it from this
System.out.println(tTotal income:/t/t + totalIncome);
to this:
System.out.println( "tTotal income:/t/t" + money.format(totalIncome));
Before you put in money.format statements, your results may have had 12 or so decimal places. You may have noticed that the answer was not quite exact. This is generally true of decimal arithmetic (Java is doing base10 arithmetic using a base2 number system).
You do not have to do any error checking unless the directions specifically ask for it. For example, if the user enters a negative number for salary, you do not have to check for that. However, there is a potential problem in this assignment if the user puts in 0 dependents, your code may try to divide by 0. In most languages, this is a runtime error and stops the program.
You should check to see if you are dividing by zero when you calculate per capita income. If you are, skip the statement(s) that calculate and print per capita income.
This program has another good opportunity to use constants (final in java). Constants should be all uppercase and used when a number will not change in the program. Decide what should be defined as final and implement it that way.
Since this program requires input, do not use the Capture Output preference in TextPad. If you try to Capture Output, the input will not work.
Example 1 (your prompts and output should be formatted like this, The bold part of what you see below is user input):
Please input your name
Steve Nash
Please input salary, commissions, and bonus (no commas or dollar sign)
30000.00 5025.73 2000.00
Please input number of dependents (including yourself)
2
Please choose which state you are from (by letter)
A Arizona
B California
C Florida
D Hawaii
E Iowa
F Nevada
A
Steve Nash
Total income: $37,025.73
Dependents: 2
Adjusted income: $35,025.73
Tax: $2,101.54
Per Capita income: $18,512.86
Example 2:
Please input your name
Eli Manning
Please input salary, commissions, and bonus (no commas or dollar sign)
25000 2453.45 0
Please input number of dependents (including yourself)
3
Please choose which state you are from (by letter)
A Arizona
B California
C Florida
D Hawaii
E Iowa
F Nevada
G
Illegal letter for state
Example 3:
Please input your name
Jim Brown
Please input salary, commissions, and bonus (no commas or dollar sign)
2000 0 0
Please input number of dependents (including yourself)
4
Please choose which state you are from (by letter)
A Arizona
B California
C Florida
D Hawaii
E Iowa
F Nevada
b
Jim Brown
Total income: $2,000.00
Dependents: 4
Adjusted income: $0.00
Tax: $0.00
Per Capita income: $500.00
Notes about testing:
1.Be sure and test it with all other input than the example above (although that is a good start). Thinking up good test cases is a good skill to have. Your test cases should always include border cases what happens if I dont put in anything, what happens if I put in bad data, what happens if I put in different cases that I know will trigger an if statement, etc.
Comments and indenting:
At the top of the program should be a short comment which describes its purpose. It should also have your name and class on a separate line.
In the code itself, indent inside the class and then again inside main. Also indent inside if statements and the else part. Put a blank line and then a comment as needed to describe sections of code.