5.7 A Complete Program


5.7 A Complete Program

This section presents the complete definition of class Person. This class includes two initializer functions (also called constructors). Two objects are created from function main, one created with the default initializer function, and the other created with the complete initializer function. The KJP code for class Person is as follows:

       description         This is the complete definition of class Person.         The attributes are age and name.         */         class Person is           private           // attributes           variables     // variable data declarations               integer age               string obj_name           // no private methods in this class           public           description             This is the default initializer method.             */           function initializer is             begin               set age = 21               set obj_name = "None"           endfun initializer           //           description             This is a complete initializer function, it             sets the attributes to the values given on             object creation.             */           function initializer parameters integer iage,                           string iname is           begin             set age = iage             set obj_name = iname           endfun initializer           //           description               This accessor function returns the name of               the object.               */           function get_name of type string is             begin               return obj_name           endfun get_name         //         description             This mutator function changes the name of             the object to the name in variable new_name.             This function has type void.             */         function change_name parameters                                     string new_name is          begin            set obj_name = new_name         endfun change_name         //         description            This accessor function returns the age of            the Person object.   */         function get_age of type integer is           begin            return age         endfun get_age         //         description            This mutator function increases the age of            the person object when called.            */         function increase_age is           begin             increment age         endfun increase_age       endclass Person 

On the CD

The complete KJP implementation of class Person is stored in the file Person.kpl.

The instructions in function main start and control the execution of the entire program. The following KJP code (in file Mperson.kpl) implements class Mperson that contains function main for the creation and manipulation of two objects of class Person.

       description            This is the main class in the program. It            creates objects of class Person and then            manipulates these objects.  */       class Mperson is         public         description             This is the control function.  */         function main is          variables             // data declarations             integer lage             string lname          objects             object person_a of class Person             object person_b of class Person          begin            display                "Creating two objects of class Person"            create person_a of class Person            create person_b of class Person using 37,                                           "James Bond"            call change_name of person_a using                                            "Agent 008"            set lname = call get_name of person_a            set lage = call get_age of person_a            display "First object: ", lname,                                        " age: ", lage            set lname = call get_name of person_b            set lage = call get_age of person_b            display "Second object: ", lname,                                        " age: ", lage            call change_name of person_a using                                            "Agent 009"            set lage = call get_age of person_a            set lname = call get_name of person_a            display "First object: ", lname,                                        " age: ", lage         endfun main       endclass Mperson 

On the CD

The two KJP classes discussed are stored in the files Person.kpl and Mperson.kpl; the corresponding Java classes are stored in the files Person.java and Mperson.java.

After translating and compiling both classes (Person and Mperson) of the problem, the execution of class Mperson gives the following output:

       Creating two objects of class Person       First object: Agent 008 age: 21       Second object: James Bond age: 37       First object: Agent 009 age: 21 

The Java implementation of class Person, generated by the KJP translator from Person.kpl, is the following:

       //  KJP v 1.1 File: Person.java, Sun Dec 01                                          18:25:45 2002       /**         This is the complete definition of class         Person. The attributes are age and name.         */       public  class Person   {         // attributes         // variable data declarations         private int  age;         private String  obj_name;         /**           This is the default initializer method.           */         public Person() {           age = 21;           obj_name =  "None";       } // end constructor       /**         This is a complete initializer function, it         sets the attributes to the values given on         object creation.         */       public Person(int iage, String iname) {         age =  iage;         obj_name =  iname;       } // end constructor       /**           This accessor function returns the name of           the object.    */       public String  get_name() {         return obj_name;       }  // end get_name       /**           This mutator function changes the name of           the object to the name in variable new_name.           This function has type void.           */       public void  change_name(String  new_name) {         obj_name =  new_name;       }  // end change_name       /**          This accessor function returns the age of          the Person object.   */       public int  get_age() {         return age;       }  // end get_age       /**          This mutator function increases the age of          the person when called.          */       public void increase_age() {         age++;       }  // end increase_age       } // end Person 

On the CD

The Java implementation of class Mperson, generated by the KJP translator from Mperson.kpl, is stored in the file Mperson.java, and the code is as follows:

       // KJP v 1.1 File: Mperson.java, Sun Dec 01                                          19:00:02 2002       /**            This is the main class in the program. It            creates objects of class Person and then            manipulates the objects.     */       public class Mperson {         /**            This is the control function.     */         public static void main(String[] args) {            // data declarations            int  lage;            String  lname;            Person person_a;            // body of function starts here            Person person_b;            System.out.println(                "Creating two objects of class Person");            person_a = new Person() ;            person_b = new Person(37, "James Bond");            person_a.change_name("Agent 008");            lname =  person_a.get_name();            lage =  person_a.get_age();            System.out.println("First object: "+ lname+                     " age:  "+ lage);            lname =  person_b.get_name();            lage =  person_b.get_age();            System.out.println("Second object: "+ lname+                     " age: "+ lage);            person_a.change_name("Agent 009");            lage = person_a.get_age();            lname = person_a.get_name();            System.out.println("First object: "+ lname+                     " age: "+ lage);         }   // end main       }  // end Mperson 




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net