Class 03: Classes and Objects
Held Wednesday, January 27
- Reminder: you should read the discussion section of each lab
before coming to class to do the lab.
- Some of you have asked whether you can play with Java in the comfort
of your dorm rooms.
- It is possible to remote login to the MathLAN. The preferable way
is to use an X-windows emulator on your machine. At some point,
some clever person will document this.
- If you have a PC running Windoze, it is possible to download a free
Java compiler. Look at
- If you have a PC running Linux, there should also be something out
there for you, but I haven't looked for it yet.
- If you have a Macintosh, you may be out of luck, since Apple doesn't
seem to support textual I/O. You can find some info at
- If you want to keep track of the corrections (and how much credit
you're getting for making them), there is
a handout detailing
them. So far, no majors ones!
- As we've seen, object-oriented programming involves not just objects,
but also classes that provide generic information about
groups of similar objects.
- The kinds of attributes these objects will have (e.g., every book
has a title). These are usually called fields.
- The facilities these objects will have. These are usually called
- In Java, most of your programming will center around classes.
- As you've seen, each class will go in its own file.
- What goes in a class?
- A class definition begins with the keywords
class class and is followed
by the name of the class and the body of the class, surrounded in
- What does in the body of the class?
- The methods.
- The fields.
- Not much else.
- As you may have seen from the first lab session, when you program
in Java you often need to use multiple files.
- Each file provides one part of the program.
- To some, this is a form of modular programming. A program
is built from a number of reusable modules, along with some
- In Java, the
main method of the first class you execute
provides the structure that runs the whole program.
- When you write programs, you should strive to put general parts in
a reuable module.
- Methods provide functionality for classes and objects.
- They are like the functions you know from Scheme.
- Unlike Scheme functions, Java methods must also be typed.
This means that you must specify what kind of information each method
takes as parameters, and what kind of information each method returns.
- A print method might take something to print as a parameter and
- An average method might take numeric values to average as parameters
and return a number.
- Hence each method has
- a name (so that we can tell which method we're using),
- a return type (what kind of value it returns),
- a parameter list (giving names and types of parameters),
- a body (describing what it does), and
- a number of optional components that we won't yet concern ourselves with.
- There are two kinds of methods:
- object methods require an instantiated object to work;
- class methods do not require objects.
- Object methods can refer to all the attributes of the object, as well
as to other methods of the object.
- Class methods can only refer to class attributes and class methods
(they can, of course, refer to object methods in conjunction with an
object of the same class).
- We will almost exclusively use object methods.
Do Lab J2.
- What were some of the interesting things you learned from this lab?
- What was the most challenging part of this lab?
- Created Monday, January 11, 1999
- Added short summary on Friday, January 22, 1999
- Filled in some details on Tuesday, January 26, 1999
- Did a little fine-tuning on Wednesday, January 27, 1999 (including
the addition of the reflection questions)