Laboratory 3
Chapters 10 - 11 & Javadoc

Before the lab, do the Self-Review exercises in each chapter.

Please do as much of the lab as you can in the time allotted. The most important things are turning in:

Feel free to talk to whomever you like about any aspect of the laboratory. Working in pairs may be helpful.

Chapter 10

Do exercises 10.3 - 10.8.

Chapter 11

Do exercises 11.16, 11.17, 11.19, 11.21, 11.22.

Programming exercise

  1. Create a utility package, named util, that has the following 2 classes:
  2. IntegerOptionPane: It has 1 private String attribute, the string used to prompt the user to enter an integer. It has 1 public constructor that has the prompt string as its argument. It has 1 public method, getIntvalue, that takes no arguments, prompts the user via a JOptionPane, to enter an integer, & returns the int value of this entered string. If the user enters a string that is not an integer, the method catches Integer.parseInt's thrown exception, NumberFormatException, and prompts the user for another number, after issuing a message via the JOptionPane.showMessageDialog method, that the number entered was not an integer. it repeats this until a valid integer is entered. Please see the textbook for an illustration of the use of the JOptionPane.showMessageDialog method in section 3.8., Fig. 3.17, on p. 93.

    Use the class's main method to unit test this class.

  3. BoundedIntegerOptionPane, a subclass of IntegerOptionPane. It has 2 private int attributes: lower & upper bounds on what are valid integers. The bounds are inclusive. It has 1 public constructor that has 3 arguments: the prompt string, supplied to the constructor of the parent class; the int lower bound, & the int upper bound. It has 1 public method, getIntvalue, that takes no arguments. It invokes super.getIntvalue, to get an int from the user. It then ensures that the int supplied is within range of the bounds, continually prompting the user for a valid integer, until such an integer is obtained, which it returns as its value. If super.getIntValue returns an int that is not within the specified bounds, a string that is not an integer, the method prompts the user for another number, after issuing a message via the JOptionPane.showMessageDialog method, that the number entered violated 1 of the bounds.

    Use the class's main method to unit test this class.


 cappello@cs.ucsb.edu © Copyright 2014 Peter Cappello                                           2014.04.14