Comparing Objects Phil Tayco San Jose City College Slide version 1.1

Slides:



Advertisements
Similar presentations
CS 211 Inheritance AAA.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Assigned “term” Prefix or Suffix ?. definition Origin or prefix/suffix Definition.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
The Object-Oriented Thought Process Chapter 03
Phil Tayco Slide version 1.0 Created Sep 10, 2017
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Java Programming: Guided Learning with Early Objects
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Phil Tayco Slide version 1.0 Created Nov 6, 2017
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Functions CIS 40 – Introduction to Programming in Python
Phil Tayco Slide version 1.0 Created Nov. 26, 2017
The dirty secrets of objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Composition Phil Tayco San Jose City College Slide version 1.2
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Phil Tayco Slide version 1.0 Created Oct 16, 2017
March 29th Odds & Ends CS 239.
Inheritance Basics Programming with Inheritance
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Lecture 22 Inheritance Richard Gesick.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Polymorphism Phil Tayco San Jose City College Slide version 1.1
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Java Programming Arrays
Java Programming Loops
Fundamental Error Handling
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Java Programming Function Introduction
Java Programming Control Structures Part 1
Computer Programming with JAVA
Lecture 9_1 George Koutsogiannakis
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Spring 2018 Final Project Phil Tayco San Jose City College
Classes and Objects Phil Tayco San Jose City College Slide version 1.1
Reference semantics, variables and names
In Class Exercises Phil Tayco Slide version 1.1 San Jose City College
COP 3330 Object-oriented Programming in C++
Java Programming Introduction
Inheritance Phil Tayco San Jose City College Slide version 1.2
Object Comparisons and Arrays
Java Programming Loops
Data Structures Unsorted Arrays
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Java Programming Function Introduction
CMPE212 – Reminders Assignment 2 due next Friday.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Creating and Using Classes
Introduction to Pointers
Phil Tayco San Jose City College Slide version 1.2 Updated Sep 9, 2019
Presentation transcript:

Comparing Objects Phil Tayco San Jose City College Slide version 1.1 Updated Sep. 30, 2018

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?

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

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

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

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

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, 95128-1642 is a zip code with a prefix and suffix. 95128 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

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