Docsity

Lecture 3: Object-Oriented Programming and Classes in CSC220 - Fall 2003, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 3
CSC220: Programming II (Data Structures) - Fall 2003
Dr. Christian A. Duncan
1 Announcements
•(Continue to) Read Chapters 1 and 2 and start to read Chapter 3.
•Labs start next week.
•You will be given your first homework assignment today. It is a written assignment (possibly the only
written for the semester). It will be due Tuesday, September 16.
•ACM Meeting today (4:25 in Engineering Bldg. Room 502)
2 Classes
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;
1
pf3

Partial preview of the text

Download Lecture 3: Object-Oriented Programming and Classes in CSC220 - Fall 2003 and more Study notes Computer Science in PDF only on Docsity!

Lecture 3

CSC220: Programming II (Data Structures) - Fall 2003 Dr. Christian A. Duncan [email protected]

1 Announcements

  • (Continue to) Read Chapters 1 and 2 and start to read Chapter 3.
  • Labs start next week.
  • You will be given your first homework assignment today. It is a written assignment (possibly the only written for the semester). It will be due Tuesday, September 16.
  • ACM Meeting today (4:25 in Engineering Bldg. Room 502)

2 Classes

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:

  • instance variables (aka member variables) - note they are private
  • instance methods - not they are usually public
  • constructor - used to create an instance of this class
  • main method - mostly for testing purposes to test the class out (really should be part of another class
    • like ThrottleTester)

Some other things to be wary about.

  • Remember that variables refer to an object. They are not really the object itself. So, small and copy both refer to the same object in this example.
  • Testing equality (small == copy) is the same as testing if they refer to the same object, not if they have identical contents.
  • Clones - copy is not a true copy of small, it actually is just a copy of the reference that small has - so they refer to the same object.

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.

2.1 Static methods

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() {