Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

PR2 - Programming 2

Lecture 3
Operation principles of Compiler, Virtual machine and basic OOP
Outline
• Compiler
• Virtual machine
• Basic OOP
References
How do computers run your code?
• File code in C++ language
• File code in Python language
C++ Language
• Source program: file code
• Target program: executable file
• Input: file, keyboard, …
• Output: the purpose of this app
Python Language
• Source program: file code
• Input: file, keyboard, …
• Output: the purpose of this
app
Compilation and Interpretation
• Compiler:
• Compiler is basically a program translation program all at once.
• Interpreter:
• Interpreter translates a program instruction-by-instruction into machine
language and executes it.
Compiler vs
Interpreter
Compiler vs Interpreter
Compiler Interpreter

Advantage - Compilation generally leads to better - Interpreter helps debug more easily.
performance.

Machine code - Store target machine code program. - Not save machine code program

Memory -Target program execute independently -The interpreter exists in the memory
and do not require the compiler in the during interpretation.
memory.
Code change - Need to generate new target machine - Change in code leads to change in
code to apply the new update in code. running time immediately.
Java Virtual Machine
• The designers of Java chose to use a combination of compiling and
interpreting
• The machine language for the Java Virtual Machine is called Java
bytecode
Why use the intermediate Java bytecode?
JVM
• It is easier to write compiler that translates Java to Java bytecode than
a compiler that translates Java to other machine language.
• JVM acts as a buffer between you and a program you are running can
protect you from potentially dangerous actions of the program.
• JVM helps the same compiled program can be run on many different
types of computers.
OOP and Procedural Programming
• Procedural programming uses a list of procedures to tell the
computer what to do step-by-step. The procedures are the functions,
routines, or subroutines that consist of the computational steps
required to be carried.
• OOP uses a set of objects that interacts with each other to achieve
the program’s intended behavior.
OOP and Procedural Programming
• Requirement: there will be shapes on GUI, a square, a circle, and a
trigangle. When the user clicks on a shape, the shape will rotate
clockwise 360 degree and play a .flac song file specific to that shape.
Procedural programming : P1 OOP: P2
Write procedures: 3 classes:
Square, Circle, Triangle
rotate(shapeNum) {
// make the shape rotate 360º
}
playSound(shapeNum) {
// use shapeNum to lookup which
// Flac sound to play, and play
it
}
OOP and Procedural Programming
There is a requirement change: There will be an amoeba shape
on the screen, with the others. When user clicks on the
amoeba, it will rotate like others, and play .mp3 sound file.
Procedural programming : P1 OOP: P2
The rotate procedure would still work; But new class:
playSound would have to change. Amoeba

playSound(shapeNum) {
// if the shape is not an amoeba
// use shapeNum to lookup which
// Flac sound to play, and play it
// else
// play amoeba .mp3 sound}

Touch previously tested code so need to change Don’t have to touch code he’d already tested and
code test and test all code again. delivered. Only test new added code.
OOP and Procedural Programming
• There is a mistake in requirement analysis. That’s
not how the amoeba is supposed to rotate.
O
• Turns out, both programmers had written their
rotate code like this:
• determine the rectangle that surrounds the
shape.
• calculate the center of that rectangle, and O

rotate the shape around that point.


• But the amoeba shape was supposed to rotate O
around a point on one end, like a clock hand.
OOP and Procedural Programming
Procedural programming : P1 OOP: P2
Add rotation point arguments to the rotate procedure. modified the rotate method, but only in the
Amoeba class.
rotate(shapeNum, xPt, yPt) {
// if the shape is not an amoeba,
// calculate the center point
// based on a rectangle,
// then rotate
// else
// use the xPt and yPt as
// the rotation point offset
// and then rotate
}

A lot of code was affected. So need to test all code again. Don’t touched the tested, working, compiled code
for the other parts of the program. Only test new
added code.
OOP and Procedural Programming
Benefit:
• OOP are more secure in data.
• OOP can maintain code more easily.
• OOP can extend code more easily.
• OOP increase reuse of code by applying inheritance and
polymorphism.
Review basic OOP definition
• Class vs Object
• Attributes and behaviors
Class vs Object
• A class is a set of similar but unique objects in a problem domain.
Class defines the data attributes and behaviors of its objects.
• Object describes an entity (a particular thing of interest) that has
state, behavior and identity. The structure and behavior of similar
objects are defined in their common class. The terms instance and
object are interchangeable.
Attributes and behaviors
• Attributes are characteristic of class. Attributes and their value form
object state.
• Behaviors are tasks that object performs. Behavior is how an object
acts and reacts, in terms of its state changes and message passing.
Present class using compartments box
A class can be visualized as a three-
compartment box, as illustrated:
• Name (or identity): identifies the class.
• Variables (or attribute, state, field):
contains the static attributes of the class.
• Methods (or behaviors, function,
operation): contains the dynamic
behaviors of the class.
Reference and object
• The object’s location is called a reference
• All object variables are references, as opposed to primitive variables
• == only compares the references when using with objects.
Object life cycle
• Object is created
• Object is accessible
• Object is inaccessible
• Garbage collection
Create object
What is the difference between declaring a reference variable and
initializing a reference variable?
Object is accessible
• Once an object is created, it can be accessed using its reference
variable. Its remain accessible until it goes out of scope or its
reference variable is set to null.
• If you reassign another object to an initialized reference variable, the
previous object becomes inaccessible from that variable.
Object is inaccessible
• An object can become inaccessible if it goes out of scope or it is
dereferenced by reassignment.
Garbage Collection
• Garbage Collector is low priority thread that marks the object eligible
for garbage collection in the JVM and then clear the memory of these
objects.
• It enables automatic memory management.
• A user can’t control or determine the execution of garbage collector.
The JVM control it.
• You can request it by calling System.gc() or Runtime.getRuntime().gc()
Summary
• Compiler is a program translation program all at once.
• Interpreter translates a program instruction-by-instruction into
machine language and executes it.
• Why use OOP?
• Basic OOP definition
• Object life cycle

You might also like