CPTR 124 Fundamentals of Programming
The purpose of this lab is to acquaint you with the environment in which we'll be developing Java programs.
- Log into a lab machine
If you are not using your own laptop, log into a lab workstation using your SAU username and password.
- Accessing information on the web
Point your browser to the the CPTR 124 web page:
- Go to http://computing.southern.edu/halterman,
- click the CPTR 124 link,
- click the Labs link), and
- look at Lab 1.
- Becoming acquainted with the lab
Quickly read over this entire lab before you begin to work on the computer. This will give you a good overview of what we will be doing today. For future labs you should make a habit of reading the entire lab before you begin any work. Ideally you will have read the assignment over completely (and maybe even begun work on it) before you come to lab.
- Teams
You are strongly encouraged to work with a partner for this lab. Working with a teammate offers several advanages:
- Two people do not think alike. By collaborating with another you can gain insights on programming concepts that might otherwise escape you.
- You have a built in support system: You can have an immediate answer to a simple question that your teammate has, or vice-versa. Sometimes students are hesitant to ask a teacher questions they perceive to be "stupid questions," but teammates are less threatening.
- Working with a classmate is more fun.
You may elect to work alone, if you prefer.
- Preparing your working folder
Within Windows, open an Explorer window and go to your H: drive. (This is the virtual drive space provided by Information Systems.) Make a folder named cptr124 on your H: drive. All our programming work for labs in this class will be stored in that folder.
Within this H:\cptr124 folder, create a Lab1 subfolder. This is where you will store all your work for this particular lab. (In future labs you will create H:\cptr124\Lab2, H:\cptr124\Lab3, etc. folders, but you should not do that now.)
- Using the DrJava integrated development environment
Start the DrJava IDE from the Start menu:
Start -> Programs -> Programming -> DrJava - The DrJava IDE
The DrJava interface has five sections:
Main Menu
Toolbar
File Manager Pane
Definitions pane
Interactions/console/compiler pane
The five sections consist of:
- Main menubar--at the very top--provides quick access to all the commands we need when building our programs. For Mac users, the menubar is at the top of the screen instead of the top of the window.
- Button bar--just below the menubar--contains some of the most often used commands.
- File manager pane--the leftmost pane--this contains information about files you are currently using.
- Interactions/console/compiler shared pane--the bottommost pane--this tabbed pane is combines three
distinct functions:
- Interactions--allows us to interact with Java in a convenient way for learning and program development.
- Console--displays the output of our programs.
- Compiler--provides feedback from the Java compiler, reporting errors that may exist in our code.
- Definitions pane--the big area--is where we enter and work with our Java programs. It can also be called the Editor pane.
The Interactions/console/compiler pane can also do find and test, but these options are not shown by default.
- Writing a simple program
Initially, the file named Untitled appears in the file manager pane. When we wish to save our work, we will be prompted to provide a real name for our program file.
Within the editor pane, type in the following Java program:
public class Prog { public static void main(String[] args) { System.out.println("Hey! This is part 1 of Lab 1."); } } - Saving your program
You save what you have typed in with the following sequence of commands from the main menu:
File -> Saveor, instead, you can simply press the CTRL key and the S key simultaneously. (We generally just say CTRL-S to mean press the CTRL and S keys together.) You will be prompted for a file name, and Prog is suggested. You want to use the name Prog, since Java requires that public classes be saved in files with the same name as the class. The actual file that is created is named Prog.java, since all Java source files have the .java file name extension. Before you press OK to save the file, be sure to navigate to your H:\cptr124\Lab1 directory you created. This is where we want to store the file.
You also have the option of specifying which folder (or directory) DrJava should save the file. Store it in your Lab1 folder on your H: drive that you created earlier.
- Compiling and running your program
After you have finished typing in the program and checked it for typographical errors, you are ready to compile it. You can compile in any of three ways:
- From the main menu select Tools -> Compile all.
- Press the Compile button in the button bar.
- Press F5 function key.
Fix any problems that may exist in your program and then recompile. Continue this process until your code is error free.
The compiler translates your Prog.java source file into bytecode that can be executed directly by the Java virtual machine. The compiler stores this bytecode in a file named Prog.class. This .class file does not appear in the file manager pane, but if you open a Windows explorer window and go to the folder where Prog.java is stored, you will see Prog.class as well.
You can execute your compiled program is one of several different ways:
- Choose Tools -> Run main method from the main menu.
- Press the F2 function key.
- In the interactions pane, type java Prog.
- In the interactions pane, type Prog.main(null);.
If you forget to save your program before you try to compile or run it, DrJava will prompt you to save it before it compiles or runs the program.
- Copying an existing program into DrJava
We will work on many programs in class, and all the code we develop in class is available online at http://perl.cs.southern.edu/repository/ClassExamples/124/ It is very useful to get the code we do in class, copy it into your filespace, and play with it (modify it, break it, etc.) until you better understand how it works. This beats copying it down on paper in class (with the unavoidable typos) and then typing it into DrJava later. In this part of the lab you will copy a file into your Lab1 folder and run it as is.
Direct your browser to http://perl.cs.southern.edu/repository/ClassExamples/124/. Navigate to the file you wish to copy. With most browsers you can right click the file to save it to your computer. You can save the Java file of your choice directly to your H:\cptr124\Lab1 folder, or, you can do what I usually do; that is, I save the file to the desktop, and then I drag the file into the folder I want it go (in this case the H:\cptr124\Lab1 folder).
Next, within DrJava's main menu, select File -> Open. Select the file you copied into the folder. You now have two active files within the file manager pane. You can move between them (that is, change the contents of the editor pane) by selecting the file within the file manager pane. You can also press CTRL-. (CTRL period) to switch among active files. Notice that simply copying a file into DrJava's working directory does not cause DrJava to recognize it; it must be opened within DrJava for the environment to be able to work with it.
You should compile and run this copied program to verify that it was properly copied into your workspace.
It is important that you are able to copy files and work with them, not only so you can experiment with class code, but also because future labs will require you to incorporarate into your working set Java source files that I provide.
- Creating a simple Java program
Write a Java program that displays the initials of your name on the screen. These initials will be large letters that you must construct. Each letter is created by composing many smaller “normal” characters into the shape of the desired letter:
- Outside of DrJava, within your H:\cptr124\Lab1
folder, copy your Prog.java file to a
new file named Initials.java.
- Return to DrJava and open your Initials.java
file.
- In the first line, change the name of the class from
Prog to Initials.
- Duplicate the line containing
System.out.println(...). You can duplicate it by the
normal Windows means of copying and pasting. (Highlight the
line with the mouse, select Edit ->
Copy, move the cursor to the next line, and select
Edit -> Paste.) Make about 10 copies
of that line.
- With the parentheses of the
System.out.println(...) , change the contents of the
quotation to form slices of the initials of your name. For
example, for my initials above, the statement would look
like:
System.out.println("RRRRRRRRRRR LLLL HHHH HHHH");This printing statement makes the top row of letters (the first "slice") in my initials.
- Compile and run the program to make sure it works. If necessary, modify your program until you are satisfied that it works correctly.
- Learn more
Now that you have some experience with DrJava, take some time to peruse the parts of its documentation affect us the most. The documentation is found at
http://drjava.sourceforge.net/userdocs/ and you should read the following sections:
Editing Programs Definitions Pane Multiple Documents Source Navigation Interactions Pane Compiling ProgramsRead more, of course, if you are curious. If you are installing DrJava on your own computer, you may want to read the sections pertaining to installing and configuring DrJava.
- Check out
I will review your lab with you before you leave. You should then print out a paper copy of your initials program. Be sure your print out has your name on it. If you worked with a partner, your parnter should print out his/her own copy to turn in.
- Finish up
You should always close DrJava when you are finished working with it. Select File -> Exit.
- Log out
Don't forget to log out on your lab workstation before you leave.
RRRRRRRRRRR LLLL HHHH HHHH RR RR LL HH HH RR RR LL HH HH RR RR LL HH HH RR RR LL HH HH RRRRRRRRRR LL HHHHHHHHHHH RR RR LL HH HH RR RR LL HH HH RR RR LL LL HH HH RRRR RRR LLLLLLLLLLLLL HHHH HHHH |
If you are working with a partner, each person should write a program to do his/her own initials.
You will not devise
a sophisticated algorithm to complete this part; instead, simply use a series
of System.out.println() statements that each print a horizontal slice of all three letters.
In order to do this part of the assignment:
Hint: Watch out for mixing tabs and spaces within the editor. Under DrJava this is not really an issue, but on other systems it can be a problem. Tabs are treated differently within the editor than in the console pane. It is best to use only spaces when laying out your initials within the editor.