Archive for September 25th, 2007

Composition – Java

September 25, 2007

Emp.Java 

public class Emp

{

            private String firstName;

            private String lastName;

            private Date birthDate;

            private Date hireDate;

           

            //constructor to initialize name, birth date, and hire date

            public Emp (String first, String last, Date dateOfBirth, Date dateOfHire)

            {

                        firstName = first;

                        lastName = last;

                        birthDate = dateOfBirth;

                        hireDate = dateOfHire;

            }

           

            //convert Emp to String format

            public String toString()

            {

                        return String.format(“%s, %s Hired: %s Birthday: %s”,

                                    lastName, firstName, hireDate, birthDate);

            }

}

  Date.Java 

//Date class declaration

public class Date

{

            private int month;          //1-12

            private int day;  //1-31 based on month

            private int year; //any year

           

            //constructor: call checkMonth to confirm proper value for month

            //call checkDay to confirm proper value for day

            public Date(int theMonth, int theDay, int theYear)

            {

                        month = checkMonth(theMonth);          //validate month

                        year = theYear;                                    //could validate year

                        day = checkDay(theDay);                     //validate day

                        System.out.printf(“Date object constructor for date %s\n”, this);

            }

           

 

            //utility method to confirm proper month value

            private int checkMonth (int testMonth)

            {

                        if (testMonth>0 && testMonth <=12)   //validate month

                                    return testMonth;

                                    else                                                      //month is invalid

                                    {

                                    System.out.printf(“Invalid month (%d) set to 1.”, testMonth);

                                    return 1;           //maintain object in consistent state

                                    }

            }

           

            //utility method to confirm proper day value based on month and year

            private int checkDay (int testDay)

            {         

                        //0 is ignored since daysPerMonth[0]=0 represents 0 months

                        //Month starts on the 2nd element after 0 (from 1-12)

                        int daysPerMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

                       

                        //check if day in range for month

                        if (testDay >= 1 && testDay <= daysPerMonth[month])

                                    return testDay;

                                   

                        //check for leap year

                        if (month == 2 && testDay == 29 && (year % 400==0 ||

                                    (year % 4 == 0 && year % 100 != 0)))

                                    return testDay;

                                   

                        System.out.printf(“Invalid day (%d) set to 1.”, testDay);

                        return 1;           //maintain object in consistent state

            }

           

            //return a String of the form month/day/year

            //automatically invoked even without calling this method

            public String toString()

            {

                        return String.format(“%d/%d/%d”, month, day, year);

            }

}

      EmpTest.Java 

//composition demonstration

public class EmpTest

{

            public static void main (String args[])

            {

                        Date birth = new Date(7,0,1980);

                        Date hire = new Date(9,5,2007);

           

                        Emp employee = new Emp(“Ana”, “Santos”, birth, hire);

                       

                        System.out.println(employee);

            }

}


Design a site like this with WordPress.com
Get started