|
|
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.
- Create a directory for your Java projects. In this document, we will call this
directory JavaProjects.
- Start up BlueJ. From the Project menu, select New Project.
A dialog box will appear.
- Using the drop-down list next to the Look In prompt, move to the JavaProjects directory.
- In the text box next to the prompt File Name, type in Assignment 1 and click Create.
- BlueJ will now return to its main window, where you should see a text document icon.
- Click on New Class. For the class name, enter ComputeAverage and click Ok.
- A second icon, which represents the ComputeAverage
program, will appear in the main window.
- Double-click on this icon. An editor window will appear with code already in
it. Select this code and delete it.
- 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);
}
}
- Compile this program by clicking on the compile button.
- Click back in the main BlueJ window.
- Right-click on the program's icon and select the menu entry void main(args).
- Click OK in the dialog box that comes up.
- 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.