Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC204 – Programming I Lecture 2 Intro to OOP with Java.

Similar presentations


Presentation on theme: "1 CSC204 – Programming I Lecture 2 Intro to OOP with Java."— Presentation transcript:

1 1 CSC204 – Programming I Lecture 2 Intro to OOP with Java

2 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 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 4 processor A Simple Computer 0 1 2 3 4 5 4194300 4194301 4194302 4194303 disk keyboard display printer memory

5 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 6 Compilation & Execution Compiled File (a.out) Source Code (xxx.c) text file saved on disk editor compile edit compiler Instructions in machine language CPU specific saved on disk Memory CPU load Execution Compilation Edition

7 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 8 Interpretation & Execution Internal file Source Code (xxx.c) text file editor interpret edit interpreter Instructions in machine language CPU specific CPU Interpretation & Execution Memory Edition

9 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 ASystem B X How can it run on System B? Compiled program compiled on System A

10 10 Java’s Approach - illustrated System ASystem BSystem N... JVM-A JVM-BJVM-N Compiled Java byte code (not in any specific machine language) Compiled once and runs everywhere

11 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 12 Bytecode program (xxx.class) Source Code (xxx.java) text file saved on disk editor compile edit Compiler (javac) Instructions in bytecode saved on disk CPU load Execution Compilation Bytecode and JVM Memory JVM interpret Edition

13 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 14 Abstraction

15 15 Encapsulation

16 16 Modularity

17 17 Hierarchy: composition

18 18 Hierarchy: taxonomy

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


Download ppt "1 CSC204 – Programming I Lecture 2 Intro to OOP with Java."

Similar presentations


Ads by Google