Held Friday, January 28, 2000
Today's outline does not yet have an overview. Please
let me know if you think it should have one.
- Monday at noon in Science 2413, the Computer Science Bag Lunch
Film festival will resume with ``The Future of Computing: Seizing
the Future We Want''. You may find it interesting to attend.
- Workload check: How much time did it take to complete lab J1?
- Assignment 1:
Complete and turn in lab J2
(due Tuesday). [The due date is incorrect in the ``At a Glance''
handout you received.]
- Start reading Lab J3
- The structure of a class, revisited
- Scheme vs. Java
- Lab J2: Objects and Methods
- Preparatory reading of Lab J2
- Now that we've written some simple programs, we're ready to
consider more details about the construction of classes.
- There are three key parts to each class:
- The fields that categorize the attributes of objects
in the class.
- The methods that describe and implement the capabilities
of objects in the class.
- The constructors that allow us to construct new objects
in the class.
- We'll consider each in turn as we attempt to build a simple
- We begin by identifying potential attributes, methods, and
- (Intentionally left out of the outline.)
- You must enclose the fields, methods, and constructors of a
class in a class declaration.
- Each class gets its own file (at least in CS152).
- The typical structure of a class is as follows.
* A helpful introductory comment that describes the class.
public class ClassName
// Field declarations
} // class ClassName
- There are three parts to a field declaration. In reverse order,
- The name of the field.
- The type of the field. (The kinds of values it can take on.)
- The word
- For example,
- You may have noted that field declarations look a lot like
variable declarations. How do they differ?
- Field declarations are part of a class; variable declarations
are part of a method definition.
- Fields are available ``anywhere'' in the class; variables
are available only in the method they are declared in.
- Fields have protection levels; variables do not.
- Variables may be initialized when they are declared;
fields typically are not.
- When you reference fields, you should preface them with
- What should the fields for our class look like?
- The things you called functions in Scheme we call
methods in Java.
- There are typically 6 parts to a method declaration.
- A comment that describes the purpose of the method.
- The protection level of the method (typically
- The type of value the method returns. You can use
void when a method doesn't return anything.
- The name of the method.
- The parameters of the method.
- The body of the method.
- For example
public String getName()
} // getName()
public void setName(String newName)
this.name = newName;
} // setName(String)
- We tend to classify methods as observers and modifiers.
- Observers extract a value from the object without
changing the object.
- Modifiers change the object.
- Constructors are special methods used to create new objects
within the class. They are much like methods except that
you don't include the type.
- Whenever you build a new object in a class, you call a
Just so you know what you're getting into, I've put together a
small chart comparing Scheme to Java.
||List, Vector, String, Number, Atom
||Primitive (numbers, characters), Object, String, ...
||Anonymous functions; functions as data
||Size; exceptions; polymorphism
||Compiled (plus JVM interpreter)
protection type name(typed-arguments)
(function arg1 ...
|Return value from function