CS 110: Introduction to Programming in Java

Kalamazoo College

Spring 2009

Lab: Getting Started with CodeWarrior


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.


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.


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.


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.

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.


When you complete the lab, you can start work on Programming Project #1 (due at the beginning of Lab 2).