Lab 5


Objectives

Since we had a test and a holiday this week I am going to have some review and introduce a new topic on this lab. The new topic is the if - if/else statement. It is a condition statement that allows your program to decide where it should go next based on certain conditions. The format of an if/else statement is in chapter 5. You should look - read through this before you attempt this lab.

The objectives of this lab are

Worksheet

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).

Levels

If you are an advanced Java student, try P5.18 in the book.  Others should do the remainder of this lab as given below.

Program Analysis

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);
   }
}

To Do

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.

  1. the names of all of the classes written as part of this program.
  2. the names of all of the classes imported for use in this program.
  3. the names of all of the attributes (instance fields) in TaxReturn.  Along with each attribute, write down whether that attribute is a variable or constant, its type, and whether it is public or private.
  4. the names of all of the methods (do not include constructors) in TaxReturn.  Along with each method list the return type of that method and the names and types of any parameters for that method (if there are none, write "none").
  5. the names of all of the constructors for TaxReturn.  Along with each constructor list the parameters and their types.
  6. Repeat 3-6 for TaxReturnTest

The Java if Statement

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

  1. Run your program on input 5, 10, and 20.  On your worksheet, print what is output from the calls to getMax and getMin.
  2. Run your program on input -5, -10, and -20. On your worksheet, print what is output from the calls to getMax and getMin.
  3. Run your program on input 5, 5, and 5. On your worksheet, print what is output from the calls to getMax and getMin.
  4. Try other input of your choice. On your worksheet, print what is output from the calls to getMax and getMin.

To Turn In

Turn your worksheet in before leaving the lab.