Espresso: A Concentrated Introduction to Java

An Introduction to Java

Summary: In this laboratory, you will run and modify your first Java programs.



Exercise 0: Preparation

a. Review An Introduction to Java, Packages in Java, and Java Programming in Eclipse.

b. Create any of the following directories that do not yet exist.

c. Make a copy of in that final directory. The easiest way to make a copy is to select the link with the right mouse button (click and hold on a Mac) and select Save Link As ..., then select the directory.

d. Using gedit, add /opt/jdk1.5.0/bin to the line in /home/username/.bash_profile which reads PATH=somestuff. Afterwards, the line should read something like


e. Using gedit, add the following line to your /home/username/.bashrc file.

alias eclipse="/home/rebelsky/bin/eclipse/ &"

f. Open a new terminal window so that those changes take effect. Type

source /home/username/.bash_profile
source /home/username/.bashrc

In the future, you should not have to type that stupid command, but it's a good idea to do so the first time you change your bash profile.

Exercise 1: Starting Eclipse

a. Type eclipse to start Eclipse. Wait. Wait some more. If you see a Welcome screen, click the close box (the x) in the Welcome tab.

b. From the File menu, select New and then Project.

c. In the dialog box that appears, select Java Project and then click Next.

d. In the dialog box that appears, type First as the project name, select Create Project from Existing Source, and type /home/username/CSC152/First as the directory. Under JDK Compliance set the default compliance to 5.0.

e. Click Finish. Wait. You are now ready to program in Java.

Exercise 2: Your First Program

a. Click the arrow next to First in the package explorer so that it points down.

b. Double click on If all goes well, that should open the program in the edit window.

c. Modify so that it uses the appropriate package name.

d. From the Run menu, select Run As and then Java Application.

e. Confirm that the output (at the bottom of the screen) is Hello.

f. Marvel at all the work it took to get one word printed.

g. Change the Hello in to a word or phrase of your choice.

h. Save the modified file, using Save from the File menu.

i. Try running it to see if it works correctly.

Exercise 3: Your Second Program

a. Make a copy of called by selecting Save As... from the file menu. Do not make any changes.

b. Click on the Problems tab at the bottom of the screen. What do you see?

c. Modify so that you can run it. Remember that the name of a class must match the name of the file.

d. Modify so that it prints two lines.


Exercise 3: print vs. println

a. Presumably you have two calls to println in Replace the first println with print.

b. What effect do you expect this change to have?

c. Verify your prediction experimentally.

Exercise 4: print, revisited

a. Change the second println in to print.

b. What effect do you expect this change to have?

c. Verify your prediction experimentally.

d. You are likely to get a different result than you expect. Try adding the command pen.flush() after the two calls to pen.print(...) and see what difference this makes.

e. Replace the call to pen.flush() with pen.close(). Rerun the program to see what difference that change makes.

e. Read the documentation for flush. Read the documentation for close.

Praise the authors for their clarity.

f. If possible, explaine the difference between flush and close.

g. If you don't understand what's happened in this exercise, please discuss it with your teacher or lab assistant.

Exercise 5: Shorthand

As you may have noticed, it can get irritating to type the full name of a class each time you use the class. Hence, the designers of Java included a shorthand mechanism that lets you type only the basic class name. To use this shorthand, you must include a line at the top of your program (after the package declaration) of the form

import packagename.ClassName;

You may then use just ClassName in the rest of the program.

a. Replace all instances of in with PrintWriter. Do not add the import statement above. What do you expect to happen when you try to compile and run Second?

b. Verify your answer experimentally.

c. Add the import statement and try again.


Sunday, 30 January 2005 [Samuel A. Rebelsky]

Monday, 31 January 2005 [Samuel A. Rebelsky]

Wednesday, 31 August 2005 [Samuel A. Rebelsky]

Friday, 2 September 2005 [Samuel A. Rebelsky]

Friday, 27 January 2006 [Samuel A. Rebelsky]

Sunday, 29 January 2006 [ Samuel A. Rebelsky]

Thursday, 30 March 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:27:19 2006.
The source to the page was last modified on Thu Mar 30 15:27:18 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky