CSC 211

Spring, 2003

Assignment 1

Using BlueJ

Due: 11:59pm, Wednesday, April 2nd

Prerequisite: Before doing this assignment, you will need to install BlueJ and Java.  Instructions are on the previous page.

Purpose: This assignment will allow you to try out the BlueJ development environment and learn a little about the structure of Java programs.

  1. Create a directory for your Java projects.  In this document, we will call this directory JavaProjects.
  2. Start up BlueJ.  From the Project menu, select New Project.  A dialog box will appear.
  3. Using the drop-down list next to the Look In prompt, move to the JavaProjects directory.
  4. In the text box next to the prompt File Name, type in Assignment 1 and click Create.
  5. BlueJ will now return to its main window, where you should see a text document icon.
  6. Click on New Class.  For the class name, enter ComputeAverage and click Ok.
  7. A second icon, which represents the ComputeAverage program, will appear in the main window.
  8. Double-click on this icon.  An editor window will appear with code already in it.  Select this code and delete it.
  9. Type the following code into the window:
    import javax.swing.*;
    /*
     * @author John Rogers
     * @course CSC 211
     * @project Assignment 1
     *
     * This program requests two integers and then computes and prints
     * their average.
     * 
     */
    public class ComputeAverage {
        public static void main(String[] args) {
            JOptionPane input = new JOptionPane();
    
            String s1 = input.showInputDialog(null, "Please enter the first number");
            String s2 = input.showInputDialog(null, "Please enter the second number");
    
            int number1 = Integer.parseInt(s1);
            int number2 = Integer.parseInt(s2);
            
            double average = (number1 + number2) / 2.0;
            
            System.out.println("The average of " + number1 + " and " + number2 + " is " + average);
            System.exit(0);
        }
    }
    
  10. Compile this program by clicking on the compile button. 
  11. Click back in the main BlueJ window. 
  12. Right-click on the program's icon and select the menu entry void main(args)
  13. Click OK in the dialog box that comes up. 
  14. Enter integer values into each of the dialog boxes that come up.

What to submit: Submit the file ComputeAverage.java through the COL assignment submission page.