1 csc204 – programming i lecture 2 intro to oop with java

Post on 01-Jan-2016

227 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CSC204 – Programming I

Lecture 2Intro to OOP with Java

2

Topics Ways to compile and execute a program Java’s approach Considerations for software design Object and class [Running a simple Java program]

3

Automation Starts from Programs What is a program?

How can the instructions defined in a program be executed? They need to be translated into machine code first

public class AutomatedProcess { public static void main(String[] args) { System.out.println("Step #1"); System.out.println("Step #2"); System.out.println("Step #3"); }}

4

processor

A Simple Computer

012345

4194300419430141943024194303

diskkeyboard

display

printer

memory

5

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.

6

Compilation & Execution

CompiledFile

(a.out)

Source Code

(xxx.c)

text filesaved on disk

editor

compile

edit compiler

Instructions inmachine languageCPU specificsaved on disk

Memory

CPU

load

Execution

Compilation

Edition

7

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.

8

Interpretation & Execution

Internalfile

Source Code

(xxx.c)

text file

editor

interpret

edit interpreter

Instructions inmachine languageCPU specific

CPU

Interpretation & Execution

Memory

Edition

9

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?

System A System B

X

How can it run on System B?

Compiled program compiled on System A

10

Java’s Approach - illustrated

System A System B System N...

JVM-A JVM-B JVM-N

Compiled Java byte code (not in any specific machine language)

Compiled once and runs everywhere

11

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.

12

Bytecodeprogram

(xxx.class)

Source Code

(xxx.java)

text filesaved on disk

editor

compile

edit Compiler(javac)

Instructions inbytecodesaved on disk

CPU

load

Execution

Compilation

Bytecode and JVM

Memory

JVM

interpret

Edition

13

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 ...

14

Abstraction

15

Encapsulation

16

Modularity

17

Hierarchy: composition

18

Hierarchy: taxonomy

19

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

20

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

21

Steps for Java Programming Analysis Design Implementation

Edit your code Compile your code

Testing Execute your code (using test data) [Debug your code]

22

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

top related