Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Similar documents
Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Midterms Save the Dates!

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Midterms Save the Dates!

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

Midterms Save the Dates!

Midterms Save the Dates!

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Midterms Save the Dates!

Midterms Save the Dates!

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Survey #3. Final Exam. Today is totally optional! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings.

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Last Week: Organizing Data. Last Week: Parallel Arrays. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Midterms Save the Dates!

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

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Inheritance and Interfaces

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

QUIZ. How could we disable the automatic creation of copyconstructors

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Midterms Save the Dates!

Midterms Save the Dates!

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

CE221 Programming in C++ Part 1 Introduction

CS 251 Intermediate Programming Inheritance

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

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).


CS 170 Java Programming 1. Week 12: Creating Your Own Types

CPSC 324 Topics in Java Programming

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Programming Languages and Techniques (CIS120)

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

Lecture 2: Java & Javadoc

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

QUIZ. How could we disable the automatic creation of copyconstructors

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Abstract Classes and Interfaces

CS S-08 Arrays and Midterm Review 1

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

5.6.1 The Special Variable this

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CS 11 java track: lecture 3

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Array. Prepared By - Rifat Shahriyar

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CSCI 355 Lab #2 Spring 2007

Arrays Classes & Methods, Inheritance

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

Software and Programming 1

CSE 303: Concepts and Tools for Software Development

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

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

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

4. Java Project Design, Input Methods

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

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

Unit 5: More on Classes/Objects Notes

Building Java Programs

Design Patterns: State, Bridge, Visitor

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

CSC9T4: Object Modelling, principles of OO design and implementation

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

JAVA: A Primer. By: Amrita Rajagopal

CSE : Python Programming

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Agent Design Example Problems State Spaces. Searching: Intro. CPSC 322 Search 1. Textbook Searching: Intro CPSC 322 Search 1, Slide 1

CSC207 Week 3. Larry Zhang

Object- Oriented Design with UML and Java Part I: Fundamentals

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Part 2: The Material PART 2

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Midterms Save the Dates!

C# Programming for Developers Course Labs Contents

Java Fundamentals (II)

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Interfaces vs. Inheritance Abstract Classes Inner Classes Readings This Week: No new readings. Consolidate! (Reminder: Readings are absolutely vital for learning this stuff!) Lecture 22 Labs and Tutorials This Week: Lab #10 Labs are due at end of lab time! (Lab #10 is fairly short, but make sure to finish by the end of lab.) Final Exam Wednesday, April 15, 7pm in SRC A This wasn t a good room last year, but we re stuck with the date, time, and room UBC assigns. Note: You are allowed to do scratch work on the exam paper! (But write your answer where indicated if the problem tells you to.) Programming Assignment 3 Assignment 3 is up on WebCT! Click on the Assignments icon. Assigned Sunday evening sorry for delay! Due at NOON, April 6 (Monday), via electronic hand in. Tips: There is some Eclipse setup. Set-up ASAP! Work in pairs. Some conceptual stuff. Think carefully before coding. If concepts right, the coding is much much easier. Learning Goals By the end of class today you will be able to List similarities and differences between interfaces and inheritance. Create abstract classes and extend abstract classes. Write code that uses inner classes. (Work through complicated examples of parameter passing and arrays.) 1

We learned these as completely separate concepts: An interface is a contract, specifying some methods that must be implemented by any class that claims to implement the interface. A superclass is a class from which other classes can inherit methods and instance fields, so we can reuse the superclass s implementation. But they have similarities Both allow creating different, new classes that share some of the same methods, e.g. Double and UBCStudent both implement Comparable, so they both have int compareto() Swimmer and Crawler both inherit from Animal, so they have e.g., changeimage() But they have similarities Both allow declaring references that can point to different kinds of objects, e.g., Comparable x; x = new Double(3.14); x = new UBCStudent( Alan ); Person y; y = new UBCStudent( Alan,0.0); y = new Celebrity( Paris Hilton ); But they have similarities Therefore, both allow polymorphism, e.g., Comparable x;... if (x.compareto()) Animal y;... y.advanceonetimestep(); But they have similarities They even have similar UML: Comparable LightInfantry They have differences, too Java allows implementing multiple interfaces Comparable Feedable UBCStudent Kangaroo UBCStudent OK! 2

They have differences, too Java does NOT allow multiple inheritance Marsupial LightInfantry (Java does allow one inheritance plus multiple interfaces.) Comparable Feedable LightInfantry Kangaroo UBCStudent OK! Kangaroo The fundamental difference: An interface provides no implementations. Everything in a (super)class is implemented. The fundamental difference: To implement interface, a class must implement everything. Subclasses automatically inherit superclass implementation. Can optionally override. Questions Therefore: You cannot create objects of an interface type (only references). You can create objects of the (super)class type. (You can create references, too.) 3

The fundamental difference: An interface provides no implementations. Everything in a (super)class is implemented. Wouldn t it be cool to have something in-between? Abstract Classes The classes we have written so far are called concrete classes. Abstract classes in Java provide a blend of the concepts of interfaces and inheritance: Some (from none to all) methods are left unimplemented. These are called. Instances fields and implemented methods are also allowed. Subclass must implement (in order to become concrete). Subclass inherits or overrides other stuff. Abstract Class Syntax To create an abstract class, just add the abstract keyword: public abstract class Foo { Abstract Method Syntax Inside an abstract class, you are allowed to declare : Just add the abstract keyword. And put a semicolon instead of a body. public abstract class Foo { public abstract void display(); Abstract Classes vs. Interfaces/Inheritance An abstract class with no is basically a normal, concrete class. (But Java still thinks it s an abstract class!) An abstract class with all is basically like an interface. (But Java still thinks it s an abstract class!) An abstract class with some abstract methods is somewhere in-between. The Interface-Class Spectrum interface abstract class w/ only abstract class w/ no concrete class Presentation idea due to Steve Wolfman 4

The Interface-Class Spectrum interface abstract class w/ only The Interface-Class Spectrum interface abstract class w/ only abstract class w/ no Can create objects only of concrete classes concrete class You can implement multiple interfaces. abstract class w/ no concrete class Presentation idea due to Steve Wolfman Presentation idea due to Steve Wolfman Questions Inner Classes So far, classes are always in a file by themselves (with the same name as the class). Sometimes, we just want a simple little class for something. It s a pain to have a separate file. We can declare classes inside another: method class Example: Inner Class in a Method public static void main(string[] args) { class Foo { private int x; public int getx() { return x; Foo is available only inside main. Foo can access surrounding vars with restrictions. Inner Class Scope Remember the general principle that you can see out through curly braces, but not in. Therefore, inner classes should be able to see variables declared around them: Instance variable in same class Local variables if declared inside a method However, object of inner class can outlive the code where it was created! Access is to instance variables of object that created inner class. Access only to final local variables. (These can be references to mutable objects, though.) 5

Questions Review: Passing Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): public void flatter(string adjective) { System.out.println("Wow, you look " + adjective + "!"); Review: Passing Parameters In the object user (the caller): a.flatter( fabulous ); adjective = fabulous ; In the class definition (the callee): public void flatter(string adjective) { System.out.println("Wow, you look " + adjective + "!"); Parameter Passing Challenge #1 Consider the following method: public void swap( int a, int b ) { int temp = a; a = b; b = temp; and the following code from main that calls swap: int x = 0; int y = 5; swap( x, y ); What values are stored in the x and y after the above code segment has executed? Parameter Passing Challenge #2 Consider the following method: Just as process is called public void process( int[][] arra, int[][] arrb ) { int row, col; int[][] arrc = new int[][] { { 1, 1, 1, { 1, 1, 1 ; arra = arrc; for( row = 0; row < arrb.length; row++ ) for( col = 0; col < arrb[ row ].length; col++ ) arrb[ row ][ col ] = row + col; and the following code from main that calls process: int[][] dataa = new int[][] { { 0, 0, { 0, 0 ; int[][] datab = new int[][] { { 0, 0, { 0, 0 ; process( dataa, datab ); What values are stored in the arrays dataa and datab after the above code segment has executed? 6

Just before process s first line Just after arrc is initialized Just after arra = arrc; Just as process completes After returning to main Parameter Passing Intuition Calling a method is like asking a friend to do something for you. Passing parameters is giving your friend the information needed to do the task. In Java, parameters are always call by value (sometimes pass by value ): You make a copy of the info for your friend. Java makes a copy of the parameters (the value of the parameters) for the method call. Passing primitive type values vs. objects? 7

Real Life Analogy You show me a $20 bill. I make a photocopy. Can I spend your money? If I burn my photocopy, do I destroy your $20 bill? You show me your credit card. I make a photocopy. Can I spend your money? If I burn my photocopy, do I destroy your credit card? $20 bill is like a primitive type. It is the value. Credit card is like an object reference. It says how to find the value (your credit line). Thoughts In every case, the variables in main still stored the same values they did before. Java is pass-by-value ; we only pass the values of arguments to the parameters of a method. So, we can never change those arguments. But if the variable is a reference variable, the object it points to can be changed! That s what happened to datab. Why didn t dataa change? Parameter Passing Challenge #3 Just as swap is called Consider the following method: public void swap( int[][] a, int[][] b ) { int[][] temp = a; a = b; b = temp; and the following code that calls swap: int[][] x = new int[][] {{0, 0, {0, 0; int[][] y = new int[][] {{1, 1, {1, 1; swap( x, y ); What values are stored in the x and y after the above code segment has executed? Just before swap s first line Just as swap completes 8

After returning to main How do I get swap to work? Java doesn t really have a good way to do this. How do I swap the contents of two pockets if you give me just copies of the contents? With object references, I can kind of do it: I can t swap your references, but I can follow the object references to the objects and change the contents of the objects. Other languages (like C++) let you create references to any variable (even primitive types). If two of you give me (a copy of) the locations of your dorm rooms, I can swap the contents. This is called call by reference or pass by reference. Something like swap private static void swap(int[][] a, int[][] b) { assert a.length == b.length; int[][] temp = new int[a.length][]; for (int i=0; i<a.length; i++) { temp[i] = a[i]; for (int i=0; i<b.length; i++) a[i] = b[i]; for (int i=0; i<b.length; i++) b[i] = temp[i]; and the following code that calls swap: int[][] x = new int[][] {{0, 0, {0, 0; int[][] y = new int[][] {{1, 1, {1, 1; swap( x, y ); What values are stored in the x and y after the above code segment has executed? 9