You are here

Modeling a Person

10 March, 2015 - 12:00
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

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.

public class Person {    /**     * All data fields are private in order to prevent code outside of this     * class to access them.     */     private int bDay; // birth day     private int bMonth; // birth month; for example, 3 means March.     private int bYear; // birth year         * Constructor: a special code used to initialize the fields of the class.     * The only way to instantiate a Person object is to call new on the constructor.     * For example: new Person(28, 2, 1945) will create a Person object with     * birth date February 28, 1945.     */    public Person(int day, int month, int year) {        _bDay = day;        _bMonth = month;        _bYear = year;     }     /**    * Uses "modulo" arithmetic to compute the number of months till the next    * birth day given the current month.     * @param currentMonth an int representing the current month.     */     public int nMonthTillBD(int currentMonth) {         return (_bMonth -  currentMonth + 12) % 12;     } } 

Download the above code:

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 28 delimit the contents of class Person. The key word public is called an accessspecifier and means all Java code in the system can reference this class.
  • lines 2-5 are comments. Everything between /* and */ are ingored by the compiler.
  • lines 6-8 define three integer variables. These variables are called fields of the class. The key word private is another access specifier that prevents access by code outside 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 9-14 are comments.
  • lines 15-19 constitute what is called a constructor. It is used to initialize the fields of the class to some particular values. The name of the constructor should spell exactly like the class name. Here it is public, meaning it can be called by code outside of the class Person via the operator new. For example, new Person(28, 2, 1945) will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear = 1945.
  • lines 20-24are comments.
  • line 23 is a special format for documenting the parameters of a metod. This format is called the javadoc format. We will learn more about javadoc in another module.
  • lines 25-27 constitute the definition of a method in class Person.
  • line 26 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.