CS 65 - Introduction to Computer Science I Anatomy of a Class

Document Sample
CS 65 - Introduction to Computer Science I Anatomy of a Class Powered By Docstoc
					CS 65 - Introduction to Computer Science I Anatomy of a Class

Example: A simple class to model an automobile.

Automobile Amount of fuel Speed License plate Increase speed Stop

Instantiations of the class Automobile are called
Object: bobsCar Amount of fuel: 10 gallons Speed: 55 mph License plate: “135 XJK” Object: suesCar Amount of fuel: 14 gallons Speed: 0 mph License plate: “SO COOL”

objects.
Object: myCar Amount of fuel: 2 gallons Speed: 75 mph License plate: “000 AAA”

Classes support abstraction and encapsulation, and allow us to define our own data types. There are two ways to think about a class: 1) as an application programmer incorporating the class definition in a program (think String or System.out or URLTester); 2) as the designer and implementor of the class. Another example:
NumberDisplay -limit : int -value : int +NumberDisplay(int rollOverLimit) +getValue() : int +getDisplayValue() : String +setValue(int replacementValue) : void +increment() : void

1) What the application programmer needs to know about a class
NumberDisplay

The NumberDisplay class represents a digital number display that can hold values from zero to a given limit. The limit can be specified when creating the display. The values range from zero (inclusive) to limit-1. If used, for example, for the seconds on a digital clock, the limit would be 60, resulting in display values from 0 to 59. When incremented, the display automatically rolls over to zero when reaching the limit. A public class to create a naive ticket machine.

Constructor (method for creating an instance of the class; there may be more than one constructor):
public NumberDisplay(int rollOverLimit) // Parameterized constructor // Post: limit initialized to rollOverLimit; value initialized to 0

Methods (defining operations applicable to objects of the type): - mutator methods change the state of the object (instance variable values); mutate data - accessor methods allow access to the private data portions of the class
public int getValue() // accessor method // Post: data member value is returned public String getDisplayValue() // accessor method // Post: return value as two-digit String public void setValue(int replacementValue) // mutator method // Post: value is set to replacementValue public void increment() // mutator method // Post: display value increased by 1

Classifying methods by return type - "action" usually encapsulated in a void method; a procedure - "define a value" methods indicate type of value to be returned; a function Provided with the class interface, an applications programmer makes use of the definitions for NumberDisplay without having to know all the details of how NumberDisplay works. A sample driver program follows. A driver allows you to test the functionality of some class. Trace through the program, sketching memory to show what all variables contains.
public class SeeingDigits { /** Programmer: J. McTaggart Assignment: Driver to test NumberDisplay class Date: 02.19.06 Purpose: */ public static void main(String[] args) { // declare and instantiate a NumberDisplay object NumberDisplay display1 = new NumberDisplay(30); // initialize the value display1.setValue(5); // access the "attribute" value and the display value System.out.print("display1 has value " + display1.getValue()); System.out.println(" and will display: " + display1.getDisplayValue()); // "exercise" and re-display for (int i=1; i<=28; i++) display1.increment(); System.out.print("display1 has value " + display1.getValue()); System.out.println(" and will display: " + display1.getDisplayValue()); // declare and instantiate a second NumberDisplay object NumberDisplay display2 = new NumberDisplay(60); // see default value and display value System.out.print("\ndisplay2 has value " + display2.getValue()); System.out.println(" and will display: " + display2.getDisplayValue());

// check for "rollover" display2.setValue(57); display2.increment(); System.out.print("display2 has value " System.out.println(" and will display: display2.increment(); System.out.print("display2 has value " System.out.println(" and will display: display2.increment(); System.out.print("display2 has value " System.out.println(" and will display: } }

+ display2.getValue()); " + display2.getDisplayValue()); + display2.getValue()); " + display2.getDisplayValue()); + display2.getValue()); " + display2.getDisplayValue());

2) What the designer/implementor of the class knows
/** * The NumberDisplay class represents a digital number display that can hold values from zero * to a given limit. The limit can be specified when creating the display. The values range * from zero (inclusive) to limit-1. If used, for example, for the seconds on a digital clock, * the limit would be 60, resulting in display values from 0 to 59. When incremented, the * display automatically rolls over to zero when reaching the limit. * * @author Michael Kolling and David J. Barnes * @version 2001.05.26 */ public class NumberDisplay { // instance data (attributes) private int limit; private int value; /** * Constructor for objects of class Display */ public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } /** * Return the current value. */ public int getValue() { return value; } /** * Return the display value (that is, the current value as a two-digit String. If the value * is less than ten, it will be padded with a leading zero). */ public String getDisplayValue() { if(value < 10) return "0" + value; else return "" + value; } /** * Set the value of the display to the new specified value. If the new value is less than * zero or over the limit, do nothing. */ public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) value = replacementValue; } /** * Increment the display value by one, rolling over to zero if the limit is reached. */ public void increment() { value = (value + 1) % limit; } }