Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

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

2 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

3 example Let’s create continue with the Rectangle program from last time.

4 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.)‏ Objects have attributes (data) and behaviors (methods.)‏ The data are the instance variables in a class. An object of a class is instantiated by declaring a variable of that type and initializing the object by calling a constructor of that class with the new operator. – e.g. Time time_object = new Time( ); here, we are creating an object (time_object), of a programmer defined class (Time).

5 class Time Time time_object = new Time( ); The above line would appear in a method of some class (not Time) that wishes to use a Time object. When this happens, memory for the object is allocated (by the new operator) and the instance variables of the class are initialized (by Time( ) ). 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 the public data and methods of class Time1.

6 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. – Time start_time = new Time(12,55,0); – start_time.setTime(13,15,0); // dot operator used We may only access public members.

7 Let's create a class Time What instance variables might we have to represent a time? What types will they be? What are the valid values of each of these variables? We can make sure that these variables ALWAYS have valid data in them –make the instance variables private –and in any method that can change the value of an instance variable make sure to not allow it to have an invalid value

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

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

10 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?

11 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);

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

13 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

14 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; }

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

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


Download ppt "CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google