15-110 FALL 2009 [CORTINA]

LAB 3:

A person owes taxes on his/her income to the federal government each year. A simplified version of the rules for the current year are summarized as follows.

A person's total income consists of the sum of his/her wages, unemployment compensation and bank interest. A person may file as a single person, or may file jointly as part of a married couple. If filing jointly, the total income is computed based on the total of the couple. The standard deduction to the total income is $5150 for a single person or $10300 for a married couple filing jointly. Additionally, if this person/couple cannot be claimed as dependent(s) on another's tax return, they can deduct an additional $3300(single) / $6600(married). The total income minus any deduction(s) is this person's taxable income. If this value is negative, the taxable income is set to $0.

EXERCISES

  1. Using Eclipse, write a simple Java program in a project named Lab3 that contains a class named TaxCalculator with a single main method that prints out the taxable income for a person. Follow these steps:

    1. Use the Scanner class to read in the following data:

      • The filing status (S or s for single, M or m for married).
      • The dependent status (N or n for non-dependent, D or d for dependent).
      • The total wages earned during the year in dollars (an integer).
      • The total unemployment compensation earned during the year in dollars (an integer).
      • The total bank interest earned during the year in dollars (an integer).

      Input the values in the order specified. Remember to prompt the user for each input with clear instructions. (You may assume all data values entered match the required type, and for numerical data, the values will not be negative.)

      HINT FOR READING IN STATUS: Use nextLine() to read in the user's input for married status and dependents. Then use toUpperCase() to convert the input to uppercase and then use charAt(0) to extract out the first character. Then match that character with one of the options.

      Example (assuming scan is the variable that references our Scanner):

      System.out.println("Input your marital status (S/s, M/m):"); 
      String userInput = scan.nextLine();     
      char status = userInput.toUpperCase().charAt(0); 
      ... 
      if (status == 'S') ... 
      

      WARNING! Do not declare a variable inside of a block. If you do, it will not be accessible outside of the block.

      Example (BAD):

      if (status == 'S') { 
      	int deduction = 5100; 
      } 
      ... 
      income -= deduction;	// syntax error: deduction variable is unknown
      

    2. Compute the total income by adding up the wages, unemployment compensation and the bank interest.

    3. Subtract the appropriate deduction for a single person or married person, whichever applies.

    4. Subtract the appropriate deduction if the person(s) cannot be claimed as dependents, if applicable.

    5. If the total income is now negative, reset it to 0.

    6. Output the total income after deductions. This is the taxable income.

  2. Modify your program so that it computes the taxes due based on the taxable income using the following table:

    TAXABLE INCOME     TAX FORMULA (compute as an integer) 
    At most $25000     5% of taxable income 
    $25001 - $59999    $1250 + 10% of amount of taxable income above $25000 
    At least $60000    $4750 + 20% of amount of taxable income above $60000 
    

    For example, if a person's taxable income is $40000, then the tax due is $1250 + 10% of the taxable income above $25000 = $1250 + 10% of $15000 = $2750.

  3. What happens if you read in the numerical data first and then the string data afterwards? Why? Can you fix this so it works correctly? (HINT: nextInt() reads only the next integer input, nothing else, whereas nextLine() reads in the entire input line and discards the newline/enter character.)

HANDIN

At the end of lab, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1.