CS 1401, Exam #3

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


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

The Miner's printing supply specialists are selling various types of printers. All printers have a brand name and model number. There are three different types of printers for sale: laser, inkjet and multifunction. Laser printers use toner. For laser printers, there are regular and wide format versions. Some printers are Mac compatible. Different laser printers differ in resolution, pages per minute, memory size and sheet input capacity. Wide format laser printers can accept paper larger than the standard and legal sizes. Ink jet printers use ink cartridges. Some are labeled photo printers, but all can actually print photos. Like laser printers, they differ in resolution, pages per minute, memory size and sheet input capacity. Multifunction printers also come in laser and inkjet versions. However, then can serve as copier and scanner. The scanner has a resolution that may differ from the printing resolution. 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 LaserPrinter. (It is not necessary to list responsibilities.)












Question 2. Implementing Derived Classes

The Miner's printing supply specialists is designing a database to record the printers they sell. Read the following problem statement and the code below. The provided code defines a superclass PrinterSale. Write the code for the subclass EducationBulkSale. For this subclass, include a class definition header, any instance variables needed, and a new constructor. Also, override any superclass method that must be implemented in a different way within the subclass.

All sales have an number of units, a product code and a date for when it was sold. There are several kinds of sales. One type of sale is an education bulk sale. This requires a large number of printers with the same product code. Prices for regular sales are computed as a product of the number of units, the price of one unit and the tax rate (1.0825 in Texas.) Prices on education bulk sales depend on the number of units sold. If there are 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. Also, there is no tax for education sales.

public class PrinterSale
 {
    private int units;
    private GregorianCalendar saleDate;
    private int productCode;

	public PrinterSale(String inUnits, GregorianCalendar inDate, 
	                   int inProductCode)
       {
	    units = inUnits;
	    shippedDate = inDate;
	    productCode = inProductCode;
       }

	public String getUnits()
	 { 
	    return units;
	 }

	public GregorianCalendar getSaleDate()
	 {
	    return saleDate;
	 }

	public int getProductCode()
	 {
	    return productCode;
	 }

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

	// Compute the total price of a sale
	public float computePrice(int unitPrice, float taxRate)
	 {
	   return unitPrice*units*taxRate;
	 }
}




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 inventory access. When the user enters a product code, the system finds and provides the inventory status for the product. 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 product 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 Inventory  
{

  private static JTextField userInputField;  // Holds product code entered by user
  private static JLabel response;            // Holds inventory status

  // 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 product" ) )   {    

          String productCode = userInputField.getText();
          
          // Here put some code to find the inventory status
          // For now, put a generic message
          
          response.setText("product not found");
          
        }  // end of if whichButton.equals "Find product"           
      
      // 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("Inventory Status");           
       ourFrame.setSize(600,100);
       
       Container ourPane=ourFrame.getContentPane();           

       userInputField = new JTextField("", 8);

       JButton findProductButton=new JButton("Find product"); 
       
       JButton resetButton=new JButton("Reset");       
              
       ButtonHandler ourEventListener=new ButtonHandler();
       
       findProductButton.addActionListener(ourEventListener); 
       resetButton.addActionListener(ourEventListener);              
       
       response=new JLabel("", JLabel.CENTER);

       ourPane.add(userInputField);       
       ourPane.add(findProductButton);
       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("before readLine");
     line = in.readLine();
     System.out.println("after readLine");
     average = total/nline;
     System.out.println("after division");
  }
catch (IOException ioErr)
  {
     System.out.println("in catch");
  }
finally
  {
     System.out.println("in finally");
  }
System.out.println("after try catch statement");

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











(b) Show below 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 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 product code 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.