CS 110: Introduction to Programming in Java |
||
![]() |
Kalamazoo College | ![]() |
Winter 2008 |
||
Exercise 1: 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 Dragon server. 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. The versions of the programs on Dragon will also include the necessary CodeWarrior project information for you to run the programs under CodeWarrior.
To run and modify the programs we provide on Dragon, you will first have to drag them to somewhere where you have the correct permissions, such as your ZIP disk, your computer in your dorm room, or the Temp folder on a machine in a lab. The directions below assume that you are working in a lab.
AquariumLabSeries, JavaMBS,
and JavaTemplates.
Exercise 2: Running a Program Using CodeWarrior; Constructing Objects in a Java Program
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 CodeWarrior development environment uses a "Project" file 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 CodeWarrior, the first thing to do is to bring up
its project. You can find CodeWarrior project files by looking for files
with a .mcp extension (for Metrowerks CodeWarrior
Project).
For this part of the lab, you will run the Marine Biology Simulation program and experiment with constructing objects. The Marine Biology Simulation program simulates fish moving in a bounded environment, such as a pond.
JavaMBS folder you created on your ZIP disk,
Temp folder, or computer in your dorm room. (Make sure you do not
navigate to the JavaMBS folder on Dragon, because the program
will not run correctly there.)
Code folder to move there. You will see
a number of Java class files, such as BoundedEnv.java, as well
as other files.
OrigFishLE2.mcp file to bring up the initial
version of the program. This will bring up the Project Window.
Alternatively, you could bring up CodeWarrior from the Start menu (Start->Programs->CodeWarrior
for Windows->CodeWarrior IDE) and then do a File->Open to open the OrigFishLE2.mcp
project file. (Note: There is another project file in the Code
folder that we will use later in the course.)
Sources folder and a
Classes folder. The Sources folder contains
the files for the classes associated with this program. If you click
on the little plus sign next to the Sources folder you will see
a folder labeledCode, corresponding to the Code
folder where you found this project. Click on the little plus sign next
to the Code folder and you will see a list of the same Java source
files, such as BoundedEnv.java, that you saw before. You could
double-click on any of those source files to view or edit them, but their
contents wouldn't mean anything to us yet. We will look at these classes
later in the course, but for now, let's just run the program and see what
it does, rather than how it does it.
Exercise 3: Telling CodeWarrior Where to Start
The Sources folder in the CodeWarrior Project Window usually contains
multiple classes. Furthermore, each class usually contains information
about multiple operations that objects of that class can perform. Thus,
there are many operations defined in the program. How does CodeWarrior
know which operation should be done first? And how does it invoke any
operation, when it doesn't have any objects yet to do the operations?
Let's answer the second question first. 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. You start a Java application by invoking a main
method, which is known as the target for that program.
What if several different classes have main methods? Each
main method, or target, will start up a different program, whose
behavior is defined by the objects that main method constructs
and the methods it invokes.
Now let's return to the initial question. How does CodeWarrior know which
class has the main method that should be the target for this particular
program? This is just the kind of information a CodeWarrior project keeps
track of.
.java extension.)
What is the Main Class for the "Full GUI" target?
OrigFishLE2 Project Window. Open the AquaLabSeries.mcp
project file. (Go to the AquariumLabSeries folder, then
to the StartingCode folder, where you will find the project file.)
Exercise 4: The Edit-Compile-Run Cycle
So far you have experimented with running existing programs without modifying them. Programming, however, consists of creating new programs and modifying existing ones. In fact, since it is almost impossible to sit down and write a program absolutely correctly from scratch, the most common behavior in programming is to edit, compile, and test existing files, whether to fix them, enhance them, or use them as templates for new development.
TemplateApplication.mcp project file in the JavaTemplates
folder.
MainClassTemplate.java file by double-clicking
on it in the Sources folder.
MainClassTemplate.java
file. Between the "welcome" and "goodbye" statements,
delete the commented-out lines of code that construct and use a ClassTemplate
object. Replace these statements with a line to print out "Hello, your
name". (If you closed the MainClassTemplate.java window
before running the program, reopen it by double-clicking on the file from
within the Project Window.) Run it again. Did you get what you expected? If
not, fix it!
ClassTemplate
class, you can remove it from your project. Single-click on ClassTemplate.java
in the Sources folder in the Project Window to select it, then
right-click on it to bring up a menu. Choose Delete or Clear to delete
the file from the project. CodeWarrior will bring up a dialog box asking
you to confirm this decision.
MainClassTemplate and ClassTemplate are fine names
for template classes, but they are not good names for classes in an actual
program. Change the name of the MainClassTemplate class
to a more descriptive name for this class. The name should start with
a capital letter, since this is the convention for class names in Java.
To change the class's name in a consistent way, you will need to:
MainClassTemplate
to the new name (for example, public class MainClassTemplate).
Don't forget the comments at the top of the file. CodeWarrior
has a Find/Replace feature (under the Search menu) similar to that in
word processing and other applications, which you may use if you want..java extension.
The name must match exactly, including capitalization. Notice that
this also changes the name of the file in the Project Window. (This
is the difference between "Save As" and "Save a Copy As"
— saving a copy will create a copy of the file with a new name,
but it will not affect the Project Window in any way.)You may use this procedure to create a program, using the templates in the
JavaTemplates folder as a starting point, anytime you need to create
a new project.
Exercise 5: Saving or Backing Up Your Program
Make sure you save your program to a floppy disk, a disk on your own computer
(if it's connected to the campus network), or a campus server such as cc.kzoo.edu.
Remember to save the entire folder containing your Java source files and your
project file. If you are using a floppy disk to save your work, you should
remove the object code from the project (this is an option from the Project
menu) before copying to your floppy disk; otherwise the folder probably won't
fit.
If you used the Temp folder, be sure to clean it out before you
leave by dragging everything in it to the Recycle bin. (Be sure you have save
a copy first, though!) You have a responsibility under the Kalamazoo College
Honor Code not to leave copies of your labs and programming projects on machines
where other students would have access to them.
Optional Follow-up Exercises: Learning More About CodeWarrior Projects
In this course we will generally provide you with the project files you need for labs and programming projects. You may, however, wish to learn how to create new programs and new projects, rather than always modifying existing ones. You may also want to learn how to change project configuration information other than the target's main class. CodeWarrior provides user manuals to help you learn more about its development environment.
Creating A New Project and Adding Files to It.
Source group - it has a TrivialApplication.java
file. You should notice that this file is short on comments. You are expected
to write more! (Such as author, modification dates, assistance from, descriptions
of project and methods, etc.) It may be more useful to you to use the MainClassTemplate.java
and ClassTemplate files from Exercise 4 as templates.
Source group if necessary.
(To move it up, click on it and drag it up.)
When you complete the lab, you can start work on Programming Project #1 (due at the beginning of Lab 2).