

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CSC220: Programming II (Data Structures) - Fall 2003 Dr. Christian A. Duncan [email protected]
Object-oriented programming centers around grouping operations into classes. A class is a data type like an int, a boolean, a float but with many different features. For most of you the following will be a review, but it is important to remember everything and for others not familiar with OO-programming it will be important to learn these concepts. Classes are basically collections of data, which can be integers, characters, booleans, even other classes. In addition, classes provide means to access and use this data by means of methods (aka functions). There are two main types of variables and methods, static and non-static (aka member. Static items are items associated with the class as a whole while the member items are associated with an instance of a class (or member of the class). These instances are typically referred to as objects, and hence the name object-oriented programming. We shall discuss how one creates and uses classes and objects shortly and that should help you understand what this topic is all about. The best way to see this is through an example.
public class Throttle { private int top; // Instance variables private int position;
// Is there a default constructor???
// The constructor (or one possible constructor) public Throttle(int size) { if (size <= 0) throw new IllegalArgumentException("Size <= 0: " + size); top = size; position = 0; // not needed but ALWAYS good to add for safety :-) }
public double getFlow() { return (double) position / (double) top; // Why is the typecasting done? }
public boolean isOn() { return (position > 0); }
public void shutOff() { position = 0; }
public void shift(int amount) { if (amount > top - position) position = top;
else if (position + amount < 0) position = 0; else position += amount; }
public static void main(String[] args) { final int SIZE = 8; final int SPOT = 3;
Throttle small = new Throttle(SIZE); small.shift(SPOT); System.out.print("I am at position "); System.out.println(SPOT + " out of " + SIZE + "."); System.out.println("The flow is now: " + small.getFlow());
Throttle copy; copy = small; copy.shift(SPOT); System.out.println("The flow of copy is now: " + copy.getFlow()); System.out.println("The flow of small is now: " + small.getFlow()); } }
There are several things to note here. In the code we have the following features:
Some other things to be wary about.
The book covers an area called packages, which we shall skip for this lecture, but we may cover it in a lab at one point.
Static methods are those methods associated with the class as a whole. The best example of this is the book’s example of a Location class. Here is a small excerpt of it.
public class Location implements Cloneable { private double x; private double y;
public Location(double initX, double initY) { x = initX; y = initY; }
public Object clone() {