UserDefined Classes - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

UserDefined Classes

Description:

System.out.println('Invalid data, defaulting to 00:00:00'); set(0, 0, 0); Creation of Objects ... new TimeType( ) ; // default constructor invoked ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 26
Provided by: juliean
Category:

less

Transcript and Presenter's Notes

Title: UserDefined Classes


1
Chapter 7
  • User-Defined Classes
  • An Example
  • The TimeType Class

2
Requirements for TimeType
  • Data?
  • Operations?
  • Access?

3
Data Members
  • We will need to represent the hours, minutes, and
    seconds
  • What should the data type be?
  • What are the ranges of each?

4
Data Members
  • public class TimeType
  • private int hrs // 3 data members
  • private int mins
  • private int secs

5
Accessors
  • Now that we have data members, we need a way to
    retrieve their values
  • The client code might want to
  • TimeType t new TimeType( )
  • System.out.println(t.hrs) // is this legal?
  • System.out.println(t.getHrs()) // what about
    this?

6
Accessors
  • The easiest way is to create a member function
    (method) for each to retrieve them
  • getHrs
  • getMins
  • getSecs
  • Each function will return a value of data type
    _______
  • Any arguments?
  • What will the logic look like?

7
Accessors
  • public class TimeType
  • // accessor methods
  • public int getHrs( ) return hrs
  • public int getMins( ) return mins
  • public int getSecs( ) return secs
  • private int hrs // 3 data members
  • private int mins
  • private int secs

8
Modifiers
  • We also need a way to set the data members
    values
  • We could write a function to set each value
  • setHrs (int h)
  • setMins (int m)
  • setSecs (int s)
  • Or we can write a single one to set all three
  • set (int h, int m, int s)
  • Do you ever want to set just the hour or minute
    or would you always set all three at one time?

9
  • Client code might be
  • t.Set(10,10,10) // what would this be?
  • t.Set(50, 50, 50) // what should this do?

10
Modifiers
  • Do we need to do any validation of the input
    arguments?
  • What should the validations be?
  • What should we do if an input is invalid?

11
Modifier Function
  • public class TimeType
  • // modifier method with validations
  • public void set(int h, int m, int s)
  • // validate 0 lt initHrs lt 23 0 lt
    initMins lt 59 0 lt initSecs lt 59
  • if (( 0 lt h h lt 24) (0 lt m m lt 60)
  • (0 lt s s lt 60))
  • hrs h
  • mins m
  • secs s
  • else
  • System.out.println(Invalid data,
    defaulting to 000000)
  • set(0, 0, 0)

12
Creation of Objects
  • Now we need to be able to create objects of type
    TimeType
  • To do that we want to declare a constructor
    method to initialize the values in the object
    (set it to a valid state)
  • First, lets look at what the client code might
    look like to create an object

13
Invocation of Constructors
  • TimeType departureTime new TimeType( ) //
    default constructor invoked
  • TimeType movieTime new TimeType(19, 30, 0 )
    // parameterized constructor
  • departureTime movieTime

set
set
Private data hrs mins secs
Private data hrs mins secs
increment
increment
0 0 0
19 30 0
toString
toString
LessThan
LessThan
equals
equals
14
Implementation of Default Constructor
  • public class TimeType
  • public TimeType ( ) // Default Constructor
  • hrs 0 // hrs 0 mins 0
    secs 0
  • mins 0
  • secs 0 // alternate set(0, 0, 0)
  • What is the return type of a constructor?

15
Implementation of Another Constructor
  • public class TimeType
  • public TimeType ( int h, int m, int s ) //
    Constructor
  • // validate input 0 lt initHrs lt 23 0 lt
    initMins lt 59
  • // 0 lt initSecs lt 59
  • set(h, m, s) // why not just set values
    or repeat validation?

16
toString
  • Now we need a way to print our object
  • System.out.println(t)
  • // implicit invocation of toString method
  • How should we print the time?

17
Implementation of toString
  • public class TimeType
  • public String toString ( )
  • String holder
  • if (hrs lt 10)
  • holder 0
  • holder hrs
  • if (mins lt 10)
  • holder 0
  • holder (mins )
  • if (secs lt 10)
  • holder 0
  • holder secs
  • return holder

18
Increment Method
  • Now lets think about the increment method
  • time1.set(22, 0, 0)
  • time1.increment( )
  • System.out.println(time is now time1)
  • for (int i 0 i lt 125 i)
  • time1.increment( )
  • System.out.println(time is now time1)

19
Increment Method
  • What is the return type?
  • Any arguments?
  • What kind of logic do we have to think about?

20
Implementation of Increment
  • public class TimeType
  • public void increment ( )
  • secs
  • if (secs gt 59)
  • secs 0
  • mins
  • if (mins gt 59)
  • mins 0
  • hrs
  • if (hrs gt 23)
  • hrs 0

21
equals Function
  • We may want to be able to determine if two times
    are equal
  • time1.set(1, 0, 0)
  • time2.set(1, 0, 0)
  • if (time1.equals (time2))
  • System.out.println(time1 is equal to
    time2)
  • What does it mean for two times to be equal?
  • What is the return type?
  • What data type is the argument?

22
Implementation of equalTo
  • public class TimeType
  • public boolean equals (TimeType t )
  • if (hrs t. hrs mins t. mins
  • secs t.secs)
  • return true
  • else
  • return false

23
Object Relationships
  • Some use associations occur between objects of
    the same class (equals)
  • For example, we might add two Rational number
    objects together as follows
  • r3 r1.add(r2)
  • One object (r1) is executing the method and
    another (r2) is passed as a parameter

24
lessThan Function
  • We may want to be able to determine if one time
    is less than another time
  • time1.set(1, 0, 0)
  • time2.set(1, 0, 0)
  • if (time1.lessThan (time2))
  • System.out.println(time1 is less than
    time2)
  • What does it mean for one time to be less than
    another time?
  • What is the return type?
  • What data type is the argument?
  • Exercise You write this function!!

25
Subtract Method
  • Write the Java code for the subtract method for
    our timer application.
Write a Comment
User Comments (0)
About PowerShow.com