(a) in assignment statements. (b) when invoking methods (with objects as parameters) (d) equality of references versus equality of objects

Similar documents
Methods (example: Methods1.java)

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Introductory Programming Arrays, sections

Example: Tax year 2000

Introductory Programming Inheritance, sections

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

Defining Your Own Classes

Chapter 4 Defining Classes I

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

University of Palestine. Mid Exam Total Grade: 100

Introduction to Programming Using Java (98-388)

Chapter 6 Introduction to Defining Classes

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

ITI Introduction to Computing II

Lab Assignment Three

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

CS 101 Exam 2 Spring Id Name

Computer Science II (20082) Week 1: Review and Inheritance

Chapter 4. Defining Classes I

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Lecture 10 Declarations and Scope

Chapter 1b Classes and Objects

CMSC131. Library Classes

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

Utilities (Part 2) Implementing static features

Classes Classes 2 / 35

H212 Introduction to Software Systems Honors

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Lecture 5: Methods CS2301

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Lecture 2: Java & Javadoc

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

Objects and Classes: Working with the State and Behavior of Objects

Java Object Oriented Design. CSC207 Fall 2014

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Object Oriented Programming

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

CS180. Exam 1 Review

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

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

Inheritance (Part 5) Odds and ends

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Java Identifiers, Data Types & Variables

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Lecture 3. Lecture

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

CS111: PROGRAMMING LANGUAGE II

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

BM214E Object Oriented Programming Lecture 7

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

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

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

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

STUDENT LESSON A5 Designing and Using Classes

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Instance Members and Static Members

CHAPTER 7 OBJECTS AND CLASSES

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

Programming Problems 22nd Annual Computer Science Programming Contest

Exam 1 - (20 points)

Exam Duration: 2hrs and 30min Software Design

Chapter 15: Object Oriented Programming

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

Defining Classes and Methods

Advanced Programming - CS239

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

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

CMSC 132: Object-Oriented Programming II. Interface

CS 231 Data Structures and Algorithms, Fall 2016

Classes Classes 2 / 36

Java and OOP. Part 2 Classes and objects

Object Oriented Programming. Java-Lecture 6 - Arrays

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

COMP 1210 Documentation Guidelines Page 1 of 7. Class documentation (Chapter 1):

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

CSC1590 Java Assignment 2-0. User Defined Classes

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Container Vs. Definition Classes. Container Class

Classes. Classes (continued) Syntax The general syntax for defining a class:

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

Questions Answer Key Questions Answer Key Questions Answer Key

Chapter 11: Create Your Own Objects

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Recitation 3 Class and Objects

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

An introduction to Java II

CS 101 Spring 2007 Midterm 2 Name: ID:

HST 952. Computing for Biomedical Scientists Lecture 5

Transcription:

Introductory Programming Object oriented programming II; sections 5.0, 5.1, 5.4, 5.5 Anne Haxthausen, IMM, DTU 1. References (section 5.0) the null reference the this reference aliases (a) in assignment statements (b) when invoking methods (with objects as parameters) (c) important implications of aliases (d) equality of references versus equality of objects 2. The static modifier (section 5.1) 3. Nested classes (read on your own: section 5.4) 4. Interfaces (section 5.5) a. Parts of this material are inspired by/originate from a course at ITU developed by Niels Hallenberg and Peter Sestoft on the basis of a course at KVL developed by Morten Larsen and Peter Sestoft. Translated into English by Morten P. Lindegaard. c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-1

The Time example revisited class Time { private int hours, min; //hours and minutes since midnight public Time(int h, int m) {hours = h; min = m; public int gethours() {return hours ; public int getmin() { return min; public String tostring() { return hours + "." + min; public void passtime(int m) { int totalmin = 60 * hours + min + m; hours = (totalmin / 60) % 24; min = totalmin % 60; c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-2

The null reference A variable of a class type contains a reference to (i.e. the address of) an object of the given class. Before the variable is initialized, it will contain the special reference value null which denotes that the variable does not (yet) refer to an object. Example Time clock1; //A: clock1 == null after declaration clock1 = new Time(12, 35); //B: clock1!= null after initialization A B clock1 : Time clock1 hours 12 min 35 c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-3

class Time { The this reference private int hours, min; //min #1... public void passtime(int min) { //min #2 (shadows min #1) int totalmin = 60 * this.hours + this.min + min; this.hours = (totalmin / 60) % 24; this.min = totalmin % 60; c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-4

Object aliases in assigment Two variables may refer to the same object they are said to be aliases. Example Time clock1 = new Time(9, 25); Time clock2 = clock1; clock1 : Time hours 9 clock2 min 25 c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-5

Important implications of aliases Watch out! If you use aliases, a change in the state of an object is a change for all the references that refer to the object. Example Time clock1 = new Time(9, 25); Time clock2 = clock1; clock1.passtime(10); clock1 : Time hours clock2 min 9 25 35 c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-6

Equality of references versus equality of objects Equality of references: == clock1 == clock2 decides whether clock1 and clock2 refer to the same object i.e. are aliases Equality of objects: In the Time class, we can define our own equality as a method named equals: boolean equals(time clock2) { return this.min == clock2.min && this.hours == clock2.hours; Two objects may then be compared as follows: clock1.equals(clock2) c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-7

Equality: examples public class TimeAliasing { public static void main(string[] args) { Time clock1, clock2; clock1 = new Time(9, 10); clock2 = new Time(9, 10); printtimes(clock1, clock2); clock1.passtime(40); printtimes(clock1, clock2); clock2 = clock1; printtimes(clock1, clock2); clock1.passtime(40); printtimes(clock1, clock2); static void printtimes(time clock1, Time clock2) { System.out.println("clock1 shows " + clock1); System.out.println("clock2 shows " + clock2); System.out.println("clock1 == clock2: " + (clock1 == clock2)); System.out.println("clock1.equals(clock2): " + clock1.equals(clock2) +"\n"); c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-8

Output from TimeAliasing clock1 shows 9.10 clock2 shows 9.10 clock1 == clock2: false clock1.equals(clock2): true clock1 shows 9.50 clock2 shows 9.10 clock1 == clock2: false clock1.equals(clock2): false clock1 shows 9.50 clock2 shows 9.50 clock1 == clock2: true clock1.equals(clock2): true clock1 shows 10.30 clock2 shows 10.30 clock1 == clock2: true clock1.equals(clock2): true c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-9

What happens to variables that are used as actual parameters for a method? Java uses call-by-value when values are passed to a method amounts to copying the value of the actual parameter to the formal parameter in the method. After a call of the mehtod, the actual parameter (here: variable) has the same value as before the call. Meaning: when a variable of a class type is an argument of a method, the reference to the object is copied not the object itself if the method body changes the formal parameter reference (makes it refer to another object), it does not influence the argument (see example1) if the method body changes the state of the object that the formal parameter refers to, the same happens to the argument (see example2) c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-10

Primitive values as arguments for methods: example What is the output of the following program? public class CallOfMethod { public static void main(string[] args) { int a = 2; System.out.println("a is " + a); f(a); System.out.println("a is " + a); static void f(int x) { x = x + 10; c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-11

Objects as arguments for methods: example1 What is the output of the following program? public class CallOfMethod1 { public static void main(string[] args) { Time a = new Time(9, 25); System.out.println("a is " + a); f(a); System.out.println("a is " + a); static void f(time x) { x = new Time(8, 17); c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-12

Objects as arguments for methods: example2 What is the output of the following program? public class CallOfMethod2 { public static void main(string[] args) { Time a = new Time(9, 25); System.out.println("a is " + a); f(a); System.out.println("a is " + a); static void f(time x) { x.passtime(10); c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-13

Static fields We have already seen that methods may be declared as static so that they can be used via the name of the class without creating an object. The same goes for the fields in a class. Then all objects that are instances of the class will have a common location in the memory for the field; not individual copies. Example A class field timezone could be used for the time zone common to all points in time. class Time { static int timezone = 60; //minutes east of Greenwich... everything else as before... c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-14

Static fields, example continued Time t1 = new Time(12, 35); Time t2 = new Time(13, 15); Time.timezone = 120; class Time timezone 120 60 hours 12 min : Time 35 t1 t2 hours 13 min : Time 15 c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-15

Interfaces So far, we have used the concept of interface for the public constants and methods of an object. This is consistent with the more formal concept of interface in Java. Definition: A Java interface consists of: a collection of public constants and/or a series of public abstract methods, i.e. methods without bodies Notice: no constructor, no variable fields. Definition: A class implements an interface by giving method implementations (bodies) for all the abstract methods of the interface and possibly additional fields, methods, and constructors. Notice: an interface may have several implementations and a class may implement several interfaces. c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-16

Interfaces: example interface TimeInterface { public int gethours(); public int getmin(); public String tostring(); public void passtime(int m); c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-17

Implementation of interfaces: example 1 class Time1 implements TimeInterface { private int hours, min; //hours and minutes since midnight public Time1(int h, int m) {hours = h; min = m; public int gethours() {return hours ; public int getmin() { return min; public String tostring() { return hours + "." + min; public void passtime(int m) { int totalmin = 60 * hours + min + m; hours = (totalmin / 60) % 24; min = totalmin % 60; c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-18

Implementation of interfaces: example 2 class Time2 implements TimeInterface { private int min; //minutes since midnight public Time2(int h, int m) {min = h * 60 + m; public int gethours() {return... ; public int getmin() { return... ; public String tostring() { return... ; public void passtime(int m) { min =... ; c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-19

What are interfaces used for (1)? An interface specifies public methods and constants that an implementing class (or classes) must offer (as a minimum). In software development, interfaces may be used for the WHAT before HOW principle: 1. An interface is made first: it is a specification of WHAT a class must offer. 2. Afterwards, classes that implement the interface are made: they define HOW data and algorithms are. The interface may be used as a contract between the people writing the class and the people using the class. c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-20

What are interfaces used for (2)? The private modifier may be used in classes for information hiding : only public methods and constants are exported. By declaring the variable fields in a class as private, it is possible to replace them and likewise replace method bodies without needing to change method calls. Interfaces are also used for information hiding with similar effect. But if you regret an implementation, i.e. Time1, you do not need to change it just make a new implementation, i.e. Time2, and replace the use of Time1 with Time2. c Haxthausen and Sestoft, IMM/DTU, 18. september 2002 02100+02115+02199+02312 Introductory Programming Side 5-21