Comp201: Principles of Object-Oriented Programming I
Spring 2006 -- Lec03: Function Abstraction using Methods   


Modeling a Person

For the first few years of his life, Peter did not have a clue what birthdays were, let alone his own birth date. He was incapable of responding to your inquiry on his birthday. It was his parents who planned for his elaborate birthday parties months in advance. We can think of Peter then as a rather "dumb" person with very little intelligence and capability. Now Peter is a college student. There is a piece of memory in his brain that stores his birth date: it's September 12, 1985! Peter is now a rather smart person. He can figure out how many more months till his next birthday and e-mail his wish list two months before his birth day. How do we model a "smart" person like Peter? Modeling such a person entails modeling
  • a birth date and
  • the computation of the number of months till the next birth day given the current month.
A birth date consists of a month, a day and a year. Each of these data can be represented by an integer, which in Java is called a number of type int. As in the computation of the area of a rectangle, the computation of the number of months till the next birth day given the current month can be represented as a method of some class. What we will do in this case that is different from the area calculator is we will lump both the data (i.e. the birth date) and the computation involving the birth date into one class. The grouping of data and computations on the data into one class is called encapsulation. Below is the Java code modeling an intelligent person who knows how to calculate the number of months before his/her next birth day. The line numbers shown are there for easy referencing and are not part of the code.
1  class Person {
2     /**
3     * Data fields: 
4     */
5     int _bDay;   // birth day
6     int _bMonth; // birth month; for example, 3 means March.
7     int _bYear;  // birth year    
    
8     /**
9     * Uses "modulo" arithmetic to compute the number of months till the next 
10    * birth day given the current month.
11    * @param currentMonth an int representing the current month.
12    */
13    int nMonthTillBD(int currentMonth) {
14        return (_bMonth - currentMonth + 12) % 12;
15    }
16 }
We now explain what the above Java code means.
  • line 1 defines a class called Person. The opening curly brace at the end of the line and the matching closing brace on line 16 delimit the contents of class Person.
  • lines 2-54 are comments. Everything between /* and */ are ignored by the compiler.
  • lines 5-7 define three integer variables. These variables are called fields of the class. Only code inside of the class can access them. Each field is followed by a comment delimited by // and the end-of-line. So there two ways to comment code in Java: start with /* and end with */ or start with // and end with the end-of-line.
  • lines 8-12 are comments.
  • line 11 is a special format for documenting the parameters of a method. This format is called the javadoc format. We will learn more about javadoc in another module.
  • lines 13-15 constitute the definition of a method in class Person.
  • line 14 is the formula for computing the number of months before the next birthday using the remainder operator "%". x % y gives the remainder of the integer division between the dividend x and the divisor y.
 

 


Last Revised Thursday, 03-Jun-2010 09:50:14 CDT

©2006 Stephen Wong and Dung Nguyen