Before We Begin. Class Implementation II. Introduction to Computer Science II. Overview (1): Administrative Details (1):

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

CMSC 202H. Classes and Objects: Reusing Classes with Composition

CSC 175 Intermediate Programming

Chapter 5. Defining Classes II. Slides prepared by Rose Williams, Binghamton University

Chapter 5. Defining Classes II. Copyright 2016 Pearson Inc. All rights reserved.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Computer Science II Data Structures

Using Objects. a first example of an object classes and objects in Java

Before We Begin. Introduction to Computer Use II. Overview (1): Winter 2006 (Section M) CSE 1530 Winter Bill Kapralos.

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

APCS Semester #1 Final Exam Practice Problems

MIT AITI Python Software Development

CSE 8B Intro to CS: Java

CSCI 161 Introduction to Computer Science

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Data Structures (list, dictionary, tuples, sets, strings)

Mutable and Immutable Objects

Aggregation and Composition. [notes Chapter 4]

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

Announcements for the Class

Aggregation and Composition. [notes Chapter 4]

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

CSE115 Lab 4 Fall 2016

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Fundamental Java Methods

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Slide 1 CS 170 Java Programming 1

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

COE318 Lecture Notes Week 4 (Sept 26, 2011)

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

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

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

CS360 Lecture 5 Object-Oriented Concepts

Programming Exercise 14: Inheritance and Polymorphism

Separate Compilation Model

Objects as a programming concept

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSE 331 Midterm Exam 2/13/12

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Lecture 7. Log into Linux New documents posted to course webpage

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

A First Object. We still have another problem. How can we actually make use of the class s data?

A Model of Mutation in Java

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

Classes Classes 2 / 35

Advanced concurrent programming in Java Shared objects

EECS168 Exam 3 Review

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

JAVA MOCK TEST JAVA MOCK TEST II

CS161: Introduction to Computer Science Homework Assignment 10 Due: Monday 11/28 by 11:59pm

CS1004: Intro to CS in Java, Spring 2005

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

A class is a user-defined type. It is composed of built-in types, other user-defined types and

CSC-140 Assignment 6

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

Software Design and Analysis for Engineers

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Chapter 4 Defining Classes I

Express Yourself. Writing Your Own Classes

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

COE318 Lecture Notes Week 3 (Sept 19, 2011)

JAVA GUI PROGRAMMING REVISION TOUR III

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Subclassing for ADTs Implementation

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Lecture 5 Representation Invariants

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

1B1b Classes in Java Part I

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

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

Midterms Save the Dates!

Lecture Notes for CS 150 Fall 2009; Version 0.5

52 Franck van Breugel and Hamzeh Roumani

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Classes Classes 2 / 36

Before We Begin. Introduction to Computer Use II. Overview (1): Winter 2005 (Section M) CSE 1530 Winter Bill Kapralos.

ITI Introduction to Computing II

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

CSE115 Lab 2 Fall 2016

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Example Program. public class ComputeArea {

3 ADT Implementation in Java

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

CE221 Programming in C++ Part 1 Introduction

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Transcription:

Overview (1): Before We Begin Some administrative details Some questions to consider A Closer Look at References Class Implementation II Thursday, May 11 2006 The Person class (from textbook) Class objects as parameters Copy constructor Accessor and mutator methods Mutable and immutable classes CSE 1030, Summer 2006, Administrative Details (1): Exercise PEx01 Before We Begin Due Friday, May 12 Although you are not graded for this, you should still submit your solution! No Lecture May 16 I will be in Europe away for a conference from May 12-17 Will respond to email but there may be a delay! I will re-schedule this lecture towards the end of the semester Administrative Details (2): Congress of the Humanities and Social Sciences 2006 at York Saturday, May 27 to Saturday, June 3, 2006 Classes (including unfortunately CSE 1030) will not be held on any York Campus from Monday, May 29 to Saturday, June 3, nor will classes be provided by Internet or correspondence during this period Some Questions to Consider (1): What is a static method / variable? What restrictions must we be aware of when implementing a static method and how can we overcome this? How is memory organized? What is a reference? How are objects represented in memory? In Java, ca we directly access/manipulate a reference?

The Person Class (1): Class From your Textbook A Closer Look at References We will use it to examine various of the concepts that we will discuss today Page 296 Brief Description Describes basic data about people in places such as author listings, library catalogues etc. Person s name Date of birth Date of death Brief Description (cont.) All instance variables are of a class type no primitive type instance variables Person s name String Date of birth Date class Date of death Date class (null if person is still alive) Lets take a look at the code of both the Person and Date class Notice how in the equals method, we call the equals method of the instance variable objects Some Comments Contains no no-argument constructor why? But shouldn t all classes contain a no-argument constructor? Does it make sense to have a no-argument constructor for the Person class? Think about what it means to be a person Class Invariant A statement that is true of every object of the class created by a constructor All other methods of the class preserve the truth of this statement After executing a method, still holds true With respect to the Person class, what is the class invariant? What is true about every instantiated object of the Person class? Class Parameters (1): Reference Method Parameters As you are aware, in addition to primitive types, a method can also have class parameters The caller to such a method can pass an object to it Important we are not really passing the actual object itself but rather, we are passing the reference that points to the object This actually can lead to some subtleties When working with reference parameters, you may not always achieve what you intended!

Class Parameters (2): Reference Method Parameters (cont.) Reference parameters are also passed-by-value as with the primitive types Recall that with call-by-value, the parameter itself within the method is a local variable in other words, we have a local variable that points to some object Lets look at an example Once again, the ToyClass Lets create a main method that creates a ToyClass object and calls its methods Class Parameters (3): 3078 Reference Method Parameters (cont.) Mr. Cellphone 0 Before the call to the change method aparameter 3078 Mr. Cellphone 0 Before the call to the change method aparameter 3078 Hot Shot 42 After the call to the change method Class Parameters (4): Reference Method Parameters What implications do reference parameters have with respect to mutator and accessor methods? Copy Constructor (1): A Special Type of Constructor Single parameter of the same type as the class itself Lets look at an example Date class Should create an public Date(Date adate) object that is a if (adate == null)//not a real date. separate and System.out.println("Error."); independent object but with instance variables that are equal (exact month = adate.month; copy) of the parameter year = adate.year; object Copy Constructor (2): Date date1 = new Date( January, 1, 2006); Date date2 = new Date(date1); public Date(Date adate) if (adate == null) After above code is executed both date1 and date2 represent the System.out.println( Error! ); same date (Jan. 1 2006) but they are two different objects. In month = adata.month; other words, if we change date1, date2 is not changed and viceversa (date2 is the caller and year = adate.year; date1 is an argument) Copy Constructor (3): Lets modify the Date class by adding an additional instance variable of a class type (assume some class called Weather) Date date1 = new Date( January, 1, 2006); Date date2 = new Date(date1); What is wrong with this copy constructor definition? Is it correct? Consider the following date2.weather = null; public Date(Date adate) if (adate == null) System.out.println( Error! ); month = adata.month; year = adate.year; weather = adate.weather;

Copy Constructor (4): The previous code segment is an example of a nonsafe copy constructor We simply copied the weather instance variable but all we really did was copied the reference But the copy and the original reference point to the same object (one object) What we need to do is make a copy of the object weather = new Weather() Create a new object and then copy the values Accessor and Mutator Methods (1): Privacy Leaks As with the issue we discussed with the copy constructor, a similar problem can arise with mutator and accessor methods Lets recall an accessor method that returns the value of a primitive type Rectangle class (getwidth) We simply return the value of the primitive type instance variable public int getwidth() return awidth; Accessor and Mutator Methods (2): What if the accessor method returns a non-primitive type (e.g., class type)? Lets look at two ways to handle this Person class (getbirthdate method) Which of the two implementations is correct and why? Accessor and Mutator Methods (3): Lets assume we chose the second one Consider the following code segment Person citizen = new Person( Joe Citizen, new Date( January, 1, 1900), new Date( January, 1, 1990)); Date mydate = citizen.getbirthdate(); mydate.setdate( April, 1, 3000); return new Date(born); return born; We have basically circumvented the private declared instance variable We have changed the birth date of the Person object to a date after the death impossible! Accessor and Mutator Methods (4): But mydate refers to the private instance variable! we have changed the value of a private declared instance variable! When returning a class type instance variable, be sure to avoid this memory leak Return a copy of the class - not the reference! Accessor and Mutator Methods (5): The same of course applies to a mutator method It is not enough to simply copy the parameter class type (reference) Rather, we must make a copy of the object that the reference parameter points to

Mutable & Immutable Classes (1): But what about the method getname of the Person class? Why does it return the reference of the private instance variable name (of type String)? This is inconsistent??? return name; Mutable & Immutable Classes (2): Basically, the String class does not have any mutator methods doesn t contain any methods that can change any data of a String once it has been created Even though it seems like we can String greeting = Hello ; greeting = greeting + Goodbye ; But haven t we changed the string greeting after executing the second line? No! Rather a new object is created and reassigned to the greeting variable Mutable & Immutable Classes (3): Immutable Class A class that contains no methods (other than the constructor) that change any of the data in an object of the class Objects of such classes are know as immutable objects Safe to return a reference to an immutable object since its data cannot be changed The String class is an example of an immutable class Mutable & Immutable Classes (4): Mutable Class A class that contains public methods that can change the data of an object of the class Objects of such classes are known as mutable objects Should avoid writing methods that return mutable objects can lead to memory leaks! In fact, you should never write such methods! Exercise PEx01 (1): Any Questions? Exercise PEx01