cosc121:$$ computer$programming$ii · admin:$lab$overview$ • lab$organizaon$ •...
TRANSCRIPT
![Page 1: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/1.jpg)
COSC 121: Computer Programming II
Dr. Bowen Hui University of Bri?sh Columbia
Okanagan
1
![Page 2: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/2.jpg)
Admin: Lab overview
• Lab organiza?on • Website for lab manual • Review guidelines • Show list of labs • Quick demo of the provided Feed Me game • Pre-‐lab rules • New mul?ple choice site demo – Bonus rule
2
![Page 3: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/3.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
3
![Page 4: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/4.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
4
![Page 5: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/5.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
5
![Page 6: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/6.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
6
![Page 7: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/7.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
7
![Page 8: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/8.jpg)
Recap: Object-‐Oriented Design
• Process of building soQware based on a series of objects that interact together to solve a problem
• Object-‐oriented programming (OOP) – Set of programming techniques to support this design
• OOP examples from COSC 111? – Classes and objects what will be involved – Iden?fying aWributes what objects store – Class responsibili?es who interacts with whom – Encapsula?on how they communicate
8
![Page 9: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/9.jpg)
Class Rela?onships (Ch 7.4)
• Dependency (“Uses”): – A class uses another class
• Ex: The Dog class uses the Scanner class – An object of one class uses another object of the same class • Ex: A Dog object shares snacks with another Dog
• Aggrega?on (“Has-‐A”): – A class has objects of another class
• Ex: A Library has Book objects
9
calls a method
![Page 10: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/10.jpg)
Inheritance (Ch 9)
• Another OOP technique • Purpose: – Organize “related” classes together – Maximize reusable classes
• What is reusability and its advantages? – Defined class once, don’t define it again – Defined methods once, don’t define them again – Changes isolated to one place – Bugs isolated to one place
10
![Page 11: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/11.jpg)
Rela?onship
• Inheritance relates two classes to each other • Conceptual examples: – Children inherit physical traits from their parents – Humans inherit biological traits from Animals
• Terminology: – A child class inherits from a parent class – A subclass inherits from a superclass – A child class is derived from a parent class – A subclass is derived from a superclass
11
![Page 12: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/12.jpg)
Visually in UML
• Use a box to represent a class • Use an upward arrow to point to the parent class
• Depicts an IS-‐A rela?onship • Text: each box has lots of details – ignore for this class
12
Vehicle
Car A car is a vehicle
![Page 13: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/13.jpg)
Benefits of Inheritance
• Inherit methods and aWributes from parent class
• Can add new methods and aWributes to child class
• Can modify inherited method defini?ons inside child class
• All to maximize soQware reusability
13
![Page 14: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/14.jpg)
How it’s done
• Use a reserved word extends to indicate the rela?onship
• Template:
• Example:
14
public class Car extends Vehicle { // class contents }
public class Child extends Parent { // class contents }
![Page 15: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/15.jpg)
Examples
• Par?al code: public class Animal { … } public class Mammal extends Animal { … }
public class Reptile extends Animal { … } public class Dog extends Mammal { … }
• List the four IS-‐A rela?onships that are defined by this code
15
![Page 16: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/16.jpg)
Longer Example
• Client says: – I want a soQware program that lets me look up word defini?ons easily. AQer that, I might also want to extend the program to give me more complicated entries, like an encyclopedia.
• What classes do we need to model? • How are they related?
16
![Page 17: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/17.jpg)
Longer Example (cont.)
• Sample solu?on: – A Dic?onary is a Book – A Dic?onary has Words
17
Book
Dictionary Words
![Page 18: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/18.jpg)
A Very Simple Book Class
18
![Page 19: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/19.jpg)
A Very Simple Book Class
19
what’s this?
![Page 20: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/20.jpg)
A Very Simple Book Class
20
only visible to derived classes
![Page 21: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/21.jpg)
An Ini?al Dic?onary Subclass
21
![Page 22: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/22.jpg)
An Ini?al Dic?onary Subclass
22
what’s this?
![Page 23: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/23.jpg)
An Ini?al Dic?onary Subclass
23
calls constructor method in super class
![Page 24: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/24.jpg)
A Test Class
24
![Page 25: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/25.jpg)
A Test Class
25
![Page 26: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/26.jpg)
What is inherited?
• All aWributes from the parent class – Even private ones – How to access them? (See Sec?on 9.4)
• All methods from the parent class – Except: constructors are not inherited – Why not?
26
![Page 27: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/27.jpg)
What about Word?
27
![Page 28: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/28.jpg)
Changing Dic?onary Class
• How to keep track of Word objects? • How to define a new method for addEntry()?
• What input parameters should it take? • How to test your new changes in TestDic?onary?
28
![Page 29: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/29.jpg)
Sample Solu?on
29
![Page 30: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/30.jpg)
Sample Solu?on (cont.)
30
![Page 31: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/31.jpg)
Sample Solu?on (cont.)
31
![Page 32: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/32.jpg)
Sample Solu?on (cont.)
32
• Tes?ng: – Call the methods you created – Check outputs before and aQer
![Page 33: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/33.jpg)
Visibility Modifiers Revisited • Previously, you saw: – No visibility modifier (called “default”) – public – private
• Recall encapsula?on rules – Don’t ever leave anything default – Unless there’s a reason, classes are public – All class aWributes are private; access and changes must be done via accessors and mutators
– Only methods that are to be called by other classes should be public, all other methods (“helpers” within the class) are private
33
![Page 34: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/34.jpg)
New: protected
• Allows a child class to access an aWribute or method from the parent class – Like gran?ng special access to child classes – Trusted classes can see more of the parent class – Unrelated classes won’t be able to see the info
• Note: protected info is also visible to any class in the same package (not part of this course)
34
![Page 35: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/35.jpg)
The super Reference
• Constructors are not inherited – Even though they have public visibility
• Recall purpose of constructors: to set up aWributes
• In many cases, we s?ll want to reuse the parent class’s setup
• Solu?on: call super() as if you were calling the parent constructor directly – Pass in the same input as you would
35
![Page 36: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/36.jpg)
More on super
• Aside from the constructor, you can use super to call other methods and aWributes in the parent class
• Examples in Dic?onary.java: super.setPages( 5000 ); super.pages = 2;
• Be careful not to break encapsula?on rules! – Use accessors and mutators when possible
36
![Page 37: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/37.jpg)
Mul?ple Inheritance • This means a class is derived from two or more classes
• Example: PickupTruck extends Truck PickupTruck extends Car
• Problem: – Collisions – different parents may have the same aWributes and/or method signatures
• Java only supports single inheritance; mul?ple inheritance is not allowed
37
![Page 38: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/38.jpg)
Example
• A motorcycle inherits proper?es from both a bicycle and a car – Motorcycles and Bikes are two-‐wheeled vehicles – Motorcycles and Cars have engines, gas, fuel, similar speeds
• Java does not allow mul?ple inheritance • How would you implement Motorcycle as a child class?
38
![Page 39: COSC121:$$ Computer$Programming$II · Admin:$Lab$overview$ • Lab$organizaon$ • Website$for$lab$manual$ • Review$guidelines$ • Show$listof$labs$ • Quick$demo$of$the$provided$Feed$Me$game$](https://reader033.vdocuments.net/reader033/viewer/2022050504/5f96028622bd7018b47b352b/html5/thumbnails/39.jpg)
Summary of New Concepts
39
• Inheritance models IS-‐A rela?onship • Visibility modifier: protected • Use of super() calls parent’s constructor • A class cannot inherit from more than one class
• Next class: con?nue on inheritance