25
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann

CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

CS 106Introduction to Computer Science I

03 / 23 / 2007

Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Today’s Topics• questions, comments?

• Object Oriented concepts

– data & methods

– constructors

– user defined classes used as types

– objects & object references

example• Let’s relook at the Rectangle.java and

RectangleTester.java• We will

– provide a way to access the private data members (via public “get” and “set” methods)

– provide another constructor just to show you that it's not necessary to have exactly only those 2 we already created

– make sure to look at all the places that the instance variables can get values and provide appropriate error checking so as not to allow invalid values to ever be set.

class / object / constructor terminology

• Programmer defined classes can be used as types, just like classes in the Java API can be used as types (e.g. String.)

• An object of a class is instantiated by declaring a variable (this is the object reference) of that type and allocating space using the new operator and initializing the object by calling a constructor of that class.

– e.g. Time1 time_object = new Time1( );

• here, we are creating an object (referred to as time_object), of a programmer defined class (Time1).

• Objects have attributes (data) and behaviors (methods.)

• The data are the instance variables in a class.

class Time1/* partial incomplete class definition of Time1 from Deitel and Deitel */public class Time1 {

private int hour; // three private instance variablesprivate int minute;private int second;

public Time1() // constructor method{ setTime( 0, 0, 0); }

public void setTime( int h, int m, int s){ // method definition goes here. }

}

setTime method of class Time1 public void setTime( int h, int m, int s)

{ if (h >= 0 && h <= 23) hour = h; else hour = 0; if (m >= 0 && m <= 59) minute = m; else minute = 0; if (s >=0 && s<=59) second = s; else second = 0;}

More methods of class Time1 public void displayTime()

{

System.out.println(“The time is “ + hour + “:” +

minute + “:” + second);

}

public int getHour()

{

return hour;}

public int getMinute()

{

return minute;}

public int getSecond()

{

return second;}

class Time1 Time1 time_object = new Time1( );

• The above line would appear in a method of some class (not Time1) that wishes to use a Time1 object.

• When this happens, memory for the object is allocated (by the new operator) and the instance variables of the class are initialized (by Time1 ( ) ).

• The object has access to any public methods and data defined in the class.

• i.e. time_object is the object variable that has access to (i.e. can refer to or can call) the public data and methods of class Time1.

Referring to members of a class via an object

• The . operator is used along with object to access particular members (data or methods) of a class.

• E.g.–Time1 start_time = new Time1(12,55,0);

– start_time.setTime(13,15,0); // dot operator used

• We may only access public members via an object reference.

public / private members• Instance variables and methods of a class are called

members of the class.• members can be specified as public or private (or

protected --- which we’ll discuss later.)• private members of a class are accessible only to

methods within the class. Private variables can only be referred to within the class. Private methods can only be called from within the class.

• public members of a class are accessible within that class as well as in other classes that contain a reference to an object of that class (via the dot operator).

public / private members

• So, in our example, setTime is a public method of class Time1, and therefore is accessible through time_object by use of the dot operator:

time_object.setTime ( 12, 15, 0 );

• However, all the instance variables of Time1 are declared as private. So, these are not accessible through time_object.

• These private instance variables named, hour, minute and second, are only accessible within methods of Time1.

public / private members• These private instance variables named, hour, minute

and second, are only accessible within methods of Time1.

• Because they are private, we cannot write code like:Time1 start_time = new Time1();

start_time.hour = 12;

start_time.minute = 100;

start_time.second = 10;

• That's good because we shouldn't be able to set the minute to be 100 (it's valid range is 0-59). But then how would we change the hour or minute or second?

public / private members• That's good because we shouldn't be able to set the minute to

be 100 (it's valid range is 0-59). But then how would we change the hour, minute and second?

• This way:

start_time.setTime(12, 100, 10);

• The above code will be allowed to be called but when setTime sees that minute is trying to be set to 100 which is not within the valid range it will set start_time's minute to be 0.

• An example call with a valid time (the time CS106 starts):

start_time.setTime(13, 25, 0);

overloaded constructors• When we create a class, we usually provide constructors

that we want to have called when objects of our class are instantiated.

• We can provide many different constructors, each taking different parameters.

• When we do this, we are said to be overloading the constructor because we are providing different methods of the same name (but different parameters.)

• Java calls the correct constructor, based on the number of and type of the arguments passed in to the method call.

set and get methods• When instance variables of a class are declared as private, we

sometimes want other classes to have access to their values and be able to change them in a controlled way.

• To this end, we usually provide what are called set and get methods for this purpose.

• A set method usually takes in a value as a parameter and checks it for validity. If it is a valid value, then the appropriate instance variable is set (has its value changed to the one passed in.)

• We usually create a get method for each instance variable of which some other class might want to know the value.

• We saw set methods in Rectangle and get methods in Time1.

set and get methods• We can update the class definition of Time1 to contain

set and get methods.public void setHour( int h )

{

if (( h >= 0 && h < 24 ))

hour = h;

else

hour = 0;

}

public int getHour( )

{

return hour;

}

set and get methods• so, we would then be able to call these methods in a program

like:Time1 t1 = new Time1();Time1 t2 = new Time1(); t1.setHour( 11 );t2.setHour( 45 ); // what will happen here?int hour_value;hour_value = t1.getHour( );hour_value = t2.getHour( );

• If hour were a public (instead of private) instance variable of class Time2 then we would be able to:

t2.hour = 45; // bad idea ...

example• Let’s add code (set and get methods) to the Time1

class and also create a class with a main method that will instantiate objects of type Time1.

Review of instance variables• each object has its own copy of all the instance variables in the

class.• Each of the objects of the Time1 class had its own values for the

instance variables (hour, minute, second.)

– e.g. We could have had objects t1, t2, t3, etc. all storing different times (values for hour, minute and second.)

• What were the instance variables of class Rectangle?• None of these instance variables were static.

static variables• A static class variable means there is only one copy of the variable,

regardless of the number of objects instantiated.• one use of static class variables is to keep a count of how many

objects of the class have been instantiated.• This can be done by incrementing a static variable in the

constructor.

Static variables• Static variables represent classwide information.• Instance variables represent object specific information.• What does that mean?

Static variables• Static variables represent classwide information.• Instance variables represent object specific information.• What does that mean?

– Example: class BankCheck to represent a check in a checkbook.• amount• date• lastCheckNumberUsed

– Let's look at example objects of type BankCheck

Static variables• Static variables represent classwide information.• Instance variables represent object specific information.• What does that mean?

– Example: a class to represent a CompactDisc• Artist• Title• TotalTime

– Let's look at example objects of type CompactDisc

Static variables• Because static variables represent classwide information,

we don't need any objects to be instantiated if we want to use them.

• Instead, we use the class name and dot operator to access any public static members (variables or methods.)

static variables• static variables are accessible in other classes if the static

variables are declared public.• They are accessible by using the class name followed by the

dot operator and then the static variable name.• e.g. if there was a public static variable named

time_object_count in the Time1 class, we could access it in another class by referring to:

Time1.time_object_count• If it was declared as private, then we would need to have a

public static method in Time1 to provide “read” access to it. e.g. a method like:

public static int getCount( ) { return time_object_count; }