Homework Assignment 7

  1. Each of the following questions asks you to collect some type of data or provide some type of operation to the user. For each question, select the most appropriate widget to use. You should select each answer from the following list of widgets:

    1. ________________ You want the user to enter a credit card number

    2. ________________ You want the user to select a university status from the choices of Professor, Student, Administrator, and Secretary. A user is allowed to selecct multiple status's.

    3. ________________ You want the user to select the an FM radio station in an internet radio application using its "frequency band" (e.g., 99.1). FM radio stations range from 87.5 to 108 in increments of .1.

    4. ________________ You want the user to select their county of residence in Tennessee

    5. ________________ You want the user to enter the volume of a speaker that should ranger between 0 and 20.

  2. Write a Java Swing application that displays two views of a numeric value that may be adjusted: 1) a slider view that contains a slider and two buttons, an increment button and a decrement button, and 2) a text view that contains a label that represents the value of the numeric value. The program should take four integers as command line arguments:

    1. minValue: The minimum value of the slider
    2. maxValue: The maximum value of the slider
    3. startValue: The starting value of the slider
    4. increment: The amount by which the buttons increment or decrement the slider's value

    The increment and decrement buttons should increment and decrement the value of the slider by the increment value. The program should use the Integer class's parseInt method to convert the command line arguments to integers and it should use a try/catch block to catch a NumberFormatException if any of the four arguments cannot be converted to an integer. Your program should print an appropriate error to the console and then exit.

    You should position the two views using a BorderLayout manager. Place the text view in the north (top) region and the slider view in the center region. The slider view will have to be placed in its own JPanel. I want the elements of the slider view to be laid out horizontally, in the order decrement button, slider, and increment button. There are at least three layout managers that I can think of which will position these three elements correctly, so I will leave it up to you to choose one.

    If the user presses the increment button and the value is already at the maximum allowable value for the slider then the application should pop up a message dialog indicating that the value cannot be further incremented (use the JOptionPane's showMessageDialog method). A similar message should be displayed if the user attempts to decrement the value when it is already at its minimum value. If the user attempts to increment the value and the increment would move the value past its maximum permissable value, then increase the value to the maximum value, but do not print an error message. Do the same thing for the decrement button.

    If you have questions about how your interface should look or behave, you can copy the file /home/bvz/cs365/hw/hw7/SliderApp.jar to your home file system and then run it using the command:

    java -jar SliderApp.jar minValue maxValue startValue incrementValue
    You do not have to get the spacing between elements exactly like mine, but I do want the general positions to be the same, and I want the text view centered.

    Designing Your Implementation

    For this assignment you can put everything into one class. It's probably easiest to make your class subclass a JFrame. You will need to create two JPanel's, one to hold the text view and one to hold the slider view.

    Layout Hints

    1. You will need to use the setHorizontalAlignment method for a JLabel since BorderLayout ignores the setAlignmentX method.
    2. Remember that you can get gaps in a BorderLayout by using the hgap and vgap properties.
    3. If you extract the content pane from a JFrame using getContentPane, make sure you downcast the content pane to a JPanel, because the return type from getContentPane is a Container object.
    4. If you extract the layout manager from the JFrame's content pane, make sure you downcast the layout manager to a BorderLayout, because the return type from getLayout is LayoutManager.

    Submission Instructions

    1. Use a word processor or text editor to answer question 1 and place the answers in a file named hw7.txt or hw7.pdf.
    2. Place your code for question 2 in a jar file named SliderApp.jar that contains your source and class files. It should allow us to execute it using the command:
      java -jar SliderApp.jar minValue maxValue startValue incrementValue