KEMBAR78
Lecture 14 - Course Revision | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
34 views97 pages

Lecture 14 - Course Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views97 pages

Lecture 14 - Course Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Course Revision

(IT069IU)

Le Duy Tan, Ph.D.


📧 ldtan@hcmiu.edu.vn

🌐 leduytanit.com 1
Previously,

- What is Web Scraping?


- Why is it important?
- How to use it safely and legally?
- HTML, CSS
- Introduction to JSoup Library
- JSoup workflow
- VNExpress Scrapping Demo
- Manga Scrapping Demo
- Potential issues with Web Scraping
2
Agenda
- Course revision:
- Go through topics we have learnt for each week
- Final exam format:
- Number of questions
- Topics
- Time
- Final Game Project
- Final Thought & Message of Tom

3
Lecture 1
Intro to OOP

4
Lecture 1 - Intro to OOP
- What are Programming Paradigms?
- What is Object Oriented Programming (OOP) ?
- Why should we learn Java?
- OOP Concepts
- Class
- Object
- Encapsulation with Access Modifier
- Abstraction
- Inheritance
- Composition
- What are JDK, JRE, JVM? How these works together?
5
Programming Paradigms
- Different approaches to build solutions to specific problems.
- Most programming languages belong to one paradigm.
- Two most popular programming paradigms:
- Procedural Programming
- Object-Oriented Programming

6
Java is still the king of job market in Vietnam

Attractive Salaries in Vietnam The most popular language at work in Vietnam


(Vietnam Developer Report 2019)
Nearly 1 out of every 5 IT jobs is Java
How Java works

Windows

Developers JVM
Java Runtime MacOS
develops (JDK) Environment JVM
(JRE)
Runs
Java source compile (JDK) Java class files
(*.class) - Java JVM Linux
files (*.java) 9
bytecode

Platform Independent Platform Dependent

=> Java is cross platform - “Write once, run everywhere!”


Lecture 2
Intro to Java

10
Lecture 2 - Intro to Java
- Different Java Platforms
- Our choice of JDK
- The best IDE selections
- Create the first Java programs
- Compile and Run with commands or on IntelliJ
- Java data types:
- Primitive
- Non-primitive (reference types)
- Variable
- Operators

11
Our first Java Program
MyFirstJavaProgram.java

12
Data Types
- Java is statically typed and also a strongly typed language.
- There are two categories of data types:
- Primitive data types (8)
- Non-primitive data types (reference type)

13
Operators
All the Java operators can be divided into the following groups −
• Arithmetic Operators: * / % + -
• Relational Operators: < > <= >= == != instanceof
• Bitwise Operators: & ^ |
• Logical Operators: && || !
• Assignment Operators: = += -= *= /=
• Ternary operator: ? :

14
Lecture 3
Intro to Classes & Objects in Java

15
Lecture 3 - Intro to Classes & Objects in Java
- Useful Classes
- Scanner
- Read input string with nextLine(), next()
- Read input number with nextDouble()
- String
- Display string with print(), println(), printf()
- Math
- pow(), max(), random()
- Primitive vs Reference
- Class
- Attributes
- Method with Parameters
- Getters and Setters Methods
- Access Modifiers (Public & Private)
- Constructor with Parameters
- UML Diagram
- Object
- Create objects from class with keyword new
- Call methods with input arguments
- Bank account application example
16
Scanner Class: Reading Inputs
- API documentation (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)
- To use Scanner class, remember to import java.util.Scanner
- Can read from different input sources: input keyboards, files, …
- Can convert inputs to different type of data types: string, byte, short, int, float, double, …

Java naming convention Method


names – lowerCamelCase

17
String Class: Representation in Text
- API documentation (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html)

18
Math Class: Performing Computation
- API documentation (http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html)
- 2 class attributes:
- E
- PI

19
Primitives vs References
Primitive types are basic Java types
- int, long, double, boolean, char, short, byte, float.
- A variable stores the actual value of that type.

Non-primitive types are Reference types


- String, Array, Class Objects, …
- A variable stores only the reference (memory address
location) to point to the actual object.

20
PRNG Applications
Monte Carlo Simulation

Machine Learning & Deep Learning

21
Homework: 649 Lotto Lottery!
- A lottery ticket is $4.
- A ticket having 6 different numbers (from 1 to 49) (Can be repeated)
- On a Saturday, they draw the lottery, and the winning numbers are:

11, 43, 24, 30, 60, 43

- Match at each position:


- Match One or Two numbers to get a small prize. ($10)
- Match Three numbers to get a small prize. ($100)
- Matching Four numbers gets a bigger prize. ($1000)
- Matching Five is even bigger. ($5000)
- Matching ALL SIX of the numbers you might win millions. ($5 million in cash)
- In the example, we got matches at position 1, 3, 4, 6 (4 numbers) = $1000
- [Math Question] What is the possibility of you winning by matching all 6 numbers?
- Homework Task:
- Write a simple program allows you to buy a ticket with six random numbers, and generate
the winning numbers and return what kind of prize you won (one game).
- Bonus: imagine you buy up to 100,000 tickets, can you figure out if you actually profit or 22
loss in a long run? mathsisfun.com/data/lottery.html
Lecture 4
Control Flow Statement & Overloading

23
Lecture 4 - Control Flow Statement & Overloading
- Casting types.
- Control flow statements:
- Decision making statements:
- If
- If…else
- Switch
- Loop statements:
- While
- Do while
- For
- Jump statements:
- Break statement
- Continue statement
- Static Keyword
- Static Method
- Static Variable
- Overloading
- Method Overloading
- Constructor Overloading
- Final Keyword 24
- Constant Variable
Casting among types
- Convert one data type to another data type.

[Question]: Which example has automatic cast, and which


one has explicit cast?

25
If Statement
- Three types of decision making statements:
- if statement:
- Performs an action, if a condition is true; skips it, if false.
- Single-selection statement—selects or ignores a single action (or
group of actions).
- if…else statement:
- Performs an action if a condition is true and performs a different
action if the condition is false.
- Double-selection statement—selects between two different actions
(or groups of actions).
- switch statement
- Performs one of several actions, based on the value of an expression.
- Multiple-selection statement—selects among many different actions
(or groups of actions).
26
Control & Loop Flow Diagram

27
Here is an UML of Lottery Game Example

28
Lecture 5
Array & ArrayList

29
Lecture 5 - Array & ArrayList
- Increment and Decrement Operators
- Scope of Declarations
- this keyword
- Array:
- Declare and Create Array
- Loop through Array
- Pass Arrays to Methods
- Pass by Value vs Pass by Reference
- Multidimensional Arrays
- Class Arrays for helper methods
- ArrayList (Collections Class)
- Array vs ArrayList

30
- For example, the three assignment statements:

- can be written more concisely with compound assignment operators


as

- prefix increment operators as

31

- postfix increment operators as


Meme about Scope of Declaration

32
Array
- An array is a group of variables (called elements or components)
containing values of the same type.
- Arrays are objects, so they’re considered reference types.

int[ ] x = new int[5];


x[0] = 34;
x[1] = 21;
x[2] = 2;
x[3] = 66;
x[4] = 567;
33
The Nature of Multidimensional Array
- Two dimensional array is one type of multidimensional array.

1-Dimensional Array 2-Dimensional Array 3-Dimensional Array

34
ArrayList & Array Example
Output:

35
What do you do when you get stuck?

36
Lecture 6
Inheritance

37
Lecture 6 - Inheritance
- Inheritance:
- Definition and Examples
- Types of Inheritance
- UML Diagram
- Animal Inheritance Example
- Without Inheritance
- With Inheritance
- Method Overriding
- Constructors in Subclasses
- Keyword Super
- Method Overriding
- Keyword Super
- Access Modifier
- Protected
- Exercise for University Staff & Lecturer
38
Inheritance
- Inheritance is a way to reuse classes by expanding them
into more specific types of classes. Person
-Name
- Inheritance allows a child class (subclass) to inherit the -Age
attributes and the methods of a parent class (superclass).
+walk()
So a child class can do anything that the parent class +eat()
can do!

- A child class
- can have its own attributes and methods.
- A child class can customize methods that it inherits
from its parent class (method overriding)

- Inheritance represents IS-A relationship between parent Programmer Dancer Singer


and child objects. -Name -Name -Name
-Age -Age -Age
- -groupName -bandName
- Inheritance promotes the idea of code reuse to reduces CompanyName
code repetition as classes can share similar common logic, +walk() +walk()
+walk() +eat() +eat()
structure, attributes and methods. +eat() +dance() +sing()
+code() +playGuitar()
Examples of Superclass, Subclasses
- Superclass tend to be “more general” and Subclass is more “more
specific.”

Parent Class Child Class


(Superclass) (Subclasses)
Vehicles Car, Truck, Boat, Bicycle

Shape Circle, Triangle, Rectangle, Cube

UniversityStaff Lecturer, TeachingAssistant

Animal Dog, Cat, Spider, Duck


Which access modifier to use?
- For beginners, keep it simple:
- Class: public
- Attributes/Instance Variables: private
- Methods (including getters and setters): public
- Class Variables (static keyword): public
- Class Methods (static keyword): public
- Constants (final keyword):
- Class Constant (with static keyword): public
- Normal Constant (without static keyword): private

- The short answer is, you should make everything as restricted as possible
(private over protected, and protected over public) while still allowing
things to get done.
- When it comes to deciding between public and private, it all comes down to
how widely used you expect the class to be. If you think a class is reusable
across many projects, then you might as well make it public from the
beginning.
41
Lecture 7
Polymorphism

42
Lecture 7 - Polymorphism
- Polymorphism
- Method overriding in Inheritance
- Zoo Example
- Abstraction
- Abstract Class
- Abstract Method
- Examples:
- Zoo Example
- Company Payroll Example
- Interface
- Interface in real life examples
- Upgrade Company Payroll with Invoices Example
- Abstract vs Interface vs Composition

43
Polymorphism
- Polymorphism literally means “many shapes”.
- The same method is called in different types of objects has different results.
- Enables you to “program in the general” rather than “program in the specific.”
- Polymorphism enables you to write programs that process objects of subclasses that share the
same superclass as if they’re all objects of the superclass; this can simplify programming.

Animal
-name
-age

+speak()

Dog Cat Duck

+speak() +speak() +speak()


Abstraction in Shape
The class Shape does not provide any implementation for draw() ; basically there is no code, and this is
what makes the method abstract (providing any code would make the method concrete).

We want the subclasses to provide the implementation. Let’s look at the Circle and Rectangle classes:

The Draw method can be invoked for every


single shape in the system, and invoking each
shape produces a different result:
- Invoking the Draw method on a Circle object
draws a circle.
- Invoking the Draw method on a Rectangle
object draws a rectangle.
In essence, sending a message to an object
evokes a different response, depending on the
object. This is the essence of polymorphism. 45
Abstract VS Interface vs Composition?
- The obvious question is this: If an abstract
class can provide the same functionality
as an interface, why do Java bother to
provide an interface?
- If both abstract classes and interfaces
provide abstract methods, what is the real
difference between the two?
- As we saw before, an abstract class
provides both abstract and concrete
methods, whereas an interface provides
only abstract methods. Why is there such
a difference?
46
- In a nutshell, Java build objects in three ways: inheritance, interfaces, and
composition.
- This example illustrates when you should use which one. When do you choose an
abstract class? When do you choose an interface? When do you choose
composition?
- You should be familiar with the following concepts:
- Dog is a Mammal , so the relationship is inheritance.
- Dog implements Nameable , so the relationship is an interface.
- Dog has a Head , so the relationship is composition.
Although inheritance is a strict is-a relationship, an interface is not
quite. For example:
- A dog is a mammal.
- A toy is not a mammal.
Thus, a Toy class could not inherit from the Mammal class.
However, an interface applies for the various classes. For example:
- A dog is nameable.
- A teddy bear is nameable.
47
48
Lecture 8
Graphic User Interface (GUI)

49
Lecture 8 - Graphic User Interface (GUI)
- Mid-term Exam
- Possible Topics
- Format
- GUI in Java
- Swing Components
- Swing vs JavaFX vs AWT
- JOption-Panel (DialogBox)
- Java 2D API
- Java Coordinate System
- Draw Lines
- Draw Rectangles and Ovals
- Draw Colors
- Draw Polygons and Polylines
- Text Fields, Buttons and Menu Nagivations.
- Event Handler (Mouse & Keyboard)
- Layout:
- BorderLayout Frame & GridLayoutFrame 50
Sample GUI of Java’s Swing GUI APIs

51
https://mlapshin.com/index.php/projects/swingset3/
Color in Computer

52
Lecture 9
Generic Collections

53
Lecture 9 - Generic Collections
- Mid-term Exam Result
- Score Distribution
- Statistics
- Solutions
- Java Generic Collections
- Type-Wrapper Classes for Primitive Types
- Autoboxing vs Auto-unboxing
- List
- ArrayList
- Vector
- LinkedList
- Sets
- HashSet
- TreeSet
- Maps
- Hashtable
- HashMap
- TreeMaps
- HackerRank
- Introduction 54
- Coding Challenge
Collections
- Java Collections framework:
- Prebuilt data structures.
- A data structure is a particular way of organizing data so that it can be used effectively.
- Interfaces and Methods for manipulating those data structures.
- A collection is a data structure—actually, an object—that can hold references to other
objects.
- Usually, collections contain references to objects that are all of the same type.

55
56
Challenge Time!
Real Job Interview Question: Ransom Note
https://www.hackerrank.com/challenges/ctci-ransom-note/problem
Hint: you should use HashMap.

57
Lecture 10
Generic Class

58
Lecture 10 - Generic Class
- Generic
- Generic Class
- Bounded type parameters
- Generic Method

59
The Problem

60
To create object/instance of Generic class
- Create a new generic class with a type placeholder T:

public class ClassName <T>{



}

- Use that generic class to create an object from it and specific what datatype
will be replace T.

ClassName <Type> objectName = new ClassName <Type>();

61
Simple Generic Class Example

62
Problem

63
Solve it with Bound Type Parameters

Output:

64
Lecture 11
OO Design Principles (SOLID)

65
Lecture 11 - OO Design Principles (SOLID)
- Object Oriented Design Principles: SOLID
- S: Single responsibility
- O: Open/closed principle
- L: Liskov substitution principle
- I: Interface segregation principle
- D: Dependency inversion principle

66
SOLID Summary

67
Lecture 12
Design Pattern

68
Lecture 12 - Design Pattern
- Design Patterns:
- Creational
- Singleton
- Factory Method Factory Design Pattern
- Structural
- Behavioral

69
OOP Concepts vs OOP Principles vs Design Pattern

70
Types of Design Patterns
There are three categories of these patterns:

- Creational Patterns
- Focused on the way objects are created.
- Structural Patterns
- Focused on creating objects into larger structures which gives developers the
advantage to keep groups of children together and also abstract objects to
simplify applications.
- Behavioral Patterns
- Focused on combining algorithms and responsibilities together which concerned
with communication between objects.

71
Creational Design Pattern
These design patterns focused on the way objects are created:

- Abstract Factory (⭐⭐⭐): Lets you produce families of related objects without
specifying their concrete classes.
- Builder(⭐⭐⭐): Lets you construct complex objects step by step. The pattern
allows you to produce different types and representations of an object using the
same construction code.
- Factory Method(⭐⭐⭐): Provides an interface for creating objects in a superclass,
but allows subclasses to alter the type of objects that will be created.
- Prototype(⭐⭐): Lets you copy existing objects without making your code
dependent on their classes.
- Singleton(⭐⭐): Lets you ensure that a class has only one instance, while providing
a global access point to this instance. 72
Structural Design Pattern
These design patterns focused on creating objects into larger structures:

- Adapter(⭐⭐⭐): Allows objects with incompatible interfaces to collaborate.


- Bridge(⭐): Lets you split a large class or a set of closely related classes into two
separate hierarchies—abstraction and implementation—which can be developed
independently of each other.
- Composite(⭐⭐): Lets you compose objects into tree structures and then work with
these structures as if they were individual objects.
- Decorator(⭐⭐): Lets you attach new behaviors to objects by placing these objects
inside special wrapper objects that contain the behaviors.
- Facade(⭐⭐): Provides a simplified interface to a library, a framework, or any other
complex set of classes.
- Flyweight(⭐): Lets you fit more objects into the available amount of RAM by sharing
common parts of state between multiple objects instead of keeping all of the data in
each object.
- Proxy(⭐): Lets you provide a substitute or placeholder for another object. A proxy
controls access to the original object, allowing you to perform something either
before or after the request gets through to the original object.
73
Behavioral Design Pattern
These design patterns focused on the communication between objects.:
- Chain of responsibility(⭐): Lets you pass requests along a chain of handlers. Upon receiving a
request, each handler decides either to process the request or to pass it to the next handler in
the chain.
- Command(⭐⭐⭐): Turns a request into a stand-alone object that contains all information
about the request. This transformation lets you pass requests as a method arguments, delay or
queue a request's execution, and support undoable operations.
- Iterator(⭐⭐⭐): Lets you traverse elements of a collection without exposing its underlying
representation (list, stack, tree, etc.).
- Mediator(⭐⭐): Lets you reduce chaotic dependencies between objects. The pattern restricts
direct communications between the objects and forces them to collaborate only via a mediator
object.
- Memento(⭐): Lets you save and restore the previous state of an object without revealing the
details of its implementation.
- Observer(⭐⭐⭐): Lets you define a subscription mechanism to notify multiple objects about
any events that happen to the object they're observing.
- State(⭐⭐): Lets an object alter its behavior when its internal state changes. It appears as if
the object changed its class.
- Strategy(⭐⭐⭐): Lets you define a family of algorithms, put each of them into a separate
class, and make their objects interchangeable.
- Template method(⭐⭐): Defines the skeleton of an algorithm in the superclass but lets
subclasses override specific steps of the algorithm without changing its structure. 74
- Visitor(⭐): Lets you separate algorithms from the objects on which they operate.
Singleton: What?
- Singleton pattern is one of the simplest design patterns in Java. This type of
design pattern comes under creational pattern as this pattern provides one
of the best ways to create an object.
- Singleton is a creational design pattern that lets you ensure that a class has
only one instance, while providing a global access point to this instance.

75
Factory Method: What?
- Factory Method is a creational design pattern that provides an interface
for creating objects in a superclass, but allows subclasses to alter the type
of objects that will be created.

76
Lecture 13
Web Scraping

77
Lecture 13 - Web Scraping

- What is Web Scraping?


- Why is it important?
- How to use it safely and legally?
- HTML, CSS
- Introduction to JSoup Library
- JSoup workflow
- VNExpress Scrapping Demo
- Manga Scrapping Demo
- Potential issues with Web Scraping
78
What is Web Scraping?

Web Pages Web Scraping Structured Data

79
Web Component Overview

80
VNExpress Homepage Scraping Result

81
Italian Recipe Scrapping Homework
https://foodnetwork.co.uk/italian-family-dinners/

Scrap all Italian recipes,


which should have:

- Recipe title
- Short description
- Preparation time
- Cook time
- Number of serves

82
Our Challenge
- Let’s scrap one of the best manga websites, “MangaDoom”!
https://www.mngdoom.com/

83
Let’s pick a best manga to scrap them!
- “One Piece” is my favourite manga of all time! Let’s scrap the whole manga!

84
I'm a collector myself! Web Scraping is awesome!

85
Ultimate Challenge
- MangaSee is the only website which got the FULL-COLORED chapters of big
mangas like One Piece and Bleach!!!
- [Task] Can you figure out how to scrap either full-colored One Piece or Bleach?
- https://mangasee123.com/manga/One-Piece-Digital-Colored-Comics
- https://mangasee123.com/manga/Bleach-Color

86
One of The Best Manga Downloader Project
- https://github.com/riderkick/FMD
- The Free Manga Downloader is a free open source application written in Object
Pascal for managing and downloading manga from various websites.
- Based on what you have learned so far about web scraping, now you can actually
start to contribute to this open source project to help the Manga community!

87
Lecture 14
Final Lecture - Revision

88
Lecture 14 - Final Lecture - Today Revision
- Course revision:
- Go through topics we have learnt for each week
- Final exam format:
- Number of questions
- Topics
- Time

89
Final Exam Format
- Offline
- Open book (Lecture notes, books, … are allowed BUT NO
mobile phone, internet, google or laptop)
- 4 Questions, with topics could be related to:
- OO Concepts (keywords, ideas, …)
- Generic (Collections, Class)
- OO Design Principles (SOLID)
- Design Patterns
- 120 minutes
90
Final Game Project
- Your team present your project will be on the dates:
- For Mr. Nhân's Lab: Friday (10/06)
- For Mr. Tom's Lab: Saturday (11/06)
- Time for your presentation: 12 Minutes
- Could be break down into:
- 9 minutes presenting slides
- 3 minutes of live demo your game
- The lab instructor will ask some questions for 3-5
minutes after the presentation finishes.
- Marking Criteria for the final Project:
- 20% report (README file)
- 30% presentation
- 50% demo
- Link to register for your lab dates!
- Link for the requirement document!
91
Final Thought & Message

92
The Secret of Success: Get 1% Better Every Day
- The only idea is to improve slightly everyday! Learn something new everyday!
- Let’s prove this point:

93
94
What’s next for you?

95
What’s next for Tom?
Career tomCareer = new Career("PhD");
while (True) {
tomCareer.study();
tomCareer.teach();
tomCareer.work();
}

96
Thank you for listening!

“Always be learning!”
Tom Huynh

97

You might also like