class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; void movevertically(int y){ this.y += y;

Size: px
Start display at page:

Download "class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; void movevertically(int y){ this.y += y;"

Transcription

1 Call b Value (2.1) Aggregation and Comosition EECS200 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG For illustration, let s assume the following variant of the class: class { int ; int ; (int, int ) { this. = ; this. = ; void moveverticall(int ){ this. += ; void movehorizontall(int ){ this. += ; of Call b Value (1) Consider the general form of a call to some mutator method m1, with contet object s and argument value arg: class Sulier { void m1( T ar) { / maniulate ar / class Client { Sulier s = new Sulier(); T arg =...; s.m1(arg) To eecute s.m1(arg), an imlicit ar := arg is done. A co of value stored in arg is assed for the method call. What can the te T be? [ Primitive or Reference ] T is rimitive te (e.g., int, char, boolean, etc.): Call b Value : Co of arg s value (e.g., 2, j ) is assed. T is reference te (e.g., String,, Person, etc.): Call b Value : Co of arg s stored reference/address is assed. 2 of Call b Value (2.2.1) ublic class Util { void reassignint(int j) { j = j + 1; void reassignref( q) { n = new (, 8); q = n; void changeviaref( q) { q.movehorizontall(); q.moveverticall(); 1 2 ublic void testcallbval() { Util u = new Util(); int i = 10; 5 asserttrue(i == 10); u.reassignint(i); 7 asserttrue(i == 10); 8 Before the mutator call at L, rimitive variable i stores 10. When eecuting the mutator call at L, due to call b value,a co of variable i is made. The assignment i = i + 1 is onl effective on this co, not the original variable i itself. After the mutator call at L, variable i still stores 10. of

2 Call b Value (2.2.2) Call b Value (2..2) Before reassignint During reassignint After reassignint i int 10 i int 10 Before reassignref During reassignref After reassignref i int 10 j int 10 j int 11 q q 8 5 of 7 of Call b Value (2..1) ublic class Util { void reassignint(int j) { j = j + 1; void reassignref( q) { n = new (, 8); q = n; void changeviaref( q) { q.movehorizontall(); q.moveverticall(); 1 2 ublic void testcallbref_1() { Util u = new Util(); = new (, ); 5 refofpbefore = ; u.reassignref(); 7 asserttrue(==refofpbefore); 8 asserttrue(.== &&.==); 9 Before the mutator call at L, reference variable stores the address of some object (whose is and is ). When eecuting the mutator call at L, due to call b value,a co of address stored in is made. The assignment = n is onl effective on this co, not the original variable itself. After the mutator call at L, variable still stores the original address (i.e., same as refofpbefore). of Call b Value (2..1) ublic class Util { void reassignint(int j) { j = j + 1; void reassignref( q) { n = new (, 8); q = n; void changeviaref( q) { q.movehorizontall(); q.moveverticall(); 1 2 ublic void testcallbref_2() { Util u = new Util(); = new (, ); 5 refofpbefore = ; u.changeviaref(); 7 asserttrue(==refofpbefore); 8 asserttrue(.== &&.==8); 9 Before the mutator call at L, reference variable stores the address of some object (whose is and is ). When eecuting the mutator call at L, due to call b value,a co of address stored in is made. [Alias: and q store same address.] Calls to q.movehorizontall and q.moveverticall are effective on both and q. After the mutator call at L, variable still stores the original address (i.e., same as refofpbefore), but its and have been modified via q. 8 of

3 Call b Value (2..2) Shared b Containers (1.1) Before changeviaref During changeviaref After changeviaref 8 q q 9 of Course Facult rof; Course(String title) { this.title = title; void setprof(facult rof) { this.rof = rof; Facult getprof() { return this.rof; 11 of rof 1 Facult String ; Facult(String ) { this. = ; void setname(string ) { this. = ; String getname() { return this.; Aggregation vs. Comosition: Terminolog Container object: an object that contains others. Containee object: an object that is contained within another. e.g., Each course has a facult member as its instructor. Container: Course Containee: Facult. e.g., Each student is registered in a list of courses; Each facult member teaches a list of courses. Container: Student, Facult Containees: Course. e.g., eecs200 taken b jim (student) and taught b tom (facult). Containees ma be shared b different instances of containers. e.g., When EECS200 is finished, jim and jackie still eist! Containees ma eist indeendentl without their containers. e.g., In a file sstem, each director contains a list of. Container: Director Containees:. e.g., Each file has eactl one arent director. A containee ma be owned b onl one container. e.g., Deleting a director also deletes the it contains. Containees ma co-eist with their containers. 10 of Shared b Containers (1.2) ublic void testaggregation1() { Course eecs200 = new Course("Advanced OOP"); Course eecs11 = new Course("Software Design"); Facult rof = new Facult("Jackie"); eecs200.setprof(rof); eecs11.setprof(rof); asserttrue(eecs200.getprof() == eecs11.getprof()); / aliasing / rof.setname("jeff"); asserttrue(eecs200.getprof() == eecs11.getprof()); asserttrue(eecs200.getprof().getname().equals("jeff")); Facult rof2 = new Facult("Jonathan"); eecs11.setprof(rof2); asserttrue(eecs200.getprof()!= eecs11.getprof()); asserttrue(eecs200.getprof().getname().equals("jeff")); asserttrue(eecs11.getprof().getname().equals("jonathan")); 12 of

4 Shared b Containers (2.1) The Dot Notation (.1) In real life, the relationshis among classes are sohisticated. Student cs Course te Facult String id; ArraList<Course> cs; / courses / Student(String id) { this.id = id; cs = new ArraList<>(); void addcourse(course c) { cs.add(c); ArraList<Course> getcs() { return cs; Student String id; ArraList<Course> cs; cs Course te Facult rof; Facult String ; ArraList<Course> te; String ; ArraList<Course> te; / teaching / Facult(String ) { this. = ; te = new ArraList<>(); void addteaching(course c) { te.add(c); ArraList<Course> gette() { return te; 1 of Aggregation links between classes constrain how ou can navigate among these classes. e.g., In the contet of class Student: Writing cs denotes the list of registered courses. Writing cs[i] (where i is a valid inde) navigates to the class Course, which changes the contet to class Course. 15 of Shared b Containers (2.2) ublic void testaggregation2() { Facult = new Facult("Jackie"); Student s = new Student("Jim"); Course eecs200 = new Course("Advanced OOP"); Course eecs11 = new Course("Software Design"); eecs200.setprof(); eecs11.setprof();.addteaching(eecs200);.addteaching(eecs11); s.addcourse(eecs200); s.addcourse(eecs11); asserttrue(eecs200.getprof() == s.getcs().get(0).getprof()); asserttrue(s.getcs().get(0).getprof() == s.getcs().get(1).getprof()); asserttrue(eecs11 == s.getcs().get(1)); asserttrue(s.getcs().get(1) ==.gette().get(1)); 1 of The Dot Notation (.2) String id; ArraList<Course> cs; Facult rof; String ; ArraList<Course> te;... / attributes / / Get the student s id / String getid() { return this.id; / Get the title of the ith course / String getcoursetitle(int i) { return this.cs.get(i).title; / Get the instructor s of the ith course / String getinstructorname(int i) { return this.cs.get(i).rof.; 1 of

5 The Dot Notation (.) String id; ArraList<Course> cs; Facult rof; String ; ArraList<Course> te;... / attributes / / Get the course s title / String gettitle() { return this.title; / Get the instructor s / String getinstructorname() { return this.rof.; / Get title of ith teaching course of the instructor / String getcoursetitleofinstructor(int i) { return this.rof.te.get(i).title; 17 of Owned b Containers (1.1) Director arent 1 Assumtion: s are not shared among directories. class { String ; (String ) { this. = ; 19 of class Director { String ; [] ; int ; / num of / Director(String ) { this. = ; = new [100]; void add(string filename) { [] = new (filename); ++; The Dot Notation (.) String id; ArraList<Course> cs; Facult rof;... / attributes / / Get the instructor s / String getname() { return this.; / Get the title of ith teaching course / String getcoursetitle(int i) { return this.te.get(i).title; 18 of String ; ArraList<Course> te; Owned b Containers (1.2.1) 1 2 ublic void testcomosition() { Director d1 = new Director("D"); d1.add("f1.tt"); 5 d1.add("f2.tt"); d1.add("f.tt"); 7 asserttrue( 8 d1.[0]..equals("f1.tt")); 9 L: 1st object is created and owned eclusivel b d1. No other directories are sharing this object with d1. L5: 2nd object is created and owned eclusivel b d1. No other directories are sharing this object with d1. L: rd object is created and owned eclusivel b d1. No other directories are sharing this object with d1. 20 of

6 Owned b Containers (1.2.2) d1 Right before test method testcomosition terminates: 21 of Director D d d1.[0] d1.[1] d1.[2] 2 f1.tt f2.tt f.tt Owned b Containers (1..1) Version 1: Shallow Co b coing all attributes using =. class Director { Director(Director other) { / value coing for rimitive te / = other.; / address coing for reference te / = other.; = other.; Is a shallow co satisfactor to suort comosition? i.e., Does it still forbid sharing to occur? [ NO ] void testshallowcoconstructor() { Director d1 = new Director("D"); d1.add("f1.tt"); d1.add("f2.tt"); d1.add("f.tt"); Director d2 = new Director(d1); asserttrue(d1. == d2.); / violation of comosition / d2.[0].changename("f11.tt"); assertfalse(d1.[0]..equals("f1.tt")); 2 of Owned b Containers (1.) Problem: Imlement a co constructor for Director. A co constructor is a constructor which initializes attributes from the argument object other. class Director { Director(Director other) { / Initialize attributes via attributes of other. / Hints: The imlementation should be consistent with the effect of coing and asting a director. Searate coies of are created. 22 of Owned b Containers (1..2) d2 d1 Right before test method testshallowcoconstructor terminates: 2 of Director Director d2. D d d1.[0] d1.[1] d1.[2] d2.[0] d2.[1] d2.[2] d2. 2 f11.tt f2.tt f.tt

7 Owned b Containers (1.5.1) Version 2: a Dee Co class { ( other) { this. = new String(other.); class Director { Director(String ) { this. = new String(); = new [100]; Director(Director other) { this (other.); for(int i = 0; i < ; i ++) { src = other.[i]; nf = new (src); this.add(nf); void add( f) {... void testdeecoconstructor() { Director d1 = new Director("D"); d1.add("f1.tt"); d1.add("f2.tt"); d1.add("f.tt"); Director d2 = new Director(d1); asserttrue(d1.!= d2.); / comosition reserved / d2.[0].changename("f11.tt"); asserttrue(d1.[0]..equals("f1.tt")); 25 of Owned b Containers (1.5.) Q: Comosition Violated? class { ( other) { this. = new String(other.); class Director { Director(String ) { this. = new String(); = new [100]; Director(Director other) { this (other.); for(int i = 0; i < ; i ++) { src = other.[i]; this.add(src); void add( f) {... void testdeecoconstructor() { Director d1 = new Director("D"); d1.add("f1.tt"); d1.add("f2.tt"); d1.add("f.tt"); Director d2 = new Director(d1); asserttrue(d1.!= d2.); / comosition reserved / d2.[0].changename("f11.tt"); asserttrue(d1.[0] == d2.[0]); / comosition violated! / 27 of Owned b Containers (1.5.2) Right before test method testdeecoconstructor terminates: d2 d1 Director Director D d2. d2.[0] d2.[1] d2.[2] D d2. d d1.[0] d1.[1] d1.[2] f11.tt f2.tt f.tt Owned b Containers (1.) Eercise: Imlement the accessor in class Director class Director { [] ; int ; [] gets() { / Your Task / so that it reserves comosition, i.e., does not allow references of to be shared. 2 of f1.tt f2.tt f.tt 28 of

8 Aggregation vs. Comosition (1) Terminolog: Container object: an object that contains others. Containee object: an object that is contained within another. Aggregation : Containees (e.g., Course) ma be shared among containers (e.g., Student, Facult). Containees eist indeendentl without their containers. When a container is destroed, its containees still eist. Comosition : Containers (e.g, Director, Deartment) own eclusive access to their containees (e.g.,, Facult). Containees cannot eist without their containers. Destroing a container destros its containeees cascadingl. 29 of Inde (1) Call b Value (1) Call b Value (2.1) Call b Value (2.2.1) Call b Value (2.2.2) Call b Value (2..1) Call b Value (2..2) Call b Value (2..1) Call b Value (2..2) Aggregation vs. Comosition: Terminolog Shared b Containers (1.1) Shared b Containers (1.2) Shared b Containers (2.1) 1 of Aggregation vs. Comosition (2) Aggregations and Comositions ma eist at the same time! e.g., Consider a workstation: Each workstation owns CPU, monitor, keword. [ comositions ] All workstations share the same network. [ aggregations ] 0 of Inde (2) Shared b Containers (2.2) The Dot Notation (.1) The Dot Notation (.2) The Dot Notation (.) The Dot Notation (.) Owned b Containers (1.1) Owned b Containers (1.2.1) Owned b Containers (1.2.2) Owned b Containers (1.) 2 of

9 Inde () Owned b Containers (1..1) Owned b Containers (1..2) Owned b Containers (1.5.1) Owned b Containers (1.5.2) Owned b Containers (1.5.) Owned b Containers (1.) Aggregation vs. Comosition (1) Aggregation vs. Comosition (2) of

Aggregation and Composition

Aggregation and Composition Aggregation and Composition EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Call by Value (1) Consider the general form of a call to some mutator method m1, with context object

More information

Object Orientation: Observe, Model, and Execute. Classes and Objects. Object-Oriented Programming (OOP) Separation of Concerns: App vs.

Object Orientation: Observe, Model, and Execute. Classes and Objects. Object-Oriented Programming (OOP) Separation of Concerns: App vs. Classes and Objects Readings: Chapters 4 of the Course Notes Object Orientation: Observe, Model, and Execute Real World: Entities Compile-Time: Classes (definitions of templates) Run-Time: Objects (instantiations

More information

EECS1022 Winter 2018 Additional Notes Tracing Point, PointCollector, and PointCollectorTester

EECS1022 Winter 2018 Additional Notes Tracing Point, PointCollector, and PointCollectorTester EECS1022 Winter 2018 Additional Notes Tracing, Collector, and CollectorTester Chen-Wei Wang Contents 1 Class 1 2 Class Collector 2 Class CollectorTester 7 1 Class 1 class { 2 double ; double ; 4 (double

More information

Object Orientation: Observe, Model, and Execute. Classes and Objects. Object-Oriented Programming (OOP)

Object Orientation: Observe, Model, and Execute. Classes and Objects. Object-Oriented Programming (OOP) Classes and Objects Object Orientation: Observe, Model, and Execute Real World: Entities Compile-Time: Classes (definitions of templates) Run-Time: Objects (instantiations of templates) Entities: jim,

More information

Classes and Objects. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Classes and Objects. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Classes and Objects EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Separation of Concerns: App/Tester vs. Model In EECS1022: Model Component: One or More Java Classes e.g., Person

More information

Encapsulation in Java

Encapsulation in Java Encapsulation in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Encapsulation (1.1) Consider the following problem: A person has a name, a weight, and a height. A person s

More information

Case Study: Modelling using Objects

Case Study: Modelling using Objects E H U N I V E R S I T Y T O H F R G Case Study: Modelling using Objects E D I N B U Murray Cole Modelling the world with data 1 Specifying systems is one of the hardest tasks in SE Key issues in object-oriented

More information

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

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I Object-Oriented Programming (OOP) Basics CSCI 161 Introduction to Programming I Overview Chapter 8 in the textbook Building Java Programs, by Reges & Stepp. Review of OOP History and Terms Discussion of

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type Variables of a Class

More information

Objects as a programming concept

Objects as a programming concept Objects as a programming concept IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4:

More information

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

Assignment 1 due Monday at 11:59pm

Assignment 1 due Monday at 11:59pm Assignment 1 due Monday at 11:59pm The heart of Object-Oriented Programming (Now it gets interesting!) Reading for next lecture is Ch. 7 Focus on 7.1, 7.2, and 7.6 Read the rest of Ch. 7 for class after

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Variables and Primitive Types

Variables and Primitive Types CS159 Variables and Primitive Types Java Primitive Types: byte, short, int, long, float, double, boolean, char Example int a; int b; boolean aisless; a = 3; b = 4; aisless = a < b; if (aisless && b < a)

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Week 9 Lab - Use of Classes and Inheritance 8th March 2018 SP1-Lab9-2018.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1 Lab 9: Objectives Exercise 1 Student & StudentTest classes 1.

More information

Instance Method Development Demo

Instance Method Development Demo Instance Method Development Demo Write a class Person with a constructor that accepts a name and an age as its argument. These values should be stored in the private attributes name and age. Then, write

More information

Getter and Setter Methods

Getter and Setter Methods Example 1 namespace ConsoleApplication14 public class Student public int ID; public string Name; public int Passmark = 50; class Program static void Main(string[] args) Student c1 = new Student(); Console.WriteLine("please..enter

More information

How to evaluate a design? How to improve on it? When to stop evaluating and improving?

How to evaluate a design? How to improve on it? When to stop evaluating and improving? How to design code? Steps Analyze the problem (we will cover later) Come up with initial design Evaluate this design (is it good enough?) Improve design & repeat evaluation How to evaluate a design? How

More information

Introduction to UML and Class Diagrams

Introduction to UML and Class Diagrams Introduction to UML and Class Diagrams Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland 2017, T. S. Norvell, A. Vardy 1 / 34 UML Unified Modelling Language

More information

No Inheritance: ResidentStudent Class. Inheritance. Why Inheritance: A Motivating Example. No Inheritance: NonResidentStudent Class

No Inheritance: ResidentStudent Class. Inheritance. Why Inheritance: A Motivating Example. No Inheritance: NonResidentStudent Class No Inheritance: ResidentStudent Class Inheritance EECS23 B: Advanced Object Oriented Programming Fall 218 CHEN-WEI WANG class ResidentStudent { String ; Course[] ; int ; double premiumrate; /* there s

More information

Introduction to UML and Class Diagrams

Introduction to UML and Class Diagrams Introduction to UML and Class Diagrams Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland 1 / 31 UML Unified Modelling Language (UML) UML is a graphical modelling

More information

Using API in Java. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Using API in Java. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Using API in Java EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Understand: Self-Exploration of Java API Method Signature Parameters vs. Arguments Non-Static Methods

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Classes. Code Generation for Objects. Compiling Methods. Dynamic Dispatch. The Need for Dispatching CS412/CS413

Classes. Code Generation for Objects. Compiling Methods. Dynamic Dispatch. The Need for Dispatching CS412/CS413 Classes CS4/CS43 Introduction to Comilers Tim Teitelbaum Lecture : Imlementing Objects 8 March 5 Comonents ields/instance variables values ma dier rom object to object usuall mutable methods values shared

More information

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Practice problem on defining and using Class types. Part 4.

Practice problem on defining and using Class types. Part 4. CS180 Programming Fundamentals Practice problem on defining and using Class types. Part 4. Implementing object associations: Applications typically consist of collections of objects of different related

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 8: Abstract Data Types Object- Oriented Programming Model the applica6on/so9ware as a set of objects

More information

Documenting, Using, and Testing Utility Classes

Documenting, Using, and Testing Utility Classes Documenting, Using, and Testing Utility Classes Readings: Chapter 2 of the Course Notes EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Structure of Project: Packages and Classes

More information

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 12 Arrays: A. Write a method add that receives an array of integers arr, the number of the elements in the

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Inheritance. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Inheritance. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Inheritance EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Why Inheritance: A Motivating Example Problem: A student management system stores data about students. There are two

More information

Object Oriented Relationships

Object Oriented Relationships Lecture 3 Object Oriented Relationships Group home page: http://groups.yahoo.com/group/java CS244/ 2 Object Oriented Relationships Object oriented programs usually consisted of a number of classes Only

More information

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Handout 8 Classes and Objects Continued: Static Variables and Constants. Handout 8 CS603 Object-Oriented Programming Fall 16 Page 1 of 8 Handout 8 Classes and Objects Continued: Static Variables and Constants. 1. Static variable Declared with keyword static One per class (instead

More information

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University User Defined Classes CS 180 Sunil Prabhakar Department of Computer Science Purdue University Announcements Register for Piazza. Deleted post accidentally -- sorry. Direct Project questions to responsible

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I CSE1030 Introduction to Computer Science II Lecture #9 Inheritance I Goals for Today Today we start discussing Inheritance (continued next lecture too) This is an important fundamental feature of Object

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of

More information

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

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been

More information

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders CMSC131 Creating a Datatype Class Continued Exploration of Memory Model Reminders The name of the source code file needs to match the name of the class. The name of the constructor(s) need(s) to match

More information

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency CS 361 Concurrent programming Dreel University Fall 2004 Lecture 6 Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Class Relationships. Lecture 18. Based on Slides of Dr. Norazah Yusof

Class Relationships. Lecture 18. Based on Slides of Dr. Norazah Yusof Class Relationships Lecture 18 Based on Slides of Dr. Norazah Yusof 1 Relationships among Classes Association Aggregation Composition Inheritance 2 Association Association represents a general binary relationship

More information

Simple example. Analysis of programs with pointers. Points-to relation. Program model. Points-to graph. Ordering on points-to relation

Simple example. Analysis of programs with pointers. Points-to relation. Program model. Points-to graph. Ordering on points-to relation Simle eamle Analsis of rograms with ointers := 5 tr := @ *tr := 9 := rogram S1 S2 S3 S4 deendences What are the deendences in this rogram? Problem: just looking at variable names will not give ou the correct

More information

Recitation 09/12/2007. CS 180 Department of Computer Science, Purdue University

Recitation 09/12/2007. CS 180 Department of Computer Science, Purdue University Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University Announcements & Reminders Project 1 grades out Remember to collect them at the end of the recitation Solution up on the web

More information

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University User Defined Classes Part 2 CS 180 Sunil Prabhakar Department of Computer Science Purdue University Class vs. Instance methods n Compare the Math and String class methods that we have used: Math.pow(2,3);

More information

Python Class Definition. Java Class Definition. Python Class Use public class Student { Java Class Use. Python Class Definition 10/6/2016

Python Class Definition. Java Class Definition. Python Class Use public class Student { Java Class Use. Python Class Definition 10/6/2016 Python 2 Java Python Class Use jim = Student( Jim, 95) print jim.getname() k = jim.getgrade() jim.setgrade(k + 5) Java Class Use Student jim = new Student( Jim, 95); Sytem.out.println(jim.getName()); int

More information

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria CS152 Programming Language Paradigms Prof. Tom Austin, Fall 2014 Syntax & Semantics, and Language Design Criteria Lab 1 solution (in class) Formally defining a language When we define a language, we need

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا Tutorial 6 CSC 201 Java Programming Concepts مبادئ الربجمة باستخدام اجلافا Chapter 6: Classes and Objects 1. Classes & Objects What is an object? Real Objects Java Objects Classes Defining a class and

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that

More information

Lesson 12: OOP #2, Accessor Methods (W03D4)

Lesson 12: OOP #2, Accessor Methods (W03D4) Lesson 12: OOP #2, Accessor Methods (W03D4) Balboa High School Michael Ferraro September 3, 2015 1 / 29 Do Now In your driver class from last class, create another new Person object with these characteristics:

More information

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Test 2 Question Max Mark Internal

More information

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks? Types in Java 8 Primitive Types byte, short, int, long float, double boolean Char Object types: predefined e.g. String ; User-defined via class and constructors What if we want to store lots of items e.g.

More information

Abstract Data Types (ADT) and C++ Classes

Abstract Data Types (ADT) and C++ Classes Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1

More information

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

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Classes & Objects Topics Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Object-Oriented Programming Classes combine data and the methods (code) to manipulate the

More information

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6 CPS109 Midterm Test Fall 05 page 1 of 6 Last Name First Name Student Number Circle Your Instructor Your last name here Your first name here Your student number here Ferworn Harley Instructions: (a) There

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

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

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003 Data abstractions: ADTs Invariants, Abstraction function Lecture 4: OOP, autumn 2003 Limits of procedural abstractions Isolate implementation from specification Dependency on the types of parameters representation

More information

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

CMSC 132: Object-Oriented Programming II. Inheritance

CMSC 132: Object-Oriented Programming II. Inheritance CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:

More information

Introduction to Java

Introduction to Java Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1 Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn

More information

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 SPRING 2017 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 CMS VideoNote.com, PPT slides, DrJava, Book 2 CMS available. Visit course webpage, click Links, then

More information

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design Electrical and Computer Engineering Object-Oriented in Java Topic : Objects and Classes (cont) Object Oriented Maj Joel Young Joel.Young@afit.edu 11-Sep-03 Maj Joel Young Using Class Instances Accessing

More information

CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Exam, Dec 6, 2011

CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Exam, Dec 6, 2011 CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Eam, Dec 6, 2011 ID Name This eam consists of 22 questions. Each question has four options, eactl one of which is correct,

More information

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty The Stack ADT Stacks and Queues ADT Data Structure Interface add() remove() find() request result EECS200: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Accessors top Mutators push pop of

More information

CS 335 Lecture 02 Java Programming

CS 335 Lecture 02 Java Programming 1 CS 335 Lecture 02 Java Programming Programming in Java Define data Calculate using data Output result Java is object-oriented: Java program must: Merge data and functions into object Invoke functions

More information

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

Recitation 02/02/07 Defining Classes and Methods. Chapter 4 Recitation 02/02/07 Defining Classes and Methods 1 Miscellany Project 2 due last night Exam 1 (Ch 1-4) Thursday, Feb. 8, 8:30-9:30pm PHYS 112 Sample Exam posted Project 3 due Feb. 15 10:00pm check newsgroup!

More information

Based on slides by Prof. Burton Ma

Based on slides by Prof. Burton Ma Based on slides by Prof. Burton Ma 1 The terms aggregation and composition are used to describe a relationship between objects Both terms describe the has-a relationship The university has-a collection

More information

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory Types and Variables We write code like: int x = 512; int y = 200; int z = x+y; The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory The compiler

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill CSCI 136 Data Structures & Advanced Programming Lecture 3 Fall 2018 Instructors: Bill & Bill Administrative Details Lab today in TCL 217a (and 216) Lab is due by 11pm Sunday Lab 1 design doc is due at

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill CSCI 136 Data Structures & Advanced Programming Lecture 3 Fall 2017 Instructors: Bill & Bill Administrative Details Lab today in TCL 216 (217a is available, too) Lab is due by 11pm Sunday Copy your folder

More information

Last Name: Circle One: OCW Non-OCW

Last Name: Circle One: OCW Non-OCW First Name: AITI 2004: Exam 1 June 30, 2004 Last Name: Circle One: OCW Non-OCW Read Instructions Carefully! This is a 3 hour closed book exam. No calculators are allowed. Please write clearly if we cannot

More information

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY Department of Computer Science & Engineering COMP 152: Object-Oriented Programming and Data Structures Fall 2010 Midterm Examination Instructor: Gary Chan

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

CS151 Principles of Computer Science I Fall 2018 Homework 6 S

CS151 Principles of Computer Science I Fall 2018 Homework 6 S 1. Exercise 19 : double Exercise 20 : String CS151 Principles of Computer Science I Fall 2018 Homework 6 S Exercise 21 : foo1 is a class method since it is declared static Exercise 22 : foo2 is an instance

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 4 Chapter 4 1 Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have.

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 03 / 23 / 2015 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Objects and Object Oriented Programming Overloaded methods / constructors

More information

Fundamentals of Programming Session 25

Fundamentals of Programming Session 25 Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Advanced Topics on Classes and Objects

Advanced Topics on Classes and Objects Advanced Topics on Classes and Objects EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG Equality (1) Recall that A primitive variable stores a primitive value e.g., double d1 =

More information

Inheritance. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Inheritance. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Inheritance EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Why Inheritance: A Motivating Example Problem: A student management system stores data about students. There are two kinds

More information

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017 Name: USC NetID (e.g., ttrojan): CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017 There are 6 problems on the exam, with 55 points total available. There are 10 pages to the exam (5 pages

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course Design-by-Contract (Dbc) Test-Driven Development (TDD) Readings: OOSC2 Chapter 11 Catching Defects: Design or Implementation Phase? To minimize development costs, minimize software defects. The cost of

More information

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

CS 335 Java Programming Controls. Fall 2007

CS 335 Java Programming Controls. Fall 2007 CS 335 Java Programming Controls Fall 2007 Java Control Structures Selection: If, If/Else, Switch Repetition (looping): While, For, Do/While Assignment: Expressions, increment/decrement Java Reserved Words

More information

CS 2530 INTERMEDIATE COMPUTING

CS 2530 INTERMEDIATE COMPUTING CS 2530 INTERMEDIATE COMPUTING Spring 2018 1-24-2018 Michael J. Holmes University of Northern Iowa Today s topic: Writing classes. 1 Die Objects A die has physical attributes: a specific Number of Sides

More information

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2 FACULTY OF SCIENCE AND TECHNOLOGY EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2 SAMPLE QUESTION Question 1 A class called TV is required by a programmer who is writing software for a retail

More information