CSC 121A Lab 1 - Spring 2005

Thursday, January 6, 2005
Due: Wednesday, January 12, 2005, 5:00PM

[Course home page]

Preliminaries

You may work in pairs for this lab. For most of this semester, lab work, along with weekly homework assignments, will be due on the Wednesday of the next week. Sometimes you will only need to finish up the lab exercises; other times, I will assign additional homework problems. While you may work on most lab assignments in pairs, you are to work on the homework problems individually. Each one of you should then upload your own copy of the lab work along with the homework problems to the VikingWeb.

Create a folder (directory) named "csc121" in your private space on the networked hard drive (M:\ or S:\) of the machines in the computer lab. Within this folder, create a folder named "lab01", "hw01", or something similar. Download or copy all files related to this lab into that folder.

In this lab, you should answer the questions that are bulleted in the exercises below. Type all your answers in a single text file and upload it to the appropriate section of the VikingWeb coursework page for this course when you are done. For the lab exercises that require writing a program, upload your source code file (with name ending in ".java") to the VikingWeb.

Using Dialog Boxes

In CSC 120, the programs you wrote used the command window (or, terminal) for input and output. However, many Java applications use windows or dialog boxes (also called dialogs) to display output or collect input. Typically, dialog boxes are windows in which programs display important messages to the user of the program.

Java includes a large package of classes for programming graphical user interfaces (GUIs), including components like windows and dialog boxes. The class JOptionPane provides standard dialog boxes to allow you to display windows containing messages to users-- these windows are called message dialogs.

Download the following Java program, compile and run it: Dialog1.java. Notice that the program imports the JOptionPane class from the javax.swing package. The Swing library is a collection of classes for GUI programming in Java.

The dialog box is generated by a call to the (static) method showMessageDialog of the JOptionPane class. The method takes two arguments. The first helps the Java program determine where to position the dialog box on the screen. When the first argument is null, the dialog appears in the middle of the screen. The second argument is the string of text to display in the dialog.

The showMessageDialog method is an overloaded method. That means that there are several "versions" of the method defined in the JOptionPane class. Each version of the method takes a different set of arguments. Try replacing the call to showMessageDialog in the program above with:

        JOptionPane.showMessageDialog( null, "Welcome\nto\nJava",
                                        "Startup Message", 
                                        JOptionPane.WARNING_MESSAGE );   

Find the documentation for the showMessageDialog method on the official Java website: http://java.sun.com.

Getting Input Using Dialog Boxes

Download and try running the following application: NameDialog.java.

Program: Multiplication Practice

The following programs are based on exercises 6.30-6.32 in Deitel & Deitel

1. Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Use a Random object (see the documentation for the java.util package) to produce two positive one-digit integers. The program should then prompt the user with a question, such as

How much is 6 times 7?

The student then inputs the answer. Next, the program checks the student's answer. If it is correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right. A separate method should be used to generate new questions. This method should be called once when the application begins execution and each time the user answers the question correctly.

If the user enters a negative number, the program should exit.

You may use either text-based input and output techniques (as in last semester's course), or GUI techniques based on the lab exercises above with dialog boxes.

At the top of your Java program, you should include a header comment that looks like the following, replacing my name with your name:

    //
    // Multiplication.java
    // Program to help elementary school students learn multiplication
    //
    // Nadeem Abdul Hamid
    // CSC 121 - Spring 2005
    // Time taken: 30 minutes
    //

For every program that you write in this course, you must have a header comment at the top of every file following the format above, namely the first line gives the name of the file, followed by one or more lines describing the program's or class' purpose, followed by your name and the course information. You may optionally include a line indicating how long it took you to complete the program. (I will try to use this information to adjust the amount of work in future assignments.) You will lose points on assignments if your files do not contain this standard formatted header at the top.

 

Additional Homework Problems

To be worked on individually. Due, Wednesday, January 12, 2005, 5:00PM with the lab work above.

2. The use of computers in education is referred to as computer-assisted instruction (CAI). One problem that develops in CAI environments is student fatigue. This problem can be eliminated by varying the computer's responses to hold the student's attention. Modify the program above so that the various comments are displayed for each correct answer and each incorrect answer as follows:

Responses to a correct answer:

    Very good!
    Excellent!
    Nice work!
    Keep up the good work!
    

Responses to an incorrect answer:

    No. Please try again.
    Wrong. Try once more.
    Don't give up!
    No. Keep trying.
    

Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer. You may use a switch statement to issue the responses, or you may use the randomly-generated number to access an array of response strings.

 

3. More sophisticated CAI systems monitor the student's performance over a period of time. The decision to begin a new topic is often based on the student's success with previous topics. Modify the program of Exercise 2 above to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75%, display Please ask your instructor for extra help and reset the program so another student can use it.

 



Last modified: Wed Dec 29 10:20:08 EST 2004