cs 106 introduction to computer science i 03 / 24 / 2008 instructor: michael eckmann

22
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

Post on 19-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

CS 106Introduction to Computer Science I

03 / 24 / 2008

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2008

Today’s Topics• Comments and/or Questions?• Object oriented programming

– more examples• public vs. private• constructors• set/get methods• static

Page 3: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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.

• 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).

Page 4: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

public / private members

• So, in our example, setTime is a public method of class Time, 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 Time 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 Time.

Page 5: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

and second, are only accessible within methods of Time.

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

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?

Page 6: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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(10, 10, 0);

Page 7: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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.

Page 8: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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.

• get methods are sometimes called accessor methods and set methods are sometimes called mutator methods

Page 9: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

set and get methods• We can update the class definition of Time 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;

}

Page 10: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

like:Time t1 = new Time();Time t2 = new Time(); 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 ...

Page 11: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

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

Page 12: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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.

Page 13: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

Page 14: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

Page 15: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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

Page 16: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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.)

Page 17: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

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 Time class, we could access it in another class by referring to:

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

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

public static int getCount( ) { return time_object_count; }

Page 18: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

static methods• we could call this method in the following way:

int current_count;current_count = Time.getCount( );

• Notice that we used the class name (not an object name) before the dot operator to reference the static members of a class.

• We have seen this before, such as when we called a static method of class Integer:

Integer.parseInt( some_str )• Note however that if we have instantiated objects of class

Time, we would be able to access the static members either by the class name or the object name.

Page 19: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

static methods• so, assuming t1 is an object of type Time, after it is

instantiated, we would be able to call a static method getCount( ) either by:

Time.getCount( )or

t1.getCount( )

• It is best (for readability purposes) however to use the class name way. That is, Time.getCount( ) is preferred because someone reading your code will know that getCount ( ) is a static method of class Time, which may be valuable in understanding the code.

Page 20: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

static methods• The methods in class Math that we’ve used are static

methods.• Recall that we don’t create an object of type Math.

Instead, we call the methods in Math with the class name (Math) followed by the dot operator, followed by the method name.

• This is how static methods are called (with the class name, not an object name.)

Page 21: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

Concepts recap• Public vs. private instance variables• Public vs. private methods• Static vs. instance variables• Static vs. non-static methods

Page 22: CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann

Exercise• Let's create a brand new class using all these concepts.• Let's create a class to represent a Movie

– Let's assume we want to store for each movie:• Title• Director• Running time• Year of release

– Let's also assume we want to store a count of all the movies we have instantiated

• Let's also then create a program for inventory of a video/dvd rental store.