Fundamental Java Methods

Similar documents
Mutable and Immutable Objects

Review of O-O Concepts and UML. James Brucker

Java Classes, Inheritance, and Interfaces

Super-Classes and sub-classes

Interfaces. James Brucker

Abstract Classes and Interfaces

For this section, we will implement a class with only non-static features, that represents a rectangle

Inheritance (Part 5) Odds and ends

Domain-Driven Design Activity

COE318 Lecture Notes Week 9 (Oct 31, 2011)

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Collections Algorithms

Distributed Systems Recitation 1. Tamim Jabban

Not overriding equals

Lexical Ordering and Sorting. These slides refer to interfaces.

Aggregation and Composition. [notes Chapter 4]

Distributed Systems Recitation 1. Tamim Jabban

Polymorphism. return a.doublevalue() + b.doublevalue();

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

The class Object. Lecture CS1122 Summer 2008

Introduction to Inheritance

Class design guidelines. Most of this material comes from Horstmann, Cay: Object-Oriented Design & Patterns (chapter 3)

INTERFACE WHY INTERFACE

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Chapter 11: Collections and Maps

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Creating an Immutable Class. Based on slides by Prof. Burton Ma

INHERITANCE. Spring 2019

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

USAL1J: Java Collections. S. Rosmorduc

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

Methods Common to all Classes

Objects and Iterators

Classes Classes 2 / 36

Comparing Objects 3/28/16

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS108, Stanford Handout #12. Inheritance 2

Compsci 201 Hashing. Jeff Forbes February 7, /7/18 CompSci 201, Spring 2018, Hashiing

CompuScholar, Inc. 9th - 12th grades

Chapter 13 Abstract Classes and Interfaces

Java Fundamentals (II)

Announcements/Follow-ups

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

1 Shyam sir JAVA Notes

CS 231 Data Structures and Algorithms, Fall 2016

JAVA MOCK TEST JAVA MOCK TEST II

TeenCoder : Java Programming (ISBN )

CSE331 Winter 2014, Midterm Examination February 12, 2014

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

Equality. Michael Ernst. CSE 331 University of Washington

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

CMSC 132: Object-Oriented Programming II. Effective Java. Department of Computer Science University of Maryland, College Park

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

MAT 3670: Lab 3 Bits, Data Types, and Operations

Chapter 5 - A Simple Dynamic Linked List (Holding Strings)

Comparing Objects 3/14/16

Classes, interfaces, & documentation. Review of basic building blocks

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

public static boolean isoutside(int min, int max, int value)

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

Chapter 13 Abstract Classes and Interfaces

Java Language Features

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

A simple map: Hashtable

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Introduction to Programming Using Java (98-388)

CS11 Java. Winter Lecture 8

Basic Object-Oriented Concepts. 5-Oct-17

Java An Introduction. Outline. Introduction

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Abstract Classes, Exceptions

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

COM1020/COM6101: Further Java Programming

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

CLASS DESIGN. Objectives MODULE 4

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CMSC 341. Nilanjan Banerjee

Media Computation. Lecture 15.2, December 3, 2008 Steve Harrison

Handout 7. Defining Classes part 1. Instance variables and instance methods.

COP 3330 Final Exam Review

CS2110: Software Development Methods. Maps and Sets in Java

CS 3410 Ch 20 Hash Tables

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Learn more about our research, discover data science, and find other great resources at:

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

More about inheritance

Collections, Maps and Generics

Transcription:

Object-oriented Programming Fundamental Java Methods Fundamental Java Methods These methods are frequently needed in Java classes. You can find a discussion of each one in Java textbooks, such as Big Java. You should practice until you can write each of these methods without any effort. T = the name of a type or class. String getname( ) boolean ison( ) accessor method for a String attribute name accessor for a boolean attribute (on) begins with "is" not "get" void setname( String name) void seton( boolean on) String tostring() @Override boolean equals(object obj) int hashcode( ) mutator ("setter") method: set the value of name. A mutator method can be trivial like: public void setname( String name ) { this.name = name; A mutator can perform data validation and convert a synthetic attribute into actual attributes. For example, to require that a Person's name is not null or empty: public void setname( String name ) { if ( name == null ) throw new IllegalArgumentException("cannot be null"); if (name.isempty() ) throw new IllegalArgumentException(... ); this.name = name; Return a string representation of the object suitable for printing. This method does not print anything on System.out. Example: public String tostring( ) { return name+" "+id; Test if two objects are equal in value. The parameter for this method should always be Object, not the class of this object. Use @Override (optional) to detect typing errors. There is a 4-part pattern for writing equals: public boolean equals(object obj) { // (1) verify that obj is not null if (obj == null) return false; // (2) test that obj is the same class as "this" object if ( obj.getclass()!= this.getclass() ) return false; // (3) cast obj to this class's type Person other = (Person) obj; // (4) compare whatever values determine "equal" if ( name.equalsignorecase( other.name ) ) return true; return false; hashcode( ) is used by HashSet, HashMap, and a other classes to decide where to store the object in a collection. Page 1

If two objects are "equal", then the hashcode should be same. If a.equals( b ) then a.hashcode() == b.hashcode( ). For a Set or Map, the hashcode determines which "bin" an object is put in, therefore, the hashcode should be something that normally doesn't change. See textbook for how to choose a good hash code. int compareto(t obj) Object clone( ) == or == T clone( ) Defines an ordering of objects. Used for sort and binary search methods in java.util.arrays and java.util.collections. The semantics of this method are defined by the Comparable interface. See example below. a.compareto( b ) < 0 if a is "less than" or "before" b a.compareto( b ) = 0 if a has same order as b a.compareto( b ) > 0 if a is "greater than" or "after" b Any positive or negative value can be returned by compareto. Only the sign of the return value is important (+, -, or 0). To see this, try some Strings: "ant".compareto("dog") Be careful of null parameter values, esp. if the collection you want to sort may contain nulls. Make an identical copy of an object. If you implement this, then declare that the class implements Cloneable. Otherwise, calling clone( ) will throw CloneNotSupportedException. Usually clone performs a deep copy, Horstmann 7.4. Sorting and Comparable The compareto method is used for sorting and searching. Your class must declare that it has a compareto( ) method by implementing the Comparable interface. If your class has a compareto method, then include this in your Java class: /** Person objects can be sorted using compareto */ public class Person implements Comparable<Person> { /** order Person objects by name. */ public int compareto(person other) { if ( other == null ) return -1; // this calls compareto of the String class, ignoring case of letters int comp = this.name.comparetoignorecase( other.name ); return comp; If your class already has a compareto but you want to order objects some other way, implement a java.util.comparator which has a single method compare( a, b ). Its like an externalized compareto. Fundamental Java Methods Page 2

Example public class Person implements Cloneable, Comparable<Person> { private String name; // java.util.date is mutable. In Java 8 use LocalDate instead. private Date birthday; /** constructor initializes the attributes using parameters */ public Person(String name, Date birthday) { this.name = name; // copy the (mutable) Date parameter to preserve encapsulation this.birthday = new Date( birthday ); /** accessor method for name (immutable) returns the name */ public String getname( ) { return name; /** Accessor for birthday. The caller should not change this. */ public Date getbirthday( ) { return this.birthday; // or: return (Date)(birthday.clone()) /** Change the person's birthday. * @param birthday is birthday to assign to this person */ public void setbirthday( Date birthday ) { // don't allow birthday to be null. if ( birthday == null ) throw new IllegalArgumentException("must not be null"); this.birthday = new Date( birthday ); // because Date is mutable /** Two persons are equal if name *and* birthday are same. */ public boolean equals( Object obj ) { // This is the 4-step template for equals. You should know it. if ( obj == null ) return false; if ( this == obj ) return true; // this test is optional if ( this.getclass()!= obj.getclass() ) return false; // cast obj to Person so we can get its attributes Person other = (Person) obj; // now test equality any way to want. return this.name.equals( other.name ) && this.birthday.equals( other.birthday ); /** hashcode should be consistent with equals */ public int hashcode( ) { int hash = 0; if (name!= null) hash += name.hashcode(); // String hashcode if (birthday!= null) hash += 37 * birthday.hashcode( ); return hash; /** compare people by name. Used for sorting. */ public int compareto( Person other ) { if ( other == null ) return -1; // this uses comparetoignorecase of the String class // this assumes that a Person's name is never null. Fundamental Java Methods Page 3

return name.comparetoignorecase( other.name ); /** Clone makes a deep copy of an object. * It returns Object for compatibility with superclass, * but it is also legal to declare return type as Person. * @return a copy of this Person as a new object. */ public Object clone( ) { Person clone = (Person)super.clone( ); // clone parent type first clone.name = name; // String is immutable, so sharing is OK clone.birthday = (Date)birthday.clone(); // clone mutable attribute return clone; Fundamental Java Methods Page 4

Exercises 1) Date objects are mutable (can be changed). Since getbirthday() returns a reference to the Person's birthday, we can use it to surreptitiously change a person's birthday!! // Nok is born on 1 Jan 2000 ("Jan" = month 0) Person nok = new Person("Nok", new Date(100, 0, 1) ); System.out.println("Nok = " + nok); // get Nok's birthday. Date date = nok.getbirthday( ); System.out.printf( "Nok was born on %tf\n", date); // change the date object date.setmonth( Calendar.JUNE ); date.setyear( 99 ); // this means 1999 // Did Nok's birthday change? System.out.println( "Nok = " + nok); System.out.printf( "Nok was born on %tf\n", nok.getbirthday() ); If protecting an object's attributes is important, getbirthday() should return a copy of the birthday using birthday.clone(). The downside of returning a copy is that it creates a new object each time. 2) Java 8 adds several date and time classes in the package java.time. One of these classes is LocalDate which holds a date. LocalDate is immutable, so we don't need to worry about its value being modified. 2.1 Modify the code for Person to use LocalDate for birthday, and simplify it. Since LocalDate is immutable you don't need to make copies of it. Actually run the code to verify it works. 2.2 Suppose some application is already using the original Person class. We can't change the Person constructor because that will break their code. Instead, (a) modify the original constructor to internally copy the java.util.date parameter to a java.time.localdate attribute. (b) add a new constructor that accepts a LocalDate parameter. 3) Create an array of Person objects and sort them using Arrays.sort( ). Person [ ] people = new Person [4]; // create people using the original constructor // the parameters may not be what you'd expect! // 100 means the year 2000, 0 means January! people[0] = new Person("Nok", new Date(101, 0, 1)); // What kind of thing is Calendar.MARCH? people[1] = new Person("Maew", new Date(99, Calendar.MARCH, 1)); // use the new constructor (Exercise 2) with LocalDate people[2] = new Person( "Ling", LocalDate.of(2001,1,30) ); // two persons named "Nok" to test compare by birthday people[3] = new Person( "Nok", new Date(100, 0, 15) ); System.out.println("Before sorting:"); // indexed "for" loop over the array for(int k=0; k<people.length; k++) System.out.println( people[k] ); // Sort the people. This uses person.compareto to determine order. java.util.arrays.sort( people ); Fundamental Java Methods Page 5

System.out.println("\n\nAfter sorting:"); // a "for-each" loop that iterates over the same array for( Person p : people ) System.out.println( p ); 4) (Custom sorting) We want to sort people by birthday using only the month and day! But Person already has a compareto method that orders Person objects by name. No problem! Arrays.sort has another form like this: Array.sort( T [] array, Comparator<T> comparator ); A Comparator is an object that compares two other objects -- for example, to compare two Person Comparator is an interface in java.util. To write a Comparator you create a new class with a single method named compare. The compare method compares 2 parameters and returns an integer, similar to the way compareto does, except compare uses parameters instead of "this". To write a Comparator you must implement this method: compare( Person p1, Person p2 ) The Comparator.compare method returns a result of the comparison like this: compare(person p1, Person p2) < 0 if p1 should be "before" p2 = 0 if p1 and p2 have same order > 0 if p1 should be "after" p2 (a) Write a BirthdayComparator class that implements Comparator<Person> and write the compare method to order the objects by month and day of birthday. import java.util.comparator; public class BirthdayComparator implements Comparator<Person> { public int compare( Person person1, Person person2 ) { //TODO check for person1 == null or person2 == null Date date1 = person1.getbirthday(); Date date2 = person2.getbirthday(); // compare months first. if same then compare day. int comp = date1.getmonth() - date2.getmonth(); if (comp == 0) comp = date1.getdate() - date2.getdate(); return comp; (b) Test your BirthdayComparator by creating an instance of it and sort an array of Person. Comparator<Person> comp = new BirthdayComparator( ); Arrays.sort( people, comp ); // print the array System.out.println("People sorted by birthday"); for(person p : people ) System.out.println( p ); Fundamental Java Methods Page 6