You are on page 1of 7

Anthony W.

Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

BlueJ homework

Purpose
Purpose is to become comfortable using BlueJ to enter and run Java programs. You will
also learn how to unzip and run the example programs, how to install BlueJ onto your
own computer, and how to print programs for grading.

1. How to enter and run a program


Begin to learn how to use BlueJ as you type-in and run the Rectangle example program:

start BlueJ

double-click the icon on your desktop

BlueJ has a large main window which shows the class diagram of the program
you are working on

if necessary, use Project | Close to close an old project that may have opened
when BlueJ started

create a new folder in which to save your new program

choose Project | New Project

set Look in: to the place where your folder will be saved. For example, I used
Desktop

set File name: to something appropriate for the name of the new folder. I used
Rectangle

create your new Rectangle class

click the New Class button

set Class Name: to Rectangle and click Ok

this shows the Rectangle class in the main window

(the stripes mean that the class has not been compiled)

(you can drag the bottom right hand corner of the icon to re-size it)

1
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

type-in the Java code for the Rectangle class

double-click the Rectangle icon to open the editor window

BlueJ automatically gives you the generic template of a new Java class, to help
get you started. You edit and delete from this template so that it becomes the Java
code you want

heres the (corrected) Rectangle code again, type it in carefully! Be advised that
it continues onto the next page. Delete any parts of the generic template that you
do not use

(all the Java programs your write must look professionally formatted. Layout
your program exactly as shown below)

/**
* This program represents a rectangle.
*
* @author: you must put your own name here!
* @version: put today's date here!
*/

public class Rectangle


{
// instance variables
private double length;
private double width;

// constructor method
public Rectangle(double len, double w)
{
length = len;
width = w;
}

// method that calculates area


public void calculateArea()
{
System.out.println("Area is: " + (length * width));
}
}

compile the Java program

click the Compile button

if you typed everything correctly, youll see Class compiled no syntax errors
in the status window at the bottom

2
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

if you made a typing mistake you may have a syntax error, where the rules of
the Java language have been broken. Syntax errors are normal as people learn to
program

the compiler finds syntax errors and tells you what and approximately where they
are

click the ? button in the status window for more help with a syntax error

correct the error and compile again

repeat until all syntax errors are fixed and your program compiles cleanly

run the program by creating Rectangle objects from the Rectangle class

in the main window, right-click the Rectangle class icon to show a pop-up menu

choose new Rectangle(double len, double w) to run the constructor and create a
new Rectangle object

BlueJ provides a default name for the Rectangle: rectangl1 here

enter some values for your rectangles length and width and click Ok

the new object is shown on the object bench at the bottom of the window

you can create many different objects from a class

create another rectangle object with a different name and size

run your objects methods

right-click an object to pop-up a menu. Choose void calculateArea() to run


this method

output from the program causes the Terminal Window to pop-up

check that the result of calculateArea() is correct (remember the importance


of testing your program?)

inspect the inside of an object

3
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

BlueJ allows you to look inside an object, which helps you see more clearly
what is going on!...

right-click an object and choose Inspect

a dialog box shows the values of the objects instance variables

click Close

you can destroy an object if you are done with it

right-click the object and choose Remove

close your project

click Project | Close, this clears your project from the class window

you can open an existing project

click Project | Open Project or Open Recent and navigate your way to your
project

close your project, then exit BlueJ when you are done

Project | Quit

How to save the output of your program


As you saw, output from a program appears in the Terminal Window. You must save the
output for grading, as follows:

Restart your Rectangle project in BlueJ, create a new Rectangle object, and run the
calculateArea() method to pop-up some output in the Terminal Window.

First, in Terminal Windows Options, set Clear screen at method call so that the
window is cleared every time you run a program. Also set Unlimited buffering, so that
the window can scroll to handle long output.

Then use Terminal Windows Options | Save to file to save the output of the program
into a text file. The file can be named output.txt, and saved inside the Rectangle folder
with the rest of the BlueJ project. (So at File name: you have to type output.txt not
just output the .txt file type is very important)

4
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

Bring a USB flash drive


Bring a USB flash drive to class so that you can easily take home any BlueJ work you do.
Other alternatives are to email code from the lab to yourself, or send it to the cloud.
Student files are deleted every time a lab computer is restarted.

2. How to unzip and run the example programs


My lectures use example programs to illustrate and explain the major points. It is
essential every week that you run and understand the example programs.

The example programs are compressed into a .zip file, which must be unzipped before
BlueJ can open the programs.

You must now download, unzip then run the example program named Hello. (Do not
worry yet about Java syntax, that will be explained beginning next week):

in Blackboard, Course Documents, Week 1 folder, find the Example programs item.
Click the down-arrow next to the Examples.zip link and choose Open

this downloads the Examples.zip file to your computer

exactly how to unzip the file varies from computer to computer

for example, on my Windows machine the download starts Windows Explorer


with the Extract all files command available at top left

I use this to unzip or extract the example programs to a sensible location, for
example the Desktop

you have to find how to unzip the example programs on your own computer

start BlueJ when you have successfully unzipped the Hello example program

use Project | Close if a project is already open

use Project | Open Project to open the Hello project from your uncompressed
folder

create a new Hello object

run the objects sayHello() method, which pops-up the BlueJ Terminal Window
where input/output is displayed

save the output of the Hello program, as above

5
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

you will print the Hello program source code and output, explained shortly

3. Install BlueJ onto your own computer


Install BlueJ onto your own computer, so that you can do programming outside of class
and print your work for grading.

BlueJ is available as a free download from www.bluej.org. Run the installer appropriate
to your computer.

(If you are unable to install BlueJ, you may not have enough computer experience to be
able to complete this course successfully.)

4. Java programs and output must be printed for grading


You will be printing the programs you write, to hand in for grading. Heres how to do it,
using the Rectangle example program.

first, look inside the Rectangle folder to review some of the many files in a BlueJ
project:

package.bluej BlueJ package file. There is always a single .bluej file,


generated automatically by BlueJ. This contains information about the entire
program, such as a list of all classes in the project, layout of classes on screen, and
so on.
(Note that in older versions of BlueJ, the package file would be named
bluej.pkg, and that a backup called bluej.pkh would be created automatically.
You may occasionally see these older .pkg and .pkh files around)

Rectangle.java Java source file, for the Rectangle class. Written by the
programmer. Unlike this example, most programs contain many classes. Each
class is saved in its own, separate .java file

output.txt the program output, which you saved in a .txt text file

Rectangle.class Java bytecode file for the Rectangle class. Generated by the
compiler. Each .java file would be compiled into a separate .class file

Rectangle.ctxt BlueJ context file. Generated by the compiler. Stores


information about each class, such as parameter names and method comments,
which BlueJ needs to construct its menus and dialogues

6
Anthony W. Smith, 2017
CSCI 114 Programming Fundamentals II
BlueJ homework

README.TXT project documentation file. Older versions of BlueJ would


automatically create this documentation outline for the program. The
programmer would then update the file, to pass important information to future
maintenance programmers. The current version of BlueJ no longer generates it
automatically, although you may still see it around in my example programs

of these files, you need to print the Java source file that you wrote contained in
Rectangle.java. Open your Rectangle program in BlueJ, then:

double-click the Rectangle icon in the BlueJ class window to display your code
in the editor window

make sure that your full name appears clearly in your source code!

make sure that your Java code looks professionally formatted, exactly like all the
example programs I write. Fix your layout if necessary

choose Class | Print

you must also print the output of your program, which you saved as the output.txt
text file

always check your output, make sure its correct before you print it!

(note that you cannot print in MD-234. You must print at home instead)

Submitting your work


deadline for this homework is 1 week, at start of lab on Thursday 2/9/2017

print and hand-in source code and output for both the Rectangle and Hello programs

make sure your name is on there clearly

this is a homework assignment, without a numeric score I just want to see that you
can do the work

You might also like