CSC204 – Programming I

Download Report

Transcript CSC204 – Programming I

CSC204 – Programming I
Lecture 2
Intro to OOP with Java
1
Topics





Ways to compile and execute a program
Java’s approach
Considerations for software design
Object and class
[Running a simple Java program]
2
Automation Starts from Programs
What is a program?
public class AutomatedProcess {
public static void main(String[] args) {
System.out.println("Step #1");
System.out.println("Step #2");
System.out.println("Step #3");
}
}
 How can the instructions defined in a program be
executed?


They need to be translated into machine code first
3
A Simple Computer
printer
processor
0
1
2
3
4
5
4194300
4194301
4194302
4194303
display
memory
keyboard
disk
4
Compilation
The program’s source code is given to a
program called a compiler.
 The compiler checks that the source code
is valid (obeys the rules of the language)
and translates it to machine
instructions for a particular CPU.
 The compiled program (also know as
executable) is stored in a file, and it can
be run as many times as desired.

5
Compilation & Execution
Compilation
editor
edit
Source
Code
(xxx.c)
text file
saved on disk
Edition
CPU
compiler
compile
Compiled
File
load
(a.out)
Instructions in
machine language
CPU specific
saved on disk
Memory
Execution
6
Interpretation
The program’s source code is given to a
program known as an interpreter.
 The interpreter executes the program
without first translating it to machine
instructions.
 The interpreter itself is normally a
compiled program, so it can execute
machine instructions corresponding to the
source code.

7
Interpretation & Execution
editor
edit
Source
Code
(xxx.c)
CPU
interpreter
Memory
text file
Edition
interpret
Interpretation &
Execution
Internal
file
Instructions in
machine language
CPU specific
8
Problems w/ Networked Computers



Different machines are interconnected to each
other in a interconnected farm of machines
Each of them may uses a different machine
language
How can a program be executed on these
different machines?
How can it
run
on System B?
System A
Compiled program
compiled on System
A
X
System B
9
Java’s Approach - illustrated
Compiled Java byte code
(not in any specific machine language)
Compiled once and runs everywhere
JVM-A
JVM-B
System A
JVM-N
System B
...
System N
10
Java’s Approach




Java employs a combination of compilation and
interpretation.
The Java compiler translates the original program
into bytecode instructions for a computer
called the Java Virtual Machine.
The resulting bytecode program is then
executed by an interpreter.
One advantage of Java’s approach is that
programs don’t need a particular CPU or
operating system.
11
Bytecode and JVM
editor
Compilation
Source
Compiler
Code
edit
(xxx.java) (javac)
text file
saved on disk
Edition
Bytecode
compile program
load
(xxx.class)
Instructions in
bytecode
saved on disk
CPU
Memory
JVM
interpret
Execution
12
Ways to Handle Complexity



S/W can be considered as virtual machineries
that automate some information processes
These systems consist of virtual s/w parts
Ways to handle the complexity




Abstraction: consider only the essential aspect of a real
world information process
Encapsulation: don’t tell us how to make the ice cubes,
just let us know which button to press to get them
Modularity: I need a car with 4 wheels, two doors, ...
Hierarchy:


Composition: each book has a number of chapters, each ...
Taxonomy: a bird is an animal that can fly, an eagle is ...
13
Abstraction
14
Encapsulation
15
Modularity
16
Hierarchy: composition
17
Hierarchy: taxonomy
18
What is an Object?


An object is a s/w entity that can do something
for other part of the s/w system (another object)
An object is a runtime entity, it only exists when
the s/w system is in execution



A class, or the template of objects of the same kind is
defined by a program (source code)
A class file (executable) is created as a result of
compilation. It is loaded into memory when executed
Objects can then be instantiated using the class
definition
19
How Do Objects Work Each Other?


we can refer to the object that does sth a server,
the one that requests the service a client
From the client perspective, how can you get the
service (alarm)? Passing a message to the server
(clock):
clock, please set the alarm to 7 am sharp!
Recipient


service (or operation) additional info
clock.setAlarm(7);
From the server’s perspective, it is responsible
for


Maintain certain properties: such the due time at which
the alarm will ring
Be able to perform certain operations: allowing clients to
set alarm and starting the alarm at due time
20
Steps for Java Programming



Analysis
Design
Implementation



Edit your code
Compile your code
Testing


Execute your code (using test data)
[Debug your code]
21
A Quick Walkthrough



Open your favorite text editor (e.g. NotePad)
Copy the code snippet (on slide #3) into a blank
text file
Save it as c:\myname\AutomatedProcess.java






Create a folder c:\myname as shown in class
Open a DOS prompt
Change directory to the folder in which you saved
the .java file
Type javac AutomatedProcess.java to compile
Type dir to check the bytecode executable you
just created: AutomatedProcess.class
Type java AutomatedProcess to execute
22