Week 4: Objects and classes

 

More Object Oriented Programming ("OO")

 

Using existing classes

Examples (Vector again):

// Declaration:
Vector v1;
Vector v2;

// Construction:
v1 = new Vector ();

// Combined declaration and construction:
Vector v3 = new Vector ();

// Call methods on the objects.
v1.addElement ("Hello");
int s = v2.size ();
v3.setCapacity (34);

// This does not copy the data from v3 into v2.
// Instead, it makes v2 and v3 refer to the same object.
v2 = v3;

// Set v1 to null. 
v1 = null;

FYI - Some More "Complex" Data Structure

 

Create Your Own Classes

Examples:

class Fraction
{
	// Class data.
	int numerator = 0;
	int denominator = 1;

	// Member methods.
	double toDouble ()
	{
		// We need to cast the ints to doubles before
		// dividing so that we do not lose the decimal part.
		return (double) numerator / (double) denominator;
	}

	// Mult by another Fraction Object
	void multiplyBy (Fraction other)
	{
		numerator *= other.numerator;
		denominator *= other.denominator;
	}
}

Constructors

Examples:

class Fraction
{
	// Member data.
	// ...

	// Constructors.
	Fraction ()
	{
		numerator = 0;
		denominator = 1;
	}

	Fraction (int n, int d)
	{
		numerator = n;
		denominator = d;
	}

	// Member methods.
	// ...
}

Example usages:

Fraction f1 = new Fraction ();
Fraction f2 = new Fraction (4, 5);

Access Modifiers

Examples:

class Fraction
{
	// Member data.
	private int numerator;
	private int denominator;

	// Constructors.
	public Fraction ()
	{
		numerator = 0;
		denominator = 1;
	}

	public Fraction (int n, int d)
	{
		numerator = n;
		if (d != 0)
			denominator = d;
		else 
			denominator = -9999;
	}

	// Member methods.
	public int getNumerator ()
	{
		return numerator;
	}

	public int getDenominator ()
	{
		return denominator;
	}

}

toString() method

Examples:

class Fraction
{
	// ...

	public String toString ()
	{
		return Integer.toString (numerator) + "/" 
		     + Integer.toString (denominator);
	}
}

The this Object Reference

The keyword this refers to the object on which the method operates

Example: Java Date class has a toString() method that prints out the object
The current date stored in a date variable will be: this.toString()

Example:
class Account
{
	public Account(String n)
	{
		this(n, Account.generateKey());
	}
	public Account(String n, int key)
	{
		customerName = n;
		accountNum = key;
	}
	...
}

Destructors

 

Packages

Examples:

package edu.umn.JavaClass; // import a specific class

class Fraction
{
	// ...
}

Class design hints

Object wrappers

Lab

Create an inventory application for a retail store. Your application should be made up of 3 classes:

  1. A class called StockItem which represents a stock item in an inventory. It should have:
  2. A class called CustomerTransaction which represents a customer purchasing some items. It should have:
  3. A class called Week4 which is really just for the main() method of the application. It should:

Copyright 1996-2001 OpenLoop Computing. All rights reserved.