Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.

Similar presentations


Presentation on theme: "Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in."— Presentation transcript:

1 Defining Classes I Part A

2 Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in use today. Classes are the most important language feature of OOP. Classes are the most important language feature of OOP. Instance (value) of the class = a particular object (a particular value of a class type) 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. A class is a type and you can declare variables of a class type.

3 Class definitions Attributes, fields, or properties = data Attributes, fields, or properties = data Methods = function Methods = function Members = data + functions (or attributes + methods) Members = data + functions (or attributes + methods)

4 Example class Consider dates such as December 31, 2007 and July 4, 1776. Consider dates such as December 31, 2007 and July 4, 1776. We would like to develop a class to represent dates. We would like to develop a class to represent dates. public class Date { } What’s our next step? What’s our next step?

5 Example 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?

6 Example class public class Date { public StringmMonth; public intmDay; public intmYear; } What does “public” mean for the month? What does “public” mean for the month?

7 Conventions (mandatory) 1. One class per file. File name is always classname.java. 2. What does “public” mean for the month? 3. Let’s adopt the convention that all class names will begin with an uppercase letter. 4. Let’s adopt the convention that all attributes will begin with ‘m’ (to distinguish them from local variables).

8 Example class public class Date { public StringmMonth; public intmDay; public intmYear; } 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?

9 Example class public class Date { public StringmMonth; public intmDay; public intmYear; } How do we declare an object of type Date? How do we declare an object of type Date? 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(); Date d2 = new Date(); //both ops

10 Class definition In general, a class definition is of the form: In general, a class definition is of the form: public class Class_Name { Instance_Variable_Declaration_1Instance_Variable_Declaration_2…Instance_Variable_Declaration_LastMethod_Definition_1Method_Definition_2…Method_Definition_Last}

11 Example class A common operation is to ask the object to create a string that represents it values. We’d like to say: 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? What must we add to Date to make this happen?

12 Example class public class Date { public StringmMonth; public intmDay; public intmYear; public String toString ( ) { return mMonth + “ “ + mDay + “, “ + mYear; }}

13 Methods in general (aka function, procedures) (aka function, procedures) Method definition vs. method invocation Method definition vs. method invocation May have an optional return statement to optionally return a value to the caller. May have an optional return statement to optionally return a value to the caller.

14 Methods in general Definition: 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. }

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

16 Return statement The return statement exits the current function and optionally returns a value to the caller. The return statement exits the current function and optionally returns a value to the caller. public int getDay ( ) { return mDay; }… int d = dt.getDay();

17 Returning boolean Consider the following: Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; }

18 Retuning boolean Consider the following: 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); }

19 Types of variables Local Local Declared w/in methods Declared w/in methods Function parameters can be thought of as local variables Function parameters can be thought of as local variables Class Class Declared w/in a class Declared w/in a class Not declared w/in any method Not declared w/in any method We will adopt the convention of starting all of these class variables with m. We will adopt the convention of starting all of these class variables with m. Ex. String mMonth; Ex. String mMonth;

20 The this parameter Every (non static) method has an implicit, pre- defined parameter called this. Every (non static) method has an implicit, pre- defined parameter called this. this can be used to refer to class variables or methods. this can be used to refer to class variables or methods. this is a keyword. this is a keyword. this refers to the current object instance. this refers to the current object instance.

21 When do we use this? When a function parameter (or local variable) name masks a class variable name. 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 our convention avoids this situation in most cases. So you shouldn’t need to use this very often. But it doesn’t hurt: But it doesn’t hurt: public int getDay ( ) { return this.mDay; }

22 When we must use this int where = 12; public void foo ( int where ) { where = 92; } Did the class variable called where change to 92?

23 When we must use this int where = 12; public void foo ( int where ) { this.where = 92; where = 7; }

24 Determine if two dates are equal Recall what happened when we used == to test if two strings are equal. Recall what happened when we used == 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.

25 Determine if two dates are equal public boolean equals ( Date other ) { return ( mDay == other.mDay && mMonth.equals( other.mMonth ) && mYear==mYear ); }

26 Function to determine if one date precedes another date Note that December 31, 1970 precedes January 1, 1971. Note that December 31, 1970 precedes January 1, 1971. Note that December 31, 1971 does not precede January 1, 1970. Note that December 31, 1971 does not precede January 1, 1970. Note that January 1, 1970 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. Write a function to determine if one date precedes another date.

27 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!

28 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); }

29 Testing Use a separate program called a driver to test the classes that you develop. Use a separate program called a driver to test the classes that you develop. Drivers typically have a main. Your classes should not. Drivers typically have a main. Your classes should not.


Download ppt "Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in."

Similar presentations


Ads by Google