CS 110: Introduction to Programming in Java |
||
![]() |
Kalamazoo College | ![]() |
Spring 2008 |
||
Exercise 1: Creating a Folder for your CS 110 work
It makes sense to keep your CS 110 programs together in a single place, separate from other documents or materials you may have from other classes.
k03xx01). Note:
if there isn't an icon for your server directory under
"My Computer,"
you will need to follow the following instructions to get to
Dragon the long way. If you need to use this
convoluted way to get to
Dragon now, chances are you will need to use it
repeatedly throughout this course, which is why it is
getting so much prominence here.
Getting to Dragon the long way... (only if necessary)
|
CS 110 (or something
similar) in your folder.
Throughout this course, you will be creating new sub-folders in this
space.
Exercise 2: Creating a Program Using Eclipse
An object-oriented program is, as we learned in class, made up of a collection
of classes. Each class is stored in a separate file, where the name of
the file is the name of the class with a .java extension.
Thus, a class called Aquarium must be in the file Aquarium.java.
We might have many classes in a program, and we might have many programs.
How do we keep track of which classes are part of which programs?
The Eclipse development environment uses the idea of a project to keep track of information about a program, such as which classes are part of the program, where the program's starting point is, and so on. To run or modify a program using Eclipse, the first thing to do is to start up Eclipse and then create a new project or open an existing project to contain and keep track of our classes.
We also learned in class that a running program consists of objects invoking
operations on other objects. How, though, can we start an application
when it doesn't have
any
objects
yet to do any operations? The answer is that at least one of the classes
in an application must have a special operation called main. (The
definition of an operation in a class is called a method definition,
so this operation is called a main method.) A main method
is not tied to any particular object, so the development environment (or whatever
software is running the program) can invoke it, even though it doesn't have
any objects yet. The main method then constructs one or
more objects and invokes operations on them. They, in turn, may construct
objects and invoke other methods, which is the essence of an object-oriented
program. To start a Java application, Eclipse looks through the project
for a class that has a main method and invokes it.
Let's start by creating a very simple program with a single class. To keep
things simple, the only method we will create is a main method.
CS 110 folder under
"My Computer" (or following the long way to Dragon
outlined in the box above). When you get to your folder, click on
"New folder" and give it a name, such as EclipseWorkspace.
cls_compsci folder on Dragon. (You may find
yourself there already. Otherwise, see if there is a
"KNet"
folder; if so, click on it and then click on Dragon. If you
cannot find Dragon more directly, you will have to navigate
there the long way, through "My Network Places" following
the directions outlined in the box at the beginning of this
document.)
Once you have reached the
cls_compsci folder on Dragon, select
the "CodeFormatConventions.xml" file and click on the "Open" button. If
a dialog box appears, click on the "OK" button.cls_compsci
folder on Dragon as before. Select the
"EclipseCodeTemplates.xml" file and click on "Open". If
a dialog box appears, click the "OK" button. System.out.println("Hi");
This line won't mean anything to you either, just type it. Notice that
the Eclipse editor is trying to be smart. As soon as you type in the
left parenthesis and double-quote mark, it will add the right parenthesis and
second quote mark. If you type the second quote mark and then the right
parenthesis yourself, it will just move over the characters it added for you. Alternatively,
you can use the mouse or arrow keys to move past the characters it added. Don't
forget the semi-colon (not a period or colon) at the end of the line. Now
you can run your program again as you did above, or you can click on the icon
of a green circle with a white triangle (beneath and between the
:Refactor" and the "Navigate" menus). If a "Run" pop-up
window appears asking you to create, manage and run configurations, just
click on "Run" at the bottom. If you didn't
save your modified class, you will be prompted to do so. This time you
should see the word "Hi" appear
in the Console pane beneath your class.
Exercise 3: Dragging Files from Dragon
Many of the labs and programming assignments in this course will involve modifying existing programs provided for you on the college's server called "Dragon." For example, in the first few weeks of the course you will be developing an Aquarium program. You will be writing all of the logic that actually runs the program, but we will provide graphics code that your program will use. Thus, you will start by copying an existing program with the graphics code already in place, and then modify it. The versions of all the CS 110 programs on Dragon will also include the necessary Eclipse project information for you to run the programs under Eclipse.
To run and modify the programs we provide on Dragon, you will first have to copy them to somewhere where you have the correct permissions, such as your directory on the campus server or your computer in your dorm room. The directions below assume that you are working in a lab and want to copy.
cls_compsci. (This is where computer science
classes have their Knet archives.) cs110.k03xx01). [Note:
if there isn't an icon for your server directory under "My Computer,"
follow the instructions above to get to Dragon
the long way, and then double-click on
the "students" folder and then on your own directory.]cs110 folder in one window
to your own folder for this class in the other window. For this lab, drag
the JavaMBSUsingEclipse folder.
Exercise 4: Opening and Running an Existing Program Using Eclipse
For this part of the lab, you will copy and run the Marine Biology Simulation program.
JavaMBSUsingEclipse folder from
the cs110 folder under cls_compsci on Dragon to your own folder on the campus
server.
Exercise 5: Experimenting with the Marine Biology Simulation Program
Exercise 5: The Edit-Compile-Run Cycle
Now let's return to a project you were working on before, to add new functionality to it. You will do this frequently in this course, since enhancing an existing program (editing, compiling, and testing existing files) is much more common than creating a completely new one. In fact, since it is almost impossible to sit down and write a program absolutely correctly from scratch, even new programs are often created by modifying previous programs.
Exercise 6: Cleaning Up
Be sure to clean up the Eclipse workspace before you go, so that the next person to use the same machine does not see your files. One at a time, choose each project and then select "Delete" under the "Edit" menu. In the confirmation dialog box that appears, be sure that you check "Do not delete contents"!!!
Make sure that you saved your program to your directory on the campus server or a disk on your own network. In addition, you may wish to save a backup copy in one of those two places, or on a USB drive.
When you complete the lab, you can start work on Programming Project #1 (due at the beginning of Lab 2).