Defining Classes I
Defining Classes I. Part A. Class definitions. OOP is the dominant programming methodology in use today. Classes are the most important language feature of OOP. Instance (value) of the class = a particular object (a particular value of a class type)
Defining Classes I
E N D
Presentation Transcript
Defining Classes I Part A
Class definitions • OOP is the dominant programming methodology in use today. • Classes are the most important language feature of OOP. • Instance (value) of the class = a particular object (a particular value of a class type) • A class is a type and you can declare variables of a class type.
Class definitions • Attributes, fields, or properties = data • Methods = function • Members = data + functions (or attributes + methods)
Example class • Consider dates such as December 31, 2007 and July 4, 1776. • We would like to develop a class to represent dates. public class Date { } • What’s our next step?
Example class • What attributes (data) do we need? • To represent the month? • To represent the day? • To represent the year?
Example class public class Date { public String mMonth; public int mDay; public int mYear; } • What does “public” mean for the month?
Conventions (mandatory) • One class per file. File name is always classname.java. • What does “public” mean for the month? • Let’s adopt the convention that all class names will begin with an uppercase letter. • Let’s adopt the convention that all attributes will begin with ‘m’ (to distinguish them from local variables).
Example class public class Date { public String mMonth; public int mDay; public int mYear; } • How do we declare an object of type Date? • How do we create an instance of this class?
Example class public class Date { public String mMonth; public int mDay; public int mYear; } • How do we declare an object of type Date? Rule of Date dt; • How do we create an instance of this class? dt = new Date(); Date d2 = new Date(); //both ops
Class definition • In general, a class definition is of the form: public class Class_Name { Instance_Variable_Declaration_1 Instance_Variable_Declaration_2 … Instance_Variable_Declaration_Last Method_Definition_1 Method_Definition_2 … Method_Definition_Last }
Example class • A common operation is to ask the object to create a string that represents it values. We’d like to say: Date dt = new Date(); System.out.println( “date is “ + dt.toString() ); • What must we add to Date to make this happen?
Example class public class Date { public String mMonth; public int mDay; public int mYear; public String toString ( ) { return mMonth + “ “ + mDay + “, “ + mYear; } }
Methods in general • (aka function, procedures) • Method definition vs. method invocation • May have an optional return statement to optionally return a value to the caller.
Methods in general • Definition: public type_returned method_name ( optional_parameter_list ) { //body of method (containing local var definitions and other // statements. //if type_returned is not void, there must be at least one return // statement that returns a value to the caller. }
Example methods public int getDay ( ) { return mDay; } /** yet another overloaded version of setDate with * a single parameter of type int */ public void setDate ( int year ) { setDate(1, 1, year); }
Return statement • The return statement exits the current function and optionally returns a value to the caller. public int getDay ( ) { return mDay; } … int d = dt.getDay();
Returning boolean • Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; }
Retuning boolean • Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; } public boolean checkRange2 ( int i ) { return (1<=i && i<=6); }
Types of variables • Local • Declared w/in methods • Function parameters can be thought of as local variables • Class • Declared w/in a class • Not declared w/in any method • We will adopt the convention of starting all of these class variables with m. • Ex. String mMonth;
The this parameter • Every (non static) method has an implicit, pre-defined parameter called this. • this can be used to refer to class variables or methods. • this is a keyword. • this refers to the current object instance.
When do we use this? • When a function parameter (or local variable) name masks a class variable name. • But our convention avoids this situation in most cases. So you shouldn’t need to use this very often. • But it doesn’t hurt: public int getDay ( ) { return this.mDay; }
When we must use this int where = 12; public void foo ( int where ) { where = 92; } Did the class variable called where change to 92?
When we must use this int where = 12; public void foo ( int where ) { this.where = 92; where = 7; }
Determine if two dates are equal • Recall what happened when we used == to test if two strings are equal. • What method did we use? • Let’s write our own for dates.
Determine if two dates are equal public boolean equals ( Date other ) { return ( mDay == other.mDay && mMonth.equals( other.mMonth ) && mYear==mYear ); }
Function to determine if one date precedes another date • Note that December 31, 1970 precedes January 1, 1971. • Note that December 31, 1971 does not precede January 1, 1970. • Note that January 1, 1970 does not precede January 1, 1970. • Write a function to determine if one date precedes another date.
public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } I personally would never write it this way!
public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } public boolean precedes2 ( Date other ) { if (mYear < other.mYear) return true; //year must be the same if (getMonth() < other.getMonth()) return true; //year and month must be the same return (mDay<other.mDay); }
Testing • Use a separate program called a driver to test the classes that you develop. • Drivers typically have a main. Your classes should not.