collaborative classes collaborative classes
play

Collaborative classes Collaborative classes Robots Learning to - PowerPoint PPT Presentation

Collaborative classes Collaborative classes Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker chapter 8 h 8 Announcements (Oct 24) Announcements (Oct 24) Quiz6 Quiz6 Reading for next Monday:


  1. Collaborative classes Collaborative classes Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker chapter 8 h 8

  2. Announcements (Oct 24) Announcements (Oct 24) • Quiz6 Quiz6 • Reading for next Monday: Ch 9 • Program#6 out today • Program#6 out today • Exam#2 scheduled for Nov 9 th (Wed 8PM) • Check program#4 score • Check program#4 score

  3. Make this static… Practice Using Class Variables Practice Using Class Variables

  4. Class Variables Class Variables • Some variables apply across all objects of Some variables apply across all objects of a class. • Consider the interest rate on savings • Consider the interest rate on savings accounts. • Consider assigning consecutive id C id i i ti id numbers to objects. • Class variables are RARELY needed.

  5. Class Methods Class Methods • Some methods don’t refer to instance Some methods don t refer to instance variables. • They may operate only on class variables • They may operate only on class variables. • They may simply operate based on their parameters. t • These methods are called by class name instead of object.

  6. • Write a program that finds the smallest of Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read end when a sentinel value of 999 is read. Do not count –999 as one of the integers to consider to consider.

  7. Problem 3 Problem 3 • Write a program that sums a sequence of Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values read specifies the number of values remaining to be entered. Your program should read only one value at a time A should read only one value at a time. A typical input sequence might be 5 100 200 300 400 500

  8. Problem 4 Problem 4 • Write a program that finds the smallest of Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read end when a sentinel value of 999 is read. Do not count –999 as one of the integers to consider to consider.

  9. Chapter 8: Objectives Chapter 8: Objectives • Writing classes that have objects as Writing classes that have objects as instance variables • Improved class diagrams Improved class diagrams • Understanding reference variables vs primitive variables primitive variables • Understanding aliases • Comparing objects • Comparing objects • Exceptions

  10. Consider This Class Diagram Consider This Class Diagram Person -String name -String mother -String father -int birthYr -int birthMth i t bi thMth -int birthDay -int deathYr -int deathMth -int deathDay +Person(String name, String mom, String dad, int bYr, int bMth, int bDay) +int getAge() +String getMother() +String getFather() +String getBirthDate() + +void setDeathDate(int dYr, int dMth, int dDay) id tD thD t (i t dY i t dMth i t dD ) +String toString()

  11. A Date Class A Date Class • Create a Date class to hold integers to Create a Date class to hold integers to represent the – day day – month – year year • Why not use String data?

  12. Update the Person Class Update the Person Class Person Date - name:String -int day y - mother:String -int month Has-A - father:String -int year 1..2 - dateOfBirth:Date + Date(int year, int month, int day) day) - dateOfDeath:Date d t OfD th D t + getYear():int + Person(String name, String mom, + getMonth():int String dad, int bYr, int bMth, int bDay) + getDay():int + Person(String name, String mom, + getDifference(Date aDate):int + getDifference(Date aDate):int String dad, Date birth) + compareTo(Date aDate):int + getAge():int + equals(Date aDate):boolean + getMother():String + toString():String + getFather():String + getFather():String + getBirthDate():Date + setDeathDate(Date death):void + toString():String

  13. Dates Dates • Programming dates typically include both Programming dates typically include both the date and time • Java provides a number of classes to • Java provides a number of classes to handle dates – Date – technically deprecated – pre 2000 D t t h i ll d t d 2000 – GregorianCalendar • These and others are somewhat difficult to use and can be very confusing to new programmers

  14. Problems with the Date Classes Problems with the Date Classes • All data is stored as integer values All data is stored as integer values – Jan is stored as zero, Feb as 1, etc – The day of the week is also stored with Sun The day of the week is also stored with Sun stored as zero, Mon as 1, etc – However, the date is stored as 1 – 31 – However the date is stored as 1 – 31 – In the Date class the year is expected to be entered as a 2 digit year and when stored entered as a 2 digit year and when stored 1900 is added to it. So passing 06 stores 1906 and to store 2006 you must pass 106. y p Passing 2006 results in 3906!

  15. Printing Date Objects Printing Date Objects • The toString method The toString method • A Date object declared as Date aDate = new Date(59, 5, 30); Date aDate new Date(59, 5, 30); (intending to store May 30,1959) yields Tue Jun 30 00:00:00 CDT 1959 • Using a 4-digit year Date aDate = new Date(1959, 5, 30); ( ) Tue Jun 30 00:00:00 CDT 3859 • So we must remember to reduce the month by 1 and pass (59, 4, 30)

  16. Printing GregorianCalendar Printing GregorianCalendar • The toString method for the GregorianCalendar is used for debugging purposes only purposes only – not very useful not very useful • GregorianCalendar gregDate = new GregorianCalendar(1959, 5, 30); (intending to store May 30,1959) yields java util GregorianCalendar[time=? areFieldsSet=false areAllFieldsSet=fals java.util.GregorianCalendar[time ?,areFieldsSet false,areAllFieldsSet fals e,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset =- 21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=j ava.util.SimpleTimeZone[id=America/Chicago,offset=- 21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3, 21600000 d tS i 3600000 D li ht t t tY 0 t tM d 3 startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTime Mode=0,endMode=2,endMonth=9,endDay=- 1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek =1 minimalDaysInFirstWeek=1 ERA=? YEAR=1959 MONTH=5 WEEK OF 1,minimalDaysInFirstWeek 1,ERA ?,YEAR 1959,MONTH 5,WEEK_OF_ YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=30,DAY_OF_YEAR=?, DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=?,HOUR=?,H OUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFS ET=?,DST_OFFSET=?]

  17. Extracting a useful GregorianCalendar date Extracting a useful GregorianCalendar date • this .gregDate.get(Calendar. YEAR ) + "/" + this gregDate get(Calendar MONTH ) + "/" + this .gregDate.get(Calendar. MONTH ) + / + this .gregDate.get(Calendar. DAY_OF_MONTH ) • However, we must remember that the month is off by one – so if 5 is stored as the month off by one so if 5 is stored as the month number, we are talking about June not May

  18. Month Issues Month Issues • Suppose we want to store Jan 30 so we Suppose we want to store Jan 30, so we declare a new GregorianCalendar GregorianCalendar gregDate = new GregorianCalendar gregDate new GregorianCalendar(2011, 1, 30); • We would get this date returned would actually March 2 2011 would actually March 2, 2011 • Why?

  19. Becker’s DateTime Becker s DateTime • A Becker class that simplifies both the A Becker class that simplifies both the Date and GregorianCalendar classes • Declaring a becker DateTime object as • Declaring a becker.DateTime object as DateTime beckerDate = new DateTime(2012, 1, 30); yields 2012/1/30 yields 2012/1/30 • Naturally you must have a path to the N t ll t h th t th becker.jar file and you must use i import becker.util.DateTime; t b k til D t Ti

  20. Improved Class Diagram Improved Class Diagram Person becker.DateTime - name:String - year:int - mother:String - month:int - father:String - day:int Has-A - dateOfBirth:DateTime - hours:int 1 .. 2 1 2 - dateOfDeath:DateTime d t OfD th D t Ti - minutes:int i t i t + Person(String name, String mom, - seconds:int String dad, DateTime birth) + DateTime() + getAge():int + DateTime(int yr, int mth, int + getMother():String + getMother():String day) + getFather():String + DateTime(int yr, int mth, int + getBirthDate():DateTime day, int hr, int min, int sec) + setDeathDate(DateTime death):void + addYears(int howMany):void + addYears(int howMany):void + toString():String + addMonths(int howMany):void + addDays(int howMany):void + daysUntil(DateTime date):long + equals(Object obj):boolean + toString():String ...

  21. “is a” vs “has a” is a vs has a • “is a” refers to inheritance relationships p – A RobotSE “is a” Robot • “has a” refers to collaborative classes – A Person object “has a” DateTime object as an instance variable instance variable • “has a” relationships can imply several things – “has exactly one” y – “has at least one” – “has multiple” – “might have” “ i ht h ” • Composition or cardinality

Recommend


More recommend