CS 1401, Exam #3

Date: Thursday, November 3, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________


 
Question 1. Designing with Inheritance. Consider the following problem statement:

The Miner's shipping and boxing supply specialists are selling various types of boxes. All boxes have a size and a maximum weight it can carry. There are three different types of boxes for sale: corrugated boxes, heavy duty boxes and mailers. Corrugated boxes can carry up to 200 pounds. For corrugated boxes, there are cube, long and flat boxes. Each of the shape comes in different sizes. Heavy duty boxes can carry up to 300 pounds. They come in either double wall or single wall, or as a wooden crate. They have different sizes but all have similar shapes. Mailers are either white literature mailers(including video mailers and mug boxes,) brown corrugated mailers and heavy duty mailers for shipping things like tools, glassware and other small items. The heavy duty mailers have thicker walls for better protection. You are writing a software implementation to help in the inventory of the company.

(a) Draw the inheritance hierarchy.



























(b) Write a CRC card for the class CorrugatedBox. (It is not necessary to list responsibilities.)












Question 2. Implementing Derived Classes

The Miner's shipping and boxing supply specialists is designing a database to track shipments. Read the following problem statement and the code below. The provided code defines a superclass Shipment. Write the code for the subclass BulkShipment. For this subclass, include a class definition header, any instance variables needed, and a constructor taking three arguments. Also, override any superclass method that must be implemented in a different way within the subclass.

All shipments have an address, a weight and a date for when it was shipped. There are several kinds of shipments. One type of shipment is a bulk shipment. This requires a large number of shipment of the same size and weight. Rates on a regular shipment is computed as a product of the mileage rate the distance and the weight. Rates on bulk shipments depend on the volume. If there are at less than 100 units, there is no discount. If there is at least 100 units, there is a 10% discount. For over 1000 units, there is a 25% discount.

public class Shipment
 {
    private String address;
	private GregorianCalendar shippedDate;
	private float weight;

	public Shipment(String inAddress, GregorianCalendar inDate, float inWeight)
	 {
	    address = inAddress;
		shippedDate = inDate;
		weight = inWeight;
     }

	public String getAddress()
	 { 
	    return address;
	 }

	public GregorianCalendar getShippedDate()
	 {
	    return shippedDate;
	 }

	public float getWeight()
	 {
	    return weight;
	 }

    // "set" methods are omitted, and you may ignore them

	// Compute the price of the shipment
	public float computePrice(float mileageRate, long distance)
	 {
	   return mileageRate*weight*distance;
	 }
}




Question 3. Even-Driven Input and Output (GUI)

Two versions of the same display window are presented below. The windows are part of a GUI for an online tracking system. When the user enters a tracking number, the system finds the shipment record and displays information. The "Before" version of the window below shows how the window first appears, before the user has done anything. The "After" version of the same window, shows how the window appears after the user entered a tracking number and clicked the button.

"Before" version

"After" version

On the following pages, you have most of the code needed to create the display windows shown above. However, some of the lines of code are missing.

(a) Which layout manager was used to create this window? Write the code needed to create and initialize the layout manager in the space below. Write an "A" on the handout to show where this code should be places.



(b) The code for one of the components in the window is missing from the code. Write the code needed to create and initialize it in the space below. Write a "B" on the handout to show where this code should be placed.





(c) Write the line of code to cause the component you defined in (b) above to be displayed in the window with the other components. Write a "C" on the handout to show where this code should be placed.



(d) What is the line of code needed so the user can quit the program by clicking the x on the top right of the window? Write a "D" on the handout to show where this code should be placed.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tracking  
{

  private static JTextField userInputField;  // Holds tracking number entered by user
  private static JLabel response;            // Holds details of shipment

  // Define Event Listener for both buttons.
  // Perform different actions based on button clicked.
  private static class ButtonHandler implements ActionListener  {
  
    public void actionPerformed(ActionEvent event)  
    {        
      String whichButton;  // Holds the button's name
      
      // Get the button's name.
      // This is how we tell which button has been clicked.
      whichButton = event.getActionCommand ( );
      
      if ( whichButton.equals ( "Find shipment" ) )   {    
          
          // Here put some code to find the shipment in the database
          
          response.setText("shipment not found");
          
      
        }  // end of if whichButton.equals "Find shipment"           
      
      // Else assume the button clicked is Reset. 
      // Better code would be to verify by comparing to "Reset".
      else  {
        // Reset the user input data field
        userInputField.setText("");
        // Reset the response message field
        response.setText(""); 
      }      
    }  // end of actionPerformed () method
  } // end of ButtonHandler class

  
   public static void main(String[] args)
   {

       JFrame ourFrame=new JFrame("Tracking");           

       ourFrame.setSize(600,100);
                                  
       Container ourPane=ourFrame.getContentPane();           
   
       JLabel instruction=new JLabel(
                 "Enter your tracking number");
       
       JButton findShipmentButton=new JButton("Find shipment"); 
       
       JButton resetButton=new JButton("Reset");       
              
       ButtonHandler ourEventListener=new ButtonHandler();
       
       findShipmentButton.addActionListener(ourEventListener); 
       resetButton.addActionListener(ourEventListener);              
       
       
       response=new JLabel("", JLabel.CENTER);
       
       ourPane.add(instruction);      
       ourPane.add(findShipmentButton);
       ourPane.add(resetButton);
       ourPane.add(response);       
            
       ourFrame.setVisible(true);
   }
    
}
























































Question 4. Exception handling

Below is a piece of code to handle exceptions. This code is part of the main method of an application.

try
  {
     System.out.println("Section A");
	 line = in.readLine();
	 average = total/nline;
	 System.out.println("Section B");
  }
catch (IOException ioErr)
  {
     System.out.println("Section C");
  }
finally
  {
     System.out.println("Section D");
  }
System.out.println("Section E");

(a) Show below what the output produced by this piece of code if no exception occurs?











(b) Show below what the output produced by this piece of code if the call to readLine tries to read beyond the end of the file.










(c) Show below what the output produced by this piece of code if the field nline is zero.

















Question 5. Extra Credit

Refer to Question 3 to answer this question. Write the needed line(s) of code to complete the following tasks, and write the letter "E" (plus "F", and so on, if you need to place the lines of code in different places) on the handout to show where the code should be placed:

(a) Create a new integer variable.





(b) Retrieve what the user typed as input for the tracking number and assign that input to your variable.









(c) Write a catch statement to deal with the NumberFormatException that would be caused if the user does not enter an integer.