COMP 250: Java Reference Types & Object Oriented Programming

Similar documents
COMP 250: Java Object Oriented Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP Data Structures

Java Object Oriented Design. CSC207 Fall 2014

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

COMP-202 Unit 9: Exceptions

Lesson 3: Accepting User Input and Using Different Methods for Output

Full file at Chapter 2 - Inheritance and Exception Handling

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS 231 Data Structures and Algorithms, Fall 2016

Programming II (CS300)

Programming overview

COMP-202 Unit 9: Exceptions

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Classes Basic Overview

COP 3330 Final Exam Review

Exceptions and Libraries

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Programming in the Large II: Objects and Classes (Part 1)

Programming II (CS300)

Abstract Classes, Exceptions

1 Shyam sir JAVA Notes

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output

Computer Components. Software{ User Programs. Operating System. Hardware

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

More on Objects in JAVA TM

BBM 102 Introduction to Programming II Spring Exceptions

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

F I N A L E X A M I N A T I O N

Pace University. Fundamental Concepts of CS121 1

Introducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step

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

Introduction to Programming Using Java (98-388)

G Programming Languages - Fall 2012

CSCI 261 Computer Science II

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Introduction Unit 4: Input, output and exceptions

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

JAVA MOCK TEST JAVA MOCK TEST II

c) And last but not least, there are javadoc comments. See Weiss.

Identifiers and Variables

Chapter 11 Introduction to Programming in C

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS11 Java. Fall Lecture 1

CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

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

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

COMP 202 Java in one week

CS 2110 AEW. Machine Language. Outline. Assembly Language. High-Level Language 1/26/2012

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

National University. Faculty of Computer Since and Technology Object Oriented Programming

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

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

Lecture 02, Fall 2018 Friday September 7

CSCI 355 LAB #2 Spring 2004

CS 113 PRACTICE FINAL

9 Working with the Java Class Library

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Inheritance (Part 5) Odds and ends

CS 11 java track: lecture 3

Lecture 5: Methods CS2301

Java and OOP. Part 5 More

CS260 Intro to Java & Android 03.Java Language Basics

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Java Threads and intrinsic locks

Fundamentals of Object Oriented Programming

Java for Non Majors Spring 2018

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

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

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

CSCI 355 Lab #2 Spring 2007

Exception-Handling Overview

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Chapter 11 Introduction to Programming in C

Chapter 7 User-Defined Methods. Chapter Objectives

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

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

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

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Super-Classes and sub-classes

6.Introducing Classes 9. Exceptions

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

C a; C b; C e; int c;

CS-202 Introduction to Object Oriented Programming

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

What is Inheritance?

Today s plan Discuss the Bb quiz 1 Clarify Lab 1 Review basic Java materials Classes, Objects, Interfaces Strings Java IO. Understanding Board

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 1 Getting Started

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

MODULE 02: BASIC COMPUTATION IN JAVA

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Transcription:

1

COMP 250: Java Reference Types & Object Oriented Programming January 19, 2018 Carlos G. Oliver Slides adapted from M. Blanchette 2

[Downey Ch 3, 6.1-6.4] Methods Method (a.k.a. function, procedure, or routine): Piece of code that carries a specific computation Can be called (executed) from anywhere in the code (if they are public) Can take one or more parameters (arguments) as input Can return a value (or an array, or any object) public static float square( float x ) { float s = x*x; return s; Local variables: Variables declared inside a method (e.g. s). They are discarded after the method finishes being executed. 3

public class Course{ // prints welcoming statement. Takes no arguments. Returns nothing public static void printwelcome() { System.out.println("Welcome to COMP 250"); // prints welcoming statement for the given courseid. Returns nothing public static void printwelcome(int courseid) { System.out.println("Welcome to COMP + courseid); // returns the letter grade for the given percent grade public static char getgradefrompercent( double percent) { char grade; if ( percent >= 0.8 ) grade = 'A'; if ( percent >= 0.7 && percent < 0.8 ) grade = 'B'; if (percent < 0.7) grade = C ; return grade; public static void main (String args[]) { printwelcome(); printwelcome( 203 ); char g = getgradefrompercent(0.67); System.out.println("The grade is " + g); grade = A ; // compilation error: grade was only defined inside // getgadefrompercent method 4

Why are methods useful? Code re-use: a method can be called (executed) as often as we want, from anywhere in the program. No need to duplicate code. Encapsulation: Allows to think of a piece of code as a black box with a well-defined function. Users don t need to know how the method works, only what the method does: what are its arguments, what does it return. Makes program much easier to design, understand and debug 5

Parameter passing // Returns area a circle of radius r static double circlearea(double r) { double a = 3.1416 * r * r ; r = -1 ; // just to see what happens return a ; Memory (stack) public static void main(string args[]) { double radius = 2; double area = circlearea(radius); System.out.println("Radius: + radius + " Area: + area ); Output: Radius: 2 Area:12.5664 6

The truth about parameter passing What happens when a method is called? 1. The flow of execution of the code calling the method is interrupted. 2. If the methods takes some arguments, these arguments are allocated in memory (stack). They are initialized with the value of the arguments provided by the caller. 3. If variables are declared within the method, they are also put on the stack. 4. The code of the method is executed. This may include calling other methods. 5. When the code of the method has been executed, it may return a value to the caller. All local variables and arguments created on the stack are discarded. Summary: Parameters are passed by value The method called receives a copy of the parameters passed Since it is working a copy, the method can't change the original But watch out with arrays and non-primitive types... 7

static void stupidincrement( int a ) { int i = a; i = i + 1; System.out.println( In stupidincrement, i = + i); static void fakeassign( int a, int b) { a = b; System.out.println( In fakeassign, a = + a + and b = + b ); static int add(int a, int b) { int sum = a + b; a = 0; return sum; static public void main(string args[]) { int a = 1, b = 2, i = 9; fakeassign( a, b ); System.out.println( After fakeassign a: + a + b: + b + i: + i); stupidincrement(b); System.out.println( After stupid a: + a + b: + b + i: + i); stupidincrement(i); System.out.println( Again after stupid a: + a + b: + b + i: + i); a = add(i, a); System.out.println( After add a: + a + b: + b + i: + i); System.out.println( sum = + sum); // this causes an compilation error 8 // because sum is only defined inside add

Output: In assign, a =2 and b = 2 AfterfakeAssign a:1 b:2 c:9 // because in fakeassign, we were working // only on copies of the original a and b In stupidincrement, i = 3 After stupidincrement, a: 1 b: 2 i: 9 // the variable i used in // fakeassign has nothing to do // with the variable i defined in main In stupidincrement, i = 10 Again after stupidincrement a: 1 b: 2 i: 9 After add a: 10 b: 2 i: 9 9

Parameter passing with arrays static void changearray( int a[] ) { System.out.println( First, a[0] is + a[0]); a[0]=2; System.out.println( Then, a[0] is " + a[0]); a = new int[2]; a[0]=3; System.out.println("Then, a[0] is " + a[0]); public static void main(string args[]) { int[] array; array = new int[3]; array[0] = 1; changearray(array); System.out.println("Finally, array[0] is " + array[0] ); 10

Memory (stack) Memory (heap) 11

Strings [Downey Ch 8] Strings store sequences of characters Strings behave just like arrays (but they re more than that) String s; String s = "Hello"; char c = s.charat(1); // c is 'e' int l=s.length(); // l is 5 // s is a reference to a String. Currently, it s a null String String t = s.substring(1,3); // t is a new string with "el" String u = "Hello"; if (u==s) System.out.println("they are =="); // won't be printed if (s.equalsto(u)) System.out.println("They are equalsto"); // this will be Complete description of String operations: https://docs.oracle.com/javase/7/docs/api 12

[Downey Appendix B] Input/Output Java has a large number of ways to read in and write out data. We will use only the most basic. To import IO libraries, start your code with: import java.io.*; // this should be the first line of your code To read data from keyboard: // First open a stream from which data will be read BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in) ); System.out.println("Enter your name:"); String name = keyboard.readline(); // reads one line from the keyboard System.out.println("Enter your age:"); String agestring = keyboard.readline(); int age = Integer.parseInt(ageString); // convert the string into an integer keyboard.close(); // close the stream when we are done 13

[Downey Appendix B] Input To read data from file named "myfile.txt": BufferedReader myfile = new BufferedReader(new FileReader( "myfile.txt ) ); String line = myfile.readline(); To read from an URL: URL mcgill= new URL( "http://www.cs.mcgill.ca ); URLConnection mcgillconn = mcgill.openconnection(); BufferedReader myurl = new BufferedReader(new InputStreamReader( mcgillconn.getinputstream () ) ); String header = myurl.readline(); 14

[Downey Appendix B] Output To write data to a file named "myoutput.txt": BufferedWriter myfile = new BufferedWriter(new FileWriter( "myoutput.txt ) ); String line="hello my friends!"; myfile.writeline(line);... myfile.close(); Good tutorial on IO: http://java.sun.com/docs/books/tutorial/essential/io/ Full documentation: https://docs.oracle.com/javase/7/docs/api 15

Programming style and comments How? Choose meaningful names for methods and variables. Stick to your conventions. e.g. int nbsides; getplayerslist(montrealexpos) Add comments to clarify any piece of code whose function is not obvious Give a short description of each method: what does it do? what arguments does it expect? what assumptions are made? what does it return? Side-effects? Do not overcomment! Why? Makes re-use easier (even for you!) Makes finding and solving bugs easier Allows others to use your code Easier to convince your boss (or TA!) that your code is working Easier to analyze the efficiency of the solution 16

Object-Oriented Programming (OOP) Idea: User-defined types to complement primitive types like int, float... Definition of a new type is called a class. It contains: Data Methods: Code for performing operations on this data Example: the class String contains Data: Sequence of characters Operations: capitalize, substring, compare... Example: we could define a class Matrix with Data: an m x n array of numbers Operations: multiply, invert, determinant, etc. 17

Why OOP? Think of a set of classes as a toolbox: You know what each tool does You don't care how it does it OOP allows to think more abstractly: Each class has a well defined interface We can think in terms of functionality rather than in terms of implementation The creator of a class can implement it however he/she wants, as long the class fulfills the specification of the interface 18

A first example // The new type created is called SportTeam class SportTeam { // The class has four members String hometown; int victories, losses, points; public static void main(string[] args) { // we can declare variables of type SportTeam SportTeam expos; // this creates an object of typesportteam and expos now references it. expos = new SportTeam(); expos.victories = 62; expos.hometown = "Montreal"; SportTeam alouettes = new SportTeam(); alouettes.victories = 11; 19

class SportTeam { String hometown; int victories, losses, points; // Constructors are methods used to initialize members of the class public SportTeam() { // constructors are declared with no return type. victories=losses=points=0; hometown=new String("Unknown"); // Constructors can have arguments public SportTeam(String town) { victories=losses=points=0; hometown=town; public static void main(string[] args) { // now we can declare variables of type SportTeam SportTeam expos, alouettes; expos = new SportTeam(); alouettes = new SportTeam("Montreal"); 20

public class SportTeam { String hometown; int victories, losses, points; public SportTeam() { /* see previous page */ public SportTeam(String town) { /* see previous page */ // this method returns a string describing the SportTeam public String tostring() { return hometown + " : " + victories + " victories, " + losses + " losses, for " + points + " points."; public static void main(string[] args) { // now we can declare variables of type SportTeam SportTeam expos, alouettes; expos = new SportTeam(); alouettes = new SportTeam("Montreal"); expos.victories=62; alouettes.victories = expos.victories - 52; String report = alouettes.tostring(); System.out.println(report); 21

22

Private vs public We don't want to let any part of a program access members of a class It might disrupt the internal consistency of the object (e.g. one may increase the number of victories without increasing the number of points) We want to hide as much as possible the inside of a class, to enforce abstraction. Solution: Make these members private (they can only be used from inside the class) Allow access to these members only through predefined public methods 23

public class SportTeam { public String hometown; // can be changed from within any class private int victories, losses, points; // can only be changed from within // the SportTeam public SportTeam() { /* see previous page */ public SportTeam(String town) { /* see previous page */ public String tostring() { /* see previous page */ public void addwin() { victories++; points+=2; public static void main(string[] args) { // now we can declare variables of type SportTeam SportTeam expos, alouettes; expos = new SportTeam(); alouettes = new SportTeam("Montreal"); alouettes.addwin(); String report = alouettes.tostring(); 24

public class SportTeam {... (from previous slides) public class League { int nbteams; public SportTeam teams[]; // an array of SportTeam League(int n) { // constructor nbteams = n; for (int i = 0 ; i < n ; i++ ) teams[i] = new SportTeam(); public static void main(string args[]) { League NHL = new league(30); NHL.teams[0].hometown = Montreal ; NHL.teams[0].addWin(); 25

Assignments and equality testing Non-primitive types are just references to objects! public static void main(string[] args) { SportTeam expos, alouettes; SportTeam baseball, football; expos = new SportTeam(); alouettes = new SportTeam("Montreal"); alouettes.addwin(); baseball = new SportTeam(); football = alouettes; if ( expos == baseball ) System.out.println("expos == baseball"); if ( football == alouettes ) System.out.println("alouettes == football"); football.addwin(); System.out.println(alouettes.toString()); System.out.println(football.toString()); football = new SportTeam("Toronto"); System.out.println(alouettes.toString()); System.out.println(football.toString()); 26

This Sometimes, it can be useful for an object to refer to itself: the this keyword refers to the current object We could rewrite the constructor as: public SportTeam() { this.victories = this.losses = this.points = 0; this.hometown = new String("Unknown"); If there was a league object that needed to be updated: league.addteam(this); 27

Static members Normally, each object has its own copy of all the members of the class, but... Sometimes we want to have members that shared by all objects of a class The static qualifier in front of a member (or method) means that all objects of that class share the same member 28

public class SportTeam { public String hometown; private int victories, losses, points; static public double exchangerate; public SportTeam() { /* see previous page */ public SportTeam(String town) { /* see previous page */ public String tostring() { /* see previous page */ public addwin() { /* see previous page */ public static void main(string[] args) { // now we can declare variables of type SportTeam SportTeam expos, alouettes; SportTeam.exchangeRate = 1.57; expos = new SportTeam(); alouettes = new SportTeam("Montreal"); expos.exchangerate = 1.58; /* all objects of type SportTeam share the same exchangerate */ /* static members can be used without an actual object */ // or from one particular object System.out.println("Rate from expos: " + expos.exchangerate); System.out.println("Rate from alouettes: " + alouettes.exchangerate); 29

Inheritance Suppose you need to write a class X whose role would be very similar to an existing class Y. You could Rewrite the whole code anew Time consuming, introduces new bugs, makes maintenance a headache Copy the code of Y into X, then make your changes Maintenance problem: you need to maintain both X and Y Inherit the code from Y, but override certain methods Code common to X and Y is kept in Y. New methods are added in X 30

Inheritance - Example You want to extend SportTeam to make it specific to certain sports HockeyTeam Has all the members defined in sporttean, but also number of ties. Number of points = 3 * victories + 1 * ties BaseballTeam Has all the members defined in SportTeam, but also number of homeruns 31

SportTeam (parent class) Data: hometown, victories, losses, points Methods: tostring, addwin HockeyTeam (subclass of SportTeam) Data: Same as parent + ties Methods: Same as parent but new addwin, addtie BaseballTeam (subclass of SportTeam) Data: Same as parent + homeruns Methods: Same as parent ProfessionalHockeyTeam (subclass of HockeyTeam) Data: Same as parent + salaries Methods: Same as parent + sellto 32

public class HockeyTeam extends SportTeam { private int ties; public HockeyTeam() { // constructor for HockeyTeam super(); // super() calls the constructor of the superclass ties=0; public void addwin() { super.addwin(); /* This calls the addwin method provided by the parent class */ points++; /* Since points is private, this wouldn't compile. We need to declare points as "protected" instead of private to allow access to subclasses */ public void addtie() { ties++; points++; 33

Types and dispatch public static void main(string args[]) { HockeyTeam habs; habs = new HockeyTeam(); habs.hometown = "Montreal"; habs.addwin(); /* The addwin method called is the one from HockeyTeam. habs.points is 3*/ habs.addtie(); // ties is now 1, points is 4 System.out.println(habs.toString()); /* HockeyTeam doesn't provide a tostring() method but SportTeam does, so that's the one called */ SportTeam bruins = new HockeyTeam(); /* this is legal because HockeyTeam is a subtype of SportTeam */ bruins.addwin(); // bruins.points is now 3 HockeyTeam leafs = new SportTeam(); /* this is NOT legal because SportTeam is not a subtype of HockeyTeam */ 34

Exceptions - When things go wrong Some things are outside programmer's control: User types "Go expos" when asked to enter number of victories Try to open a file that doesn't exist Try to compute sqrt(-1)... Exception mechanism allows to deal with these situations gracefully When problem is detected, the code throws an exception The execution of the program stops. JVM looks for somebody to catch the exception The code that catches the exception handles the problem, and execution continues from there If no code catches exception, the program stops with error message An exception is an object that contains information about what went wrong. 35

Throwing exceptions Syntax: try { <block of code> catch (exceptiontype1 e1) { <block of code> catch (exceptiontype2 e2) { <block of code>... finally { <block of code> static double mysqrt(double x) { try { if (x<=0) throw new ArithmeticException( Sqrt is defined only for positive numbers"); /* Code for computing sqrt goes here */ catch (ArithmeticException e) { System.out.println("The mysqrt operation failed with error: + e ); return 0; 36

Methods throwing exceptions Sometimes, it is not appropriate for a method to handle the exception it threw Methods can throw exceptions back to the caller: static double mysqrt(double x) throws ArithmeticException { if (x<0) { throw new ArithmeticException( Sqrt of " + x + " is not defined"); /* Code for computing sqrt goes here */ public static void main(string args[]) { double x = 0, y = 0, z = 0 ; try { x = mysqrt(10); y = mysqrt(-2); z = mysqrt(100); catch ( ArithmeticException e ) { System.out.println(e.toString()); // what is the value of x, y, z now? // x is 1, y and z are zero 37

Java resources Java Application Programming Interface (API) http://docs.oracle.com/javase/7/docs/api// Java books: 1594 different books on Amazon The Java Programming Language -- by Ken Arnold (Author), et al; By the authors of Java itself. The ultimate reference. Not easy to read for beginners. Java in a Nutshell, Fourth Edition, by David Flanagan A text version of the Java API 38