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

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

Implementing non-static features

The class Object. Lecture CS1122 Summer 2008

Not overriding equals

Chapter 6: Inheritance

Java Fundamentals (II)

Super-Classes and sub-classes

Chapter 11: Collections and Maps

Methods Common to all Classes

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Chapter 4 Defining Classes I

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

Classes as Blueprints: How to Define New Types of Objects

Inheritance (Part 5) Odds and ends

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

3.1 Class Declaration

ITI Introduction to Computing II

equals() in the class Object

ITI Introduction to Computing II

java.lang.object: Equality

Fundamental Java Methods

Introduction to Programming Using Java (98-388)

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Programming Languages and Techniques (CIS120)

Java Comparable interface

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

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Objects and Classes (1)

CSE wi Midterm Exam 2/8/18. Name UW ID #

52 Franck van Breugel and Hamzeh Roumani

The Liskov Substitution Principle

Chapter 6 Introduction to Defining Classes

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

COMP 250 Fall inheritance Nov. 17, 2017

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.


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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

INHERITANCE. Spring 2019

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Expected properties of equality

Elementary Symbol Tables

CSE wi Midterm Exam 2/8/18 Sample Solution

CSE 331 Software Design & Implementation

Charlie Garrod Michael Hilton

Programming Languages and Techniques (CIS120e)

CSC 102 Lecture Notes Week 2 Introduction to Incremental Development and Systematic Testing More Jav a Basics

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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


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

Software Engineering Design & Construction

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Abstract Classes and Interfaces

cs2010: algorithms and data structures

Chapter 13 Abstract Classes and Interfaces

Arrays. Chapter Arrays What is an Array?

Classes Classes 2 / 36

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Object-Oriented Programming in the Java language

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

Comments are almost like C++

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

ELEMENTARY SEARCH ALGORITHMS

Programming Languages and Techniques (CIS120)

Java: advanced object-oriented features

Subclassing for ADTs Implementation

CS 302: Introduction to Programming in Java. Lecture 15

3 ADT Implementation in Java

1 Shyam sir JAVA Notes

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

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

CSE 331 Software Design & Implementation

The Java Type System (continued)

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Computer Science 210: Data Structures

EECS 1001 and EECS 1030M, lab 01 conflict

Equality for Abstract Data Types

Programming Languages and Techniques (CIS120)

CS 251 Intermediate Programming Methods and Classes

CMSC131. Library Classes

Making New instances of Classes

Advanced Topics on Classes and Objects

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Slide 1 CS 170 Java Programming 1

COMP 249: Object Oriented Programming II. Tutorial 11: Generics

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Assertions, pre/postconditions

Defining Classes and Methods

Transcription:

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

As in the last lecture, the class declaration starts by specifying the class name public class Rectangle However, we also want to compare Rectangle objects (e.g., to sort them) public class Rectangle implements Comparable<Rectangle> Additional interfaces can be added in a comma-separated sequence 3

Remember that attributes are declared as access [static] [final] type name [= value]; We are choosing to declare private variables for the height and width, leaving them uninitialized private int width; private int height; 4

Static attributes are typically initialized when they are declared, non-static are initialized in the constructor An attribute s scope is the entire class The fully qualified way to reference an attribute is with the this keyword this.width or this.height However, this can be omitted if the result is unambiguous (e.g., attribute shadowing) 5

Constructors are defined as follows: access ClassName(anyParameters) Like methods, they can be overloaded When a new object is created Memory is allocated for the new object A constructor for the corresponding class is called Attributes are initialized 6

public Rectangle(int width, int height) { this.width = width; this.height = height public Rectangle() { this.width = 0; this.height = 0; 7

Takes an object of the same class as a parameter public ClassName(ClassName other) Creates a new object with attributes identical to the passed object Note that you do not need (and should not declare) a copy constructor for an immutable type, as the resulting object would be redundant 8

When a constructor invokes another constructor it is called constructor chaining To invoke a constructor in the same class you use the this keyword If you do this then it must occur on the first line of the constructor body Used to reduce code duplication Not always feasible E.g., if constructor can throw exceptions 9

public Rectangle() { this(0, 0); // calls Rectangle(int, int) public Rectangle(Rectangle r) // copy constructor { this(r.width, r.height); // can access private attributes 10

Methods are defined as follows: access returntype signature Methods can perform any operation on an object, but typically fall into categories: Accessors return an attribute value Mutators modify an attribute value Obligatory satisfy superclass or interface requirements Class-specific defined by purpose of class 11

Allows access to (otherwise private) attribute values Naming convention: getx() for a non-boolean attribute, named x isx() for a Boolean attribute, named x public int getwidth() { return this.width; // similar for getheight() 12

Allows modification of (otherwise private) attribute values Naming convention: setx() for an attribute, named x public void setwidth(int width) { this.width = width; // similar for setheight() 13

Instead of relying on preconditions, can use mutators to validate argument values Throw an exception Return a Boolean value For the Rectangle class, validate that any width argument is non-negative 14

/** Sets the width of this rectangle to the given width. @param width The new width of this rectangle. @throws IllegalArgumentException if width < 0. */ public void setwidth(int width) throws IllegalArgumentException { if (width < 0) { throw new IllegalArgumentException( "Argument cannot be negative"); else { this.width = width; 15

/** Sets the width of this rectangle to the given width if the given width is greater than or equal to 0. Returns whether the width has been set. @param width The new width of this rectangle. @return true if width >= 0, false otherwise. */ public boolean setwidth(int width) { boolean isset = width >= 0; if (isset) { this.width = width; return isset; 16

Provides a textual representation of the Rectangle E.g., Rectangle of width 1 and height 2 Default returns class name and memory address For the Rectangle class: public String tostring() { return "Rectangle of width " + this.width + " and height " + this.height; 17

Evaluate object equality using attribute values (i.e., object state) Default compares memory address (like ==) Implementing equals() is surprisingly hard "One would expect that overriding equals(), since it is a fairly common task, should be a piece of cake. The reality is far from that. There is an amazing amount of disagreement in the Java community regarding correct implementation of equals()." Angelika Langer, Secrets of equals() Part 1 http://www.angelikalanger.com/articles/javasolutions/secretsofequals/equals.html Our approach is consistent with many texts 18

The implementation of equals() used in the notes and the textbook is based on the rule that an instance can only be equal to another instance of the same type At first glance, this sounds reasonable and is easy to implement using Object.getClass() public final Class<? extends Object> getclass() Returns the runtime class of an object. 19

Recall that the value of the attributes of an object define the state of the object Two instances are equal if all of their attributes are equal Recipe for checking equality of attributes 1. If the attribute type is a primitive type other than float or double use == 2. If the attribute type is float use Float.compare() 3. If the attribute type is double use Double.compare() 4. If the attribute is an array consider Arrays.equals() 5. If the attribute is a reference type use equals(), but beware of attributes that might be null 20

For reference values equals() is 1. Reflexive : An object is equal to itself x.equals(x) is true 2. Symmetric : Two objects must agree on whether they are equal x.equals(y) is true if and only if y.equals(x) is true 3. Transitive : If a first object is equal to a second, and the second object is equal to a third, then the first object must be equal to the third If x.equals(y) is true, and y.equals(z) is true, then x.equals(z) must be true 21

4. Consistent : Repeatedly comparing two objects yields the same result (assuming the state of the objects does not change) 5. x.equals(null) is always false 22

public boolean equals(object object) { boolean equal; if (object!= null && this.getclass() == object.getclass()) { Rectangle other = (Rectangle) object; equal = (this.width == other.width) && (this.height == other.height); else { equal = false; return equal; 23

Required if your class implements the Comparable interface (i.e., its object can be compared, order, or sorted) Compares this object with the specified object for order Returns a negative integer if this object is less than Returns a positive integer if this object is greater than Returns zero if this object is equal to the passed one Throws a ClassCastException if the specified object type cannot be compared to this object 24

1. The sign of the returned int must flip if the order of the two compared objects flip if x.compareto(y) > 0 then y.compareto(x) < 0 if x.compareto(y) < 0 then y.compareto(x) > 0 if x.compareto(y) == 0 then y.compareto(x) == 0 25

2. compareto() must be transitive if x.compareto(y) > 0 && y.compareto(z) > 0 then x.compareto(z) > 0 if x.compareto(y) < 0 && y.compareto(z) < 0 then x.compareto(z) < 0 if x.compareto(y) == 0 && y.compareto(z) == 0 then x.compareto(z) == 0 26

3. If x.compareto(y) == 0 then the signs of x.compareto(z) and y.compareto(z) must be the same 27

An implementation of compareto() is said to be consistent with equals() when and if x.compareto(y) == 0 then x.equals(y) == true if x.equals(y) == true then x.compareto(y) == 0 28

Implementing compareto is similar to implementing equals Typically compare all of the attributes Starting with the attribute that is most significant for ordering purposes and working your way down For the Rectangle class, the API states that the width is used form comparison; if the widths are equal, the heights are used 29

public int compareto(rectangle r) { int difference; if (this.width!= r.width) { difference = this.width - r.width; else { difference = this.height - r.height; return difference; 30

Hash codes used to uniquely (ideal) correspond to an object s state (i.e., like a fingerprint or signature) Default uses memory address of the object Two objects with the same state should have the same hash code Hash-based containers use hash codes to organize and access elements efficiently Performance increases with distinct hash codes 31

Poor, but legal implementation: public int hashcode() { return 1; Better implementation: public int hashcode() { return this.getwidth() + this.getheight(); 32

Demonstrate how Eclipse can help generate obligatory methods 33

Exist to fulfill the purpose of the class For the Rectangle class: public int getarea() { return this.width * this.height; public void scale(int factor) { this.width = this.width * factor; this.height = this.height * factor; 34

Similar to testing a utility class, but Must call a constructor to create an object Test all constructors to ensure objects are correctly initialized Test all mutators and accessors to ensure attributes are correctly modified and returned Test the equals and compareto methods adhere to conventional rules for equality and comparison 35

Similar to documenting a utility class Use Javadoc comments to describe the class and all public features The fully implemented and documented version of the Rectangle class is available here: www.eecs.yorku.ca/~buildit/code//2/rectangle.java.txt 36

Easier to program, debug, and maintain Already seen with constructor chaining Other techniques: Delegating to mutators Delegating to accessors 37

Use mutator whenever an attribute needs changing E.g., can re-write the Rectangle constructor s body this.width = width; this.height = height; becomes this.setwidth(width); this.setheight(height); Single source of attribute modification Can change how attributes are represented with minimal code modification (e.g., as doubles, in array, as a String) 38

Use accessor whenever reading an attribute E.g., can re-write the getarea method s body this.width * this.height; becomes this.getwidth() * this.getheight(); Single source of attribute access Can change how attributes are represented with minimal code modification (e.g., as doubles, in array, as a String) 39

A mutable object that is passed to or returned from a method can be changed Problems: Private attributes become publicly accessible Objects can be put into an inconsistent state Solution: Make a copy of the object and save the copy Use copy constructors 40

Bad public Date getduedate() { Good return duedate; // Unsafe public Date getduedate() { return new Date(dueDate.getTime()); // Avoid leak 41

Bad public void setduedate(date newdate) { Good duedate = newdate; // Unsafe public void setduedate(date newdate) { duedate = new Date(newDate.getTime()); // Avoid leak 42

A class defines an immutable type if an instance of the class cannot be modified after it is created Each instance has its own constant state More precisely, the externally visible state of each object appears to be constant Java examples: String, Integer (and all of the other primitive wrapper classes) Advantages of immutability versus mutability Easier to design, implement, and use Can never be put into an inconsistent state after creation 43

PhoneNumber API PhoneNumber - areacode : short - exchangecode : short - stationcode : short + PhoneNumber(int, int, int) + equals(object) : boolean + getareacode() : short + getexchangecode() : short + getstationcode() : short + tostring() : String 44

1. Do not provide any methods that can alter the state of the object Methods that modify state are called mutators Java example of a mutator: import java.util.calendar; public class CalendarClient { public static void main(string[] args) { Calendar now = Calendar.getInstance(); // set hour to 5am now.set(calendar.hour_of_day, 5); 45

2. Prevent the class from being extended. Note that all classes extend java.lang.object One way to do this is to mark the class as final public final class PhoneNumber { // version 0 A final class cannot be extended Don't confuse final variable and final classes The reason for this step will become clear in a couple of weeks 46

3. Make all attributes final Recall that Java will not allow a final attribute to be assigned to more than once final attributes make your intent clear that the class is immutable public final class PhoneNumber { // version 1 private final short areacode; private final short exchangecode; private final short stationcode; Notice that the attributes are not initialized here That task belongs to the class constructors 47

4. Make all attributes private This applies to all public classes (including mutable classes) In public classes, strongly prefer private attributes Avoid using public attributes private attributes support encapsulation Because they are not part of the API, you can change them (even remove them) without affecting any clients The class controls what happens to private attributes It can prevent the attributes from being modified to an inconsistent state 48

5. Prevent clients from obtaining a reference to any mutable attributes Recall that final attributes have constant state only if the type of the attribute is a primitive or is immutable If you allow a client to get a reference to a mutable attribute, the client can change the state of the attribute, and hence, the state of your immutable class 49

Involves saving an object s characteristic, rather than recalculating it Must balance added redundancy (bad) with increased performance (good) Attribute is recalculated only when needed (i.e., when its components are modified) and returned when its accessor method is called 50

A property that objects of a particular class always hold E.g., a Rectangle s width and height are always 0 Must satisfy two constraints: 1. The class invariant has to be true after each public constructor invocation (provided the constructor s precondition is also met) 2. The class invariant has to be maintained by each public method invocation (provided the method s precondition is also met) 51

You can think of a hash table as being an array of buckets where each bucket holds the stored objects 0 1 2 3... N 52

To insert an object a, the hash table calls a.hashcode() method to compute which bucket to put the object into c.hashcode() d.hashcode() N N a.hashcode() 2 b.hashcode() 0 0 1 2 3... N means the hash table takes the hash code and does something to it to make it fit in the range 0 N 53

To see if a hash table contains an object a, the hash table calls a.hashcode() method to compute which bucket to look for a in z.hashcode() N a.hashcode() 2 b a.equals( a ) z.equals( c ) true z.equals( d ) false 0 1 2 3... N 54

Searching a hash table is usually much faster than linear search Doubling the number of elements in the hash table usually does not noticably increase the amount of search needed If there are n PhoneNumbers in the hash table: Best case: the bucket is empty, or the first PhoneNumber in the bucket is the one we are searching for 0 or 1 call to equals() Worst case: all n of the PhoneNumbers are in the same bucket N calls to equals() Average case: the PhoneNumber is in a bucket with a small number of other PhoneNumbers a small number of calls to equals() 55

What do you need to be careful of when putting a mutable object into a HashSet? 56