CSC 207 Grinnell College Spring, 2012
Algorithms and Object-Oriented Design

An Introduction to Objects


This reading begins a transition to object-oriented problem solving, assuming background in functional problem solving (with Scheme) and imperative problem solving (with C). The reading proceeds in three main sections:

  1. Introduction to the concept of an object
  2. Review of a simple object-oriented program and test suite written in Scheme
  3. Direct translation of the Scheme program and testing to Java

The Object Concept

A basic motivation for object-oriented (OO) problem solving involves managing complexity. Rather than define various data structures independently from operations, OO problem solving packages data together with the operations that operate on them.



Consider a simple class that would maintain information about a course (e.g., CSC 207). Each object in the class might have the following characteristics:

A Simple Course Class in Scheme

Higher-order procedures provide one way to implement an object within Scheme. Within a higher-order procedure, a let or let* provides a mechanism to define the primary data fields.

Storage of Fields within a Vector

One way to define fields within a higher-order procedure follows the style of CSC 151; the fields may be placed within a locally-defined vector content, and each index of the vector can have a specified interpretation:

   (define course
      (lambda (subj . other-data)
         (let (;; content:  listing of private data as fields within a vector
               (content (make-vector 4))
               ;;define positions within content as data elements
               (subject 0)(number 1)(credits 2)(title 3))

With this approach, the course subject can be access with

   (vector-ref content subject) ;; retrieve subject data from index 0
   (vector-set! content subject "CSC") ;; set subject to CSC

Within this framework, operations can be given as parameters to the defined higher-level procedure. For example, the following scenario shows how two courses might be defined and used:

   ;; initialize two courses, with higher-order procedure course
   (define myCourse (course "CSC"  207  4  "Algorithms and Object-Oriented Design"))
   (define yourCourse (course "Math"))
   ;; test of initialization, toString, and extractor methods
   (myCourse 'toString)
   (yourCourse 'getSubject)
   (yourCourse 'getNumber)
   (yourCourse 'getTitle)
   (yourCourse 'getCredits)
   ;; set fields
   (yourCourse 'setNumTitle 131 "Calculus I")
   (yourCourse 'setCredits 4)
   ;; print results of modification
   (yourCourse 'toString)

To clarify this use of a higher-order course procedure, the course procedure provides a mechanism to define two objects. In the example, the definition of myCourse allows a user to define an object with four types of data: subject, course number, credits, and title. The definition of yourCourse allows a user to define an object with just a subject (default values are given for the other fields).

Program provides the full Scheme code, with testing. As this program illustrates, data are stored in fields of a vector, and vector-set! is used to adjust those fields, as appropriate.

Storage of Fields as Separate Variables

Rather than store all data fields within a single vector, a second approach uses separate local variables for each field. In this approach, field variables can be defined with default initialization at the beginning of the higher-order course procedure:

   (define course
      (lambda (subj . other-data)
         (let (;; fields:  listing of private data
               (subject subj) ;; the subject or department name
               (number 0)     ;; the course number
               (credits 0)    ;; the number of credits
               (title ""))    ;; the name of the course

The rest of this procedure is similar to the first approach, except that the variables can be accessed directly (rather than through the vector) and variables are changed with set! rather than vector-set!.

Program provides the full Scheme code for this alternative approach.

Some Questions

In reviewing these two approaches for defining objects in Scheme, be sure that you can answer the following questions:

Moving from Scheme to Java

These notes highlight the in-class discussion of a Course class, as defined in These notes are not designed to be comprehensive. Rather, they provide a brief commentary on the program. You may want to consult more substantive sources for a more complete review of the Java programming language. In what follows, the commentary starts with the first lines of code and proceeds through to the end.


Java programs identify comments in two ways:


Java organizes classes into modules, called packages. When writing small programs, such an organization may seem unnecessary. However, Java was designed for large-scale applications, in which there may be a very large number of classes. In that context, it is important to keep track of the names of various classes, and there is potential for the developers of one part of the program to use the same class names as have been specified by developers of another part. That is, developers of different parts of a program might use the same class names, and Java would need to determine which class was meant at each stage. Thus, the designers of Java decided to organize classes into packages, so the classes within one package could be distinguished from those in another.

The package name of a class is specified with the statement

   package course;

Within Unix and Linux systems, the convention is that a package name corresponds to a directory, and all classes within a package are stored within this designated directory.

Within the context of a package, names may be easy to identify. However, within a broader programming context, Java employs a naming convention for packages that reflects the directory structure. Thus, the Java "standard" suggests that you follow a similar style to a Web URL, although the ordering of the elements is reversed. For example, some of my sample Java programs might be in a package that starts with

Class Definitions

Java supports object-oriented problem solving, and all programs in Java begin with classes and objects. Thus, each program component is a class, and a file begins with the declaration of one class. In the example, the class Course is identified as public, meaning that any application is allowed to utilize this class in its work.


As with C, variables in Java must be declared, and the type of the variable must be specified.


Whenever objects are created from classes, all fields are initialized.

Extractor and Modifier Methods

Extractor methods return data from various fields, while modifier methods change the values stored in those fields.


In Java, as in C, the brackets { and } serve as begin and end markers. Thus, brackets in Java have a similar function to parentheses in Scheme.

Strings and String Operations

Java contains strings and string operations that are analogous to those in Scheme.


Java provides extensive capabilities for input and output through predefined class libraries. To get started, the simplest approach for printing involves a System object that is established whenever a Java program is run. To print, one constructs a desired string as parameters to methods print and println:

   System.out.print ("counting:  1, " + "2, " + "3, ");
   System.out.println ("..., done");
   System.ut.println ("next line");

When executed, these two lines produce the following output:

counting: 1, 2, 3, ... done
next line

In this example, several strings (e.g., "counting: 1, ", "2, ", and "3, ") are concatenated to form a single large string, and this string is printed. When using print, no "new line" character is sent to the terminal. Thus, printing for the next statement (println) continues on the same line. The println method always move to a new line when it has finished printing, so subsequent printing starts on the following line.

Java's main Method

Once we defined classes in Scheme, we tested and used them in separate code, and Java also allows classes to be used by other classes and programs. In addition, Java allows any class to have a main method, which can be used to run a program based on the given class. In this regard, Java's main method is analogous to C's main procedure.

This document is available on the World Wide Web as

created 3 April 2001
last revised 22 January 2012
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at

Copyright © 2011-2012 Henry M. Walker.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .