Download presentation
Presentation is loading. Please wait.
Published byTeresa Cain Modified over 6 years ago
1
Comparing Objects Phil Tayco San Jose City College Slide version 1.1
Updated Sep. 30, 2018
2
Comparing Objects Comparing objects is another common behavior in complex programs As with learning other OO concepts, we start with reviewing the process with simple data types and variables int x, y; x = 10; y = 10; if (x == y) System.out.println(“Equal variables”); Clearly this will result in printing Equal variables What happens if we did the exact same thing with objects?
3
Comparing Objects This will not result in printing Equal dates
Date d1, d2; d1 = new Date(10, 1, 2018); d2 = new Date(10, 1, 2018); if (d1 == d2) System.out.println(“Equal dates”); This will not result in printing Equal dates It is important to see why this happens from looking at memory behind the scenes Recall that when d1 and d2 are instantiated, they are individually assigned locations in memory Even though the property values within the objects are the same, their locations are different
4
Comparing Objects The “==“ operation can only compare one value that is at the given variable locations Notice that with objects, the immediate values compared the memory addresses of d1 and d2 – this results in comparing these 2 values as not equal What we want to happen is for the property values of these objects to be compared Since classes are complex data types, we have to define how that comparison occurs d1 0x4412ff20 d2 0x17dc7248 10 10 1 1 2018 2018
5
Comparing Objects Date d1, d2; d1 = new Date(10, 1, 2018); d2 = new Date(10, 1, 2018); if (d1.equals(d2)) System.out.println(“Equal dates”); Here, we are defining a method that acts like the “==“ operation The method is defined in the Date class which means we can code what equality means within the class function as appropriate It is technically a predication function and usually takes its own class as an input parameter (i.e. the Date class “equals” method takes a Date object as input) Now we can define the “equals” method
6
Date/Time Observations
Code changes in main, Date and Time are loosely dependent on each other while the behavior is highly related to where they reside Time class methods deal specifically with Time concepts Date class methods deal specifically with Date concepts Even when Date uses composition to include Time as a property, Date methods still focus on Date concepts and call on Time concepts as needed Moreover, Date can choose not handle “hour”, “minute” and “second” specific values (ex. “equals” and Date constructors using Time objects) main was not impacted when Time was added to the Date class because the original Date functions used did not change – all updates to Date either overloaded functions or changed code behavior within them main has the option to use new functions with Time objects – it should not be required to change its existing code because of the update
7
Programming Exercise 3 Modify your House and Address classes as follows: Add copy constructors to House and Address Add “equals” methods to House and Address Update your main test program to test these methods appropriately Create a ZipCode class which has a 5-digit prefix and 4-digit suffix The 4-digit suffix is considered optional (it may have a value or it may not) If a zip code has no suffix, it should not be displayed – for example, is a zip code with a prefix and suffix is a zip code with no suffix Comparing zip codes is as follows: If the 2 zip codes both have suffixes, they are equal if both the prefix and suffix between the two are the same If one or both of the zip codes do not have a suffix, then they are equal only if the prefixes are the same
8
Programming Exercise 3 All normal methods should be in place for the ZipCode class 3 constructors (default, prefix/suffix and copy) 4 set/get methods total equals and toString methods Change the zip code property in your Address class to your new ZipCode class All method signatures for Address that involved zip code before you the change are not to be updated and the code from in the main test program from exercise 2 must not change Add/overload methods and constructors with the new ZipCode class as appropriate and then add to the main test program code to test them
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.