1 / 22

CSC204 – Programming I

CSC204 – Programming I. Lecture 2 Intro to OOP with Java. Topics . Ways to compile and execute a program Java’s approach Considerations for software design Object and class [Running a simple Java program]. Automation Starts from Programs. What is a program?

Download Presentation

CSC204 – Programming I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC204 – Programming I Lecture 2 Intro 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. disk A Simple Computer 0 printer 1 2 processor 3 4 5 4194300 4194301 4194302 display 4194303 memory keyboard

  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 Compilation editor CPU Source Code (xxx.c) compiler edit Memory Compiled File (a.out) load text file saved on disk compile Instructions in machine language CPU specific saved on disk Execution 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 editor CPU Source Code (xxx.c) interpreter edit Memory text file interpret Internal file Instructions in machine language CPU specific Interpretation & Execution 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? How can it run on System B? X Compiled program compiled on System A System A System B

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

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

  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!Recipientservice (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

More Related