defining classes i part a. class definitions oop is the dominant programming methodology in use...

29
Defining Classes I Defining Classes I Part A Part A

Upload: herbert-mcdowell

Post on 14-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Defining Classes IDefining Classes I

Part APart A

Class definitionsClass definitions

OOP is the dominant programming OOP is the dominant programming methodology in use today.methodology in use today.

Classes are the most important language Classes are the most important language feature of OOP.feature of OOP.

Instance (value) of the class = a particular Instance (value) of the class = a particular object (a particular value of a class type)object (a particular value of a class type)

A class is a type and you can declare A class is a type and you can declare variables of a class type.variables of a class type.

Class definitionsClass definitions

Attributes, fields, or properties = dataAttributes, fields, or properties = dataMethods = functionMethods = functionMembers = data + functions (or attributes Members = data + functions (or attributes

+ methods)+ methods)

Example classExample class

Consider dates such as December 31, 2007 and Consider dates such as December 31, 2007 and July 4, 1776.July 4, 1776.

We would like to develop a class to represent We would like to develop a class to represent dates.dates.

public class Date {public class Date {

}}

What’s our next step?What’s our next step?

Example classExample class

What attributes (data) do we need?What attributes (data) do we need?To represent the month?To represent the month?To represent the day?To represent the day?To represent the year?To represent the year?

Example classExample class

public class Date {public class Date {

public Stringpublic String mMonth;mMonth;

public intpublic int mDay;mDay;

public intpublic int mYear;mYear;

}}What does “public” mean for the month?What does “public” mean for the month?

Conventions (mandatory)Conventions (mandatory)

1.1. One class per file. File name is always One class per file. File name is always classnameclassname.java..java.

2.2. What does “public” mean for the month?What does “public” mean for the month?3.3. Let’s adopt the convention that all class Let’s adopt the convention that all class

names will begin with an uppercase names will begin with an uppercase letter.letter.

4.4. Let’s adopt the convention that all Let’s adopt the convention that all attributes will begin with ‘m’ (to attributes will begin with ‘m’ (to distinguish them from local variables).distinguish them from local variables).

Example classExample class

public class Date {public class Date {

public Stringpublic String mMonth;mMonth;

public intpublic int mDay;mDay;

public intpublic int mYear;mYear;

}} How do we declare an object of type Date?How do we declare an object of type Date? How do we create an instance of this class?How do we create an instance of this class?

Example classExample class

public class Date {public class Date {public Stringpublic String mMonth;mMonth;public intpublic int mDay;mDay;public intpublic int mYear;mYear;

}} How do we declare an object of type Date?How do we declare an object of type Date?

Rule of Date dt;Rule of Date dt; How do we create an instance of this class?How do we create an instance of this class?

dt = new Date();dt = new Date();

Date d2 = new Date(); //both opsDate d2 = new Date(); //both ops

Class definitionClass definition

In general, a class definition is of the form:In general, a class definition is of the form:public class Class_Name {public class Class_Name {

Instance_Variable_Declaration_1Instance_Variable_Declaration_1Instance_Variable_Declaration_2Instance_Variable_Declaration_2……Instance_Variable_Declaration_LastInstance_Variable_Declaration_Last

Method_Definition_1Method_Definition_1Method_Definition_2Method_Definition_2……Method_Definition_LastMethod_Definition_Last

}}

Example classExample class

A common operation is to ask the object to A common operation is to ask the object to create a string that represents it values. create a string that represents it values. We’d like to say:We’d like to say:Date dt = new Date();Date dt = new Date();

System.out.println( “date is “ + dt.toString() );System.out.println( “date is “ + dt.toString() );

What must we add to Date to make this What must we add to Date to make this happen?happen?

Example classExample class

public class Date {public class Date {public Stringpublic String mMonth;mMonth;public intpublic int mDay;mDay;public intpublic int mYear;mYear;

public String toString ( ) {public String toString ( ) {return mMonth + “ “ + mDay + “, “ + mYear;return mMonth + “ “ + mDay + “, “ + mYear;

}}}}

Methods in generalMethods in general

(aka function, procedures)(aka function, procedures)Method definition vs. method invocationMethod definition vs. method invocationMay have an optional return statement to May have an optional return statement to

optionally return a value to the caller.optionally return a value to the caller.

Methods in generalMethods in general

Definition:Definition:

public type_returned method_name ( optional_parameter_list )public type_returned method_name ( optional_parameter_list )

{{

//body of method (containing local var definitions and other//body of method (containing local var definitions and other

// statements.// statements.

//if type_returned is not void, there must be at least one return//if type_returned is not void, there must be at least one return

// statement that returns a value to the caller.// statement that returns a value to the caller.

}}

Example methodsExample methods

public int getDay ( ) {public int getDay ( ) {return mDay;return mDay;

}}

/** yet another overloaded version of setDate with/** yet another overloaded version of setDate with * a single parameter of type int* a single parameter of type int */*/public void setDate ( int year ) {public void setDate ( int year ) { setDate(1, 1, year);setDate(1, 1, year);}}

Return statementReturn statement

The return statement exits the current function The return statement exits the current function and and optionallyoptionally returns a value to the caller. returns a value to the caller.

public int getDay ( ) {public int getDay ( ) {

return mDay;return mDay;

}}

……

int d = dt.getDay();int d = dt.getDay();

Returning booleanReturning boolean

Consider the following:Consider the following:public boolean checkRange ( int i ) {public boolean checkRange ( int i ) {

if (1<=i && i<=6) return true;if (1<=i && i<=6) return true;

return false;return false;

}}

Retuning booleanRetuning boolean

Consider the following:Consider the following:public boolean checkRange ( int i ) {public boolean checkRange ( int i ) {

if (1<=i && i<=6) return true;if (1<=i && i<=6) return true;return false;return false;

}}

public boolean checkRange2 ( int i ) {public boolean checkRange2 ( int i ) {return (1<=i && i<=6);return (1<=i && i<=6);

}}

Types of variablesTypes of variables

LocalLocal Declared w/in methodsDeclared w/in methods Function parameters can be thought of as local Function parameters can be thought of as local

variablesvariables

ClassClass Declared w/in a classDeclared w/in a class Not declared w/in any methodNot declared w/in any method We will adopt the convention of starting all of these We will adopt the convention of starting all of these

class variables with class variables with mm.. Ex. String mMonth;Ex. String mMonth;

The The thisthis parameter parameter

Every (non static) method has an implicit, pre-Every (non static) method has an implicit, pre-defined parameter called defined parameter called thisthis..

thisthis can be used to refer to class variables or can be used to refer to class variables or methods.methods.

thisthis is a keyword. is a keyword.

thisthis refers to the current object instance. refers to the current object instance.

When do we use When do we use thisthis??

When a function parameter (or local When a function parameter (or local variable) name variable) name masksmasks a class variable a class variable name.name.But our convention avoids this situation in But our convention avoids this situation in

most cases. So you shouldn’t need to use most cases. So you shouldn’t need to use thisthis very often. very often.

But it doesn’t hurt:But it doesn’t hurt:public int getDay ( ) {public int getDay ( ) {

return this.mDay;return this.mDay;}}

When we must use When we must use thisthis

int where = 12;int where = 12;

public void foo ( int where ) {public void foo ( int where ) {where = 92;where = 92;

}}

Did the class variable called Did the class variable called wherewhere change change to 92?to 92?

When we must use When we must use thisthis

int where = 12;int where = 12;

public void foo ( int where ) {public void foo ( int where ) {

this.where = 92;this.where = 92;

where = 7;where = 7;

}}

Determine if two dates are equalDetermine if two dates are equal

Recall what happened when we used == Recall what happened when we used == to test if two strings are equal.to test if two strings are equal.

What method did we use?What method did we use?Let’s write our own for dates.Let’s write our own for dates.

Determine if two dates are equalDetermine if two dates are equal

public boolean equals ( Date other ) {public boolean equals ( Date other ) {

return ( mDay == other.mDayreturn ( mDay == other.mDay

&& mMonth.equals( other.mMonth )&& mMonth.equals( other.mMonth )

&& mYear==mYear );&& mYear==mYear );

}}

Function to determine if one date Function to determine if one date precedes another dateprecedes another date

Note that December 31, 1970 precedes Note that December 31, 1970 precedes January 1, 1971.January 1, 1971.

Note that December 31, 1971 does not Note that December 31, 1971 does not precede January 1, 1970.precede January 1, 1970.

Note that January 1, 1970 does not Note that January 1, 1970 does not precede January 1, 1970.precede January 1, 1970.

Write a function to determine if one date Write a function to determine if one date precedes another date.precedes another date.

public boolean precedes ( Date other ) {public boolean precedes ( Date other ) {

return ( ( mYear<other.mYear)return ( ( mYear<other.mYear)

|| (mYear==other.mYear && getMonth()<other.getMonth())|| (mYear==other.mYear && getMonth()<other.getMonth())

|| (mYear==other.mYear && mMonth.equals(other.mMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) &&&&

mDay<other.mDay) );mDay<other.mDay) );

}}

I personally would never write it this way!I personally would never write it this way!

public boolean precedes ( Date other ) {public boolean precedes ( Date other ) {

return ( ( mYear<other.mYear)return ( ( mYear<other.mYear)

|| (mYear==other.mYear && getMonth()<other.getMonth())|| (mYear==other.mYear && getMonth()<other.getMonth())

|| (mYear==other.mYear && mMonth.equals(other.mMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) &&&&

mDay<other.mDay) );mDay<other.mDay) );

}}

public boolean precedes2 ( Date other ) {public boolean precedes2 ( Date other ) {

if (mYear < other.mYear)if (mYear < other.mYear) return true;return true;

//year must be the same//year must be the same

if (getMonth() < other.getMonth())if (getMonth() < other.getMonth()) return true;return true;

//year and month must be the same//year and month must be the same

return (mDay<other.mDay);return (mDay<other.mDay);

}}

TestingTesting

Use a separate program called a Use a separate program called a driverdriver to to test the classes that you develop.test the classes that you develop.

Drivers typically have a main. Your Drivers typically have a main. Your classes should not.classes should not.