The objectives of this lab are
Take out a clean sheet of paper. In the upper right hand corner print your name, your esus e-mail address, and your lab section time (e.g., 2:00-4:00).
If you are an advanced Java student, try P5.18 in the book. Others should do the remainder of this lab as given below.
Examine the following program and then answer the questions below it. You can also find this program on pages 204-206 in your book.
/**
A tax return of a taxpayer in 1992.
*/
class TaxReturn
{
/**
Constructs a TaxReturn object for a given income and
marital status.
@param anIncome the taxpayer income
@param aStatus either SINGLE or MARRIED
*/
public TaxReturn(double anIncome, int aStatus)
{
income = anIncome;
status = aStatus;
}
public double getTax()
{
double tax = 0;
if (status == SINGLE)
{
if (income <= SINGLE_CUTOFF1)
tax = RATE1 * income;
else if (income <= SINGLE_CUTOFF2)
tax = SINGLE_BASE2
+ RATE2 * (income - SINGLE_CUTOFF1);
else
tax = SINGLE_BASE3
+ RATE3 * (income - SINGLE_CUTOFF2);
}
else
{
if (income <= MARRIED_CUTOFF1)
tax = RATE1 * income;
else if (income <= MARRIED_CUTOFF2)
tax = MARRIED_BASE2
+ RATE2 * (income - MARRIED_CUTOFF1);
else
tax = MARRIED_BASE3
+ RATE3 * (income - MARRIED_CUTOFF2);
}
return tax;
}
public static final int SINGLE = 1;
public static final int MARRIED = 2;
private static final double RATE1 = 0.15;
private static final double RATE2 = 0.28;
private static final double RATE3 = 0.31;
private static final double SINGLE_CUTOFF1 = 21450;
private static final double SINGLE_CUTOFF2 = 51900;
private static final double SINGLE_BASE2 = 3217.50;
private static final double SINGLE_BASE3 = 11743.50;
private static final double MARRIED_CUTOFF1 = 35800;
private static final double MARRIED_CUTOFF2 = 86500;
private static final double MARRIED_BASE2 = 5370;
private static final double MARRIED_BASE3 = 19566;
private double income;
private int status;
}
import javax.swing.JOptionPane;
/**
A class to test the TaxReturn class.
*/
public class TaxReturnTest
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(
"Please enter your income:");
double income = Double.parseDouble(input);
input = JOptionPane.showInputDialog(
"Please enter S (single) or M (married)");
int status = 0;
if (input.equalsIgnoreCase("S"))
status = TaxReturn.SINGLE;
else if (input.equalsIgnoreCase("M"))
status = TaxReturn.MARRIED;
else
{
System.out.println("Bad input.");
System.exit(0);
}
TaxReturn aTaxReturn = new TaxReturn(income, status);
System.out.println("The tax is "
+ aTaxReturn.getTax());
System.exit(0);
}
}
On your worksheet write down the following information. This is not a quiz, so if you have questions about any of these questions, ask the TA or one of the consultants.
Again, if you are having a struggle with any of the programming concepts in this part of the lab, be sure to ask the consultants or the TA for help.
Write a short program that has two classes, one called ThreeNumbers and one called TestThreeNumbers. The Class ThreeNumbers is to have three private instance fields for number , number2, and number3, all of type int. It only needs a default constructor. It must have methods called setNumber1, setNumber2, setNumber3, getMax, and getMin with the obvious meanings. The class TestThreeNumbers is to be the usual sort of test class that has a public static void main method used to test the ThreeNumbers class. In this class you are to include code that
Turn your worksheet in before leaving the lab.