🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

AC-S24-OOP-Lect04-Part2-UML-Collections.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

LuxuryAbundance

Uploaded by LuxuryAbundance

Algonquin College

2024

Tags

object-oriented programming UML computer science

Full Transcript

Lect 04B CST8132 OOP Algonquin College Lect 04B Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof....

Lect 04B CST8132 OOP Algonquin College Lect 04B Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa Algonquin College Lect 04B Computer Engineering Technology More UML / Collections CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa L1 L2 Week 4B: UML A2W01 A2W02 A2W09 A2W10 A2W03 A2W11 A2W04 A2W12 A2W05 A2W13 Basic concepts A2W06 A2W14 Examples A2W07 A2W15 A2W08 4 Topics More about UML Representations ArrayList More details Inheritance Review Javadoc Resume 5 Warm up – Brief Reviews Q1. 1 minute Explain the difference between Overloading and Overriding Q2. 1 minute Array of Primitives vs. Array of References Q3. 1 minute What does UML stand for? What is it, and what is its purpose? 3 6 Summary 1. Inheritance Super-class, sub-class Super keyword – super(), super.method() 2. Polymorphism Taking multiple forms Static polymorphism – compile time (ex. Method overloading) Dynamic polymorphism – runtime (ex. Method overriding) 3. Abstract class Providing abstract of methods…. Will be overridden in child classes Can have non-abstract methods – they can have their definitions in the abstract class 4. Interface Only abstract methods and public static final variables Abstract keyword not required – by default, methods are abstract 7 OOP – Week 4 More About UML 8 UML – Unified Modeling Language General-purpose developmental modeling language Standard way to visualize the design of a system Many types on language exist References: https://www.oracle.com/technetwork/developer-tools/jdev/gettingstartedwithumlclassmodeling-130316.pdf https://en.wikipedia.org/wiki/Unified_Modeling_Language https://creately.com/blog/diagrams/class-diagram-tutorial/ 9 UML – Unified Modeling Language Using Unified Modeling Language (UML) to represent relationships between Objects in OO programs visually. Manipulating objects together using collections 10 UML – Unified Modeling Language General-purpose developmental modeling language Standard way to visualize the design of a system Many types on language exist https://www.oracle.com/technetwork/developer- tools/jdev/gettingstartedwithumlclassmodeling-130316.pdf https://en.wikipedia.org/wiki/Unified_Modeling_Language https://creately.com/blog/diagrams/class-diagram-tutorial/1 11 Uses of UML As a sketch: to communicate aspects of the system Forward design: create UML before coding Backward design: create UML after coding (for documentation) Often done on whiteboard or on paper Used in brainstorming As a blueprint: a complete design to be implemented Done with professional tools like Visio As a programming language: tools available to auto-generate 1 the structure of the code from the UML 12 UML diagrams 13 Association, Aggregation & Composition Association Aggregation Composition Relationships owners feed pets, pets please 6owners (association) a tail is a part of both dogs and cats (aggregation / composition) a cat is a kind of pet (inheritance / generalization) Taken from https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-aggregation-vs-composition/ 14 Relationships between Objects - Association Association (general relationship, using) Weakest relationship Class A uses/references Class B Notation: If there is an import statement, there is at least an association or between the classes Associations can be unidirectional or bidirectional (one-to-one, one- to-many, many-to-one, many-to-many) 15 Relationships between Objects - Association Examples: Employee class & Scanner Pet 1 class Owner class & Pet class Owner Pet 2 Owner can have more Pet 3 7 than one Pets Owner and Pet are associated through their objects 16 Relationships between Objects - Aggregation Aggregation (has-a relationship) Special form of Association Class A has Class B as a property College (instance or classvariable) No strict ownership between these Stude class nt Notation: Unidirecional association 17 Relationships between Objects - Aggregation Examples: classes can exist separately of one another A duck has-a pond Duck class has a Pond attribute, which refers to the pond the duck is currently swimming in If the Duck dies, the Pond does not disappear College & Student College has multiple students 18 Relationships between Objects - Composition Composition (stronger has-a, consists of) Restricted form of aggregation Whole/Part relationship where the Part cannot exist without the whole There is a strict ownership between the Whole and the Part. Notation: Student Professor 19 Relationships between Objects - Composition Examples: A Vehicle has a Cabin and Trunk Space, which do not exist without the enclosing Vehicle A Business is composed of Departments which do not exist without the Business A Student has a Person (with personal properties), which do not exist without the Person Trees have leaves A Tree has-a Leaf A Tree has a Leaf array (array of many leaves actually) If the Tree dies, the leaves die, as they are Student Professor physically part of the tree 20 Relationships between Objects - Summary Relationship between objects (from weakest to strongest) 1. Association – “Uses” 2. Aggregation – “Has” 3. Composition – “Consist of” (stronger Has) Cardinality – how many objects are there? 1 1 21 Structural : Class Diagram Top compartment – class name – Centered and Bold Middle compartment – State (attributes, properties, instance or class variables) Bottom compartment – Behaviors (methods) Access Level Modifiers: + means public # means protected ~ means package protected - means private Notice that in UML, types come after the member name, rather than before in Java In Java: private int myInt and public int factorial (int n) 22 UML Relationships Lines drawn between class boxes indicate relationships between objects of the classes A line with a solid diamond on one end indicates that the objects of the class at the other end are a part of objects of the class at the diamond end 0..1 - optional 1 – one and only one 1..1 – one and only one Cardinality: numbers at either end of 1..n – one to a specific limit an association line represent how 1..* - one or more many of each are involved 0..* - zero or more * - zero or more 0..n – zero to a specific limit 23 Simple Class Diagram UML class diagram – college system 24 UML class diagram Shape (abstract class) 25 UML class diagram Shape (interface) 26 UML Notation Symbols (relationships) 27 UML Inheritance Example Think of super class GeometricShape. What you see below are all geometric shapes. Circle is-a GeometricShape. Rectangle is-a GeometricShape Two dim Geometric Shapes Circle Square Rectangle Triangle Base Height Radius Length Width Hypotenuse Diameter filled Length color filled color color calcArea() color calcArea() calcArea() calcArea() Perimeter 28 UML Inheritance Example Question: What do they have in common? What is unique to each of them? Three Dim GeometricObject cuboid cube Height Width Length Height Width Length Volume Volume calcSurfaceArea( calcSurfaceArea() ) 29 UML - Inheritance To Do/Practice Create a program for the class hierarchy shown on the left 30 Inheritance – based on UML in previous slide Circle and Rectangle are subclasses of Geometric Shape Circle and rectangle are subtypes of supertype GeometricShape Circle inherits all attributes of Geometric Shape. Circle has its own attribute ‘radius’ and methods: getDiameter(), getArea(), getPerimeter() 31 OOP – Week 4 More About ArrayList 32 ArrayList (Review) An ArrayList’s capacity indicates how many items it can hold without growing. When the ArrayList grows, it must create a larger internal array and copy each element to the new array. ▪ This is a time-consuming operation. It would be inefficient for the ArrayList to grow each time an element is added. ▪ An ArrayList grows only when an element is added and the number of elements is equal to the capacity—i.e., there is no space for the new element. 33 ArrayList (Review) About methods: Method add() adds elements to the ArrayList. One-argument version appends its argument to the end of the ArrayList. Two-argument version inserts a new element at the specified position. Remember: Collection indices start at zero. 34 ArrayList (Review) About other methods: size() returns the number of elements in the ArrayList. get() obtains the element at a specified index. remove() deletes an element with a specific value. contains() determines if an item is in the ArrayList. Remember: An overloaded version of the method removes the element at the specified index. 35 ArrayList (Review) 36 ArrayList (Review) 37 ArrayList (Review) 38 Packages A collection of related classes Groups related classes together Namespace to avoid naming conflicts Provides a layer of access/protection Easy access Can also contain sub packages Package ➔ directory (folder) Class ➔ file 39 Packages and directories import java.util.Scanner; 40 Package declaration and importation Notation: import.*; package shape; public class Rectangle { //... Rectangle will import util package } Import java.util.*; File Rectangle.java will be saved in public class Rectangle { the folder named shape. //... } 41 Packages Workspace Project Package (CST8132_20S) (Lecture3) (shapes) 42 Importing a class import.*; import java.util.Scanner; Example: public class Rectangle { //... } Importing single classes has high precedence If we import.*, a class with the same name in the current directory will override If we import.className, it will not. 43 Referring to Packages import java.util.Scanner; input = new Scanner(System.in); We can use a type from any package without importing it… just use the full name 44 Default package If we do not declare a package, files will be added to the default, unnamed package Classes in the default package Cannot be imported Cannot be used by classes in other packages The package java.lang is implicitly imported in all programs by default. 45 Access Modifiers Private Only instances of the class itself can access it Protected Only instances of the class and instances of the subclasses can access Public Everyone has access. can be accessed from within the class, outside the class, within the package and outside the package Default (Package) Members of the same package can access all members 46 Open questions… Any doubts / questions? How we are until now? 47 See you… Enjoy our course and season… 48 OOP – Week 4 Thank you for your attention! Contact: [email protected] 49

Use Quizgecko on...
Browser
Browser