Organize Exercise Solutions In Eclipse

The solutions to all of the ODD numbered exercises in Cay Horstmann's BIG JAVA 2nd Edition text book are freely available. These solutions will be found at Horstmann's textbook site under "Support Materials" at the link labelled Solutions to selected exercises. Given the fact that these solutions are already publically available, I shall use them to demonstrate how I wish my students to organize their exercise solutions when using the Eclipse IDE.

The visual impact of well organzed code will be best appreciated by viewing the tree structure of Exclipse's Package Explorer which, by default, is the left-most column.

In following the instructions and illustrations below, it is most convenient to have use two computers, a situation which is generally available in school labs before class in the morning and during lunch hour. Scroll through the instructions on the monitor of one of the computers and implement those instructions in Eclipse on the other computer. Alternatively, though it does consume paper, you can print the instructions and implement them on your single computer while referring to them. Alternatively, you can split your screen into two windows, one for the instructions and the other for working with Eclipse. Finally, you can simply flip back and forth between the instructions and Eclipse with or with clicks on their tabs in the taskbar at the bottom of the screen.


This is the default display configuration of Eclipse as we are about to begin.


Each chapter will be a seperate project.
Click:        File --> New --> Project


In the New Project Wizard select Java Project.
Click:        Java Project --> Next


For the Project name combine the Chapter Name and Number.
After "Project name:", type:        Introduction 1

Click:        the Radio Button labelled "Create new project in workspace"

Click:        the Radio Button labelled "Use default compiler compliance (Default 5.0)" OR
if compliance is not 5.0, click "Configure default" and change it to 5.0.(
See next illustration.)

Click:        the Radio Button labelled "Create separate source and output folders"
This places source code files (*.java) in a "src" directory
and binary code files (*.class) in a "bin" (binary) directory.

Click:        Finish

Now proceed to the illustration following the illustration below.


For those who clicked "Configure default ..." in the JDK Compliance section of the above dialog box:

In the Compiler Preferences Dialog Box select the most recent JDK.
Click:        OK

Return to above
New Java Project dialog box and complete it before moving on.


The Package Explorer will now contain the project called Introduction 1.


Each exercise (program) will be a seperate package.

In later chapters we will see that a program may be comprised of multiple classes. All classes of a single program will be contained within a single package. However, because we are just beginning, we will solve each exercise in chapter 1 with only one class. Thus, at the beginning, every class will coincidentally have its own package. That will change as exercises become more complicated.

Click:        File --> New --> Package


In the New Java Package dialog box, type an appropriate package name.
Convention dictates that package names begin with a lower case letter.

Remember that a project is an identifier and so cannot contain a space or period.

Click:        Finish


The Package Explorer which contains a project called Introduction 1 will now contain a package within that project called exercise_P1_1.


The Package Explorer contains a project called Introduction 1, which in turn contains a package called exercise_P1_1. Ensure that the name of that package is selected (click on it), so that the screen appears as follows. This is done so that when you create a class (the next step) Eclipse will choose this package by default as the package that will contain the class.


Each exercise is solved with a Java program. Java programs are comprised of chunks of code called classes. A Java program must have at least one class. All Java classes in a program are contained in a package. For now each package will contain only one class.

Click:        File --> New --> Class


In the New Java Class Wizard do the following:

  • Type the name of the class: ExP1_1. Convention dictates that class names begin with a capital letter.

  • Place a check mark [] beside "public static void main(String[] args)" to automatically create a stub for this method. Every Java program must have at least one class with a main( ) method since it is at the beginning of this method that a program starts executing. Since our initial programs will only have one class, then that one class must contain the compulsory main( ) method.

  • Place a check mark [] beside Inherited abstract methods. Initially we won't inherit any abstract methods to worry about, so ticking this option does no harm, but we want to be in the habit of doing it so that it acts as a reminder when the time comes that we do inherit an abstract method. Inherited abstract methods do not contain a body. If we inherit one, then we must write the code for the body.

Click:        Finish


The Package Explorer contains a project called Introduction 1,
which in turn contains a package within that project called exercise_P1_1.
That package now contains a class called ExP1_1.


Double click the editor's tab with the left mouse button to expand it's area.
Do NOT click the [X] button. See below.


The editor now occupies all of the workspace.


Insert an Identification Header between the package and import statements.


Generate the source code to solve the problem.


Click the editor's tab to regain the Java perspective.
Do NOT click the [X] button. See below.


The editor again becomes part of the larger Java "perspective".


Eclipse automatically compiles source code to Java byte code every time you change or add to the source code. That's how it automatically identifies when you've made a syntax error. A syntax error is an error in grammer, like omitting a semi-colon.

But only you can determine if your program has made a logical error. A logical error is an unintended result. Eclipse cannot determine whether you wanted to print 10 cheques or 1000 cheques. Only you can indentify that sort of error.

Having eliminated all syntax errors, you will now "run" your program. When you "run" a Java program, another program called an "interpreter" translates the Java byte code into the "native machine language" of the computer that you're executing the program on. On a WinTel computer, that would be 80X86 machine language.

To run a program in Eclipse, click the right mouse button in the editor's window, then left mouse click after Run As --> Java Application.


The following dialog box asks which "resources" you wish to save.

Click:        OK


Textual Output Appears In The Console Window Just Below The Editor.


Copy the output from the console to the clip board.

Place the cursor in the Console window.

Select All Content In The Window. Click:        <Ctrl-A>

Copy All Selected Content. Click:        <Ctrl-C>


Paste the output from the clip board to the editor below the source code.

Place the cursor in the Editor window.

Copy the Clipboard to the Editor. Click:        <Ctrl-V>

Surround the screen dump with multi-comment tags: /* and /*.

Label the pasted output as "Screen Dump".


Copy all source code, including the ID header and screen dump, to a wordprocessor.
It is easier to arrange hard copy (ie: paper copy) of documentation in a wordprocessor.

Place the cursor in the Editor window.

Select All Content In The Window. Click:        <Ctrl-A>

Copy All Selected Content to the clipboard. Click:        <Ctrl-C>


Paste the output from the clip board to the wordprocessor.

Place the cursor in the WordProcessor window.

Copy the Clipboard to the WordProcessor. Click:        <Ctrl-V>


Repeat for Programming Exercises P1.3, P1.5 and P1.7.

There is only one project for all exercises in the chapter.

You must create a new package for every exercise in the chapter.

Place all classes of a single program (exercise) in the single package for that exercise.

For now, you are to therefore create a new package and corresponding class for each exercise.

See below on placing a graphic screen dump into a wordprocessor, as required for Exercise P1.7

Text editors can't hold graphics because ... graphics are not text!

The screen dump of a graphic cannot be placed in an editor as can the screen dump of textual output.

The screen dump of a graphic must be pasted directly into a wordprocessor.

Copy the graphic to the clipboard.

Place the cursor on the graphic by clicking the left mouse button on it.

Copy the graphic. Simultaneously press the <Alt> and <PrtScn> keys.

Paste the graphic from the clip board to the wordprocessor.

Place the cursor in the WordProcessor window.

Copy the Clipboard to the WordProcessor. Click:        <Ctrl-V>


The Package Explorer Displays Packages, Classes and Members of Classes.

There is one project, a different package for each exercise, and (at least) one class for each package.


[Counter On Strike [Home of Gerry Donaldson's Com Sci Gate]
[Gerry Donaldson's Email Address]
gerry@comscigate.com
[EFC Blue Ribbon - Free Speech Online]

On the Internet Since March 9, 1996    URL:   http://www.comscigate.com    Last Revised:   February 5, 2006.