1 / 23

Algorithm Programming 1 (using Java) 89-210

Algorithm Programming 1 (using Java) 89-210. Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko. Course objectives. Learning Professional Programming by Algorithmic Examples Learning Java Object Oriented Programming Design Patterns Multithreaded Programming GUI

norina
Download Presentation

Algorithm Programming 1 (using Java) 89-210

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. Algorithm Programming 1(using Java) 89-210 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko

  2. Course objectives • Learning Professional Programming • by Algorithmic Examples • Learning Java • Object Oriented Programming • Design Patterns • Multithreaded Programming • GUI • Run-time Class Information

  3. Course Requirements • Exercises/Projects, 25 % of the grade • Programming Exercises • One step-by-step Project • Time schedules are strict, no late exercises are accepted. • They will be handed on via the Department Submitec utility • Cheating is punished by Discipline Assembly( Very strictly ) • Exam 75 %

  4. What is Java ? • (Almost) Fully OO Programming Language. • Syntax: Similar to C++, Different in some Semantics and Internals. • Architecture Neutral • World-Wide-Web programming language

  5. History • Early 1990s, Oak, by Sun • For embedded systems in Consumer Electronic Devices • 1994, Sun decided to adapt Oak to Web • January 1995, renamed as Java, • Building Web based applications • May 1995, Sun’s first JDK (Java Development Kit) • With HotJava capable of running Applets • 1998, Sun released Java 2 SDK (J2SDK) • By time Bunch of Libraries are added

  6. Object Oriented Reminders • Abstraction • Encapsulation • Inheritance • Abstract classes • Polymorphism • By Inheritance • By Overloading • Interfacing • Constructors and Finalizers

  7. Design Patterns • Design Patterns help you learn from others’ successes, instead of your failures • Separate things that change, from the things that doesn’t change • Elegant and Cheap-to-Maintain • Inheritance can be thought as a DP, and so Composition, etc. • Iterator (Enumeration in Java 1.0 and 1.1)

  8. Resources • Internet • http://java.sun.com For Tutorials and Downloads • http://java.sun.com/j2se/1.4.2/docs/api/ API of Java 1.4 • http://java.sun.com/j2se/1.5.0/docs/api/ API of Java 1.5 • http://java.sun.com/docs/books/tutorial/reallybigindex.html • Development Environment • NetBeans ide • Symantec’s Cofe • Borland’s JBuilder • Books • Developing Java SoftwareRussel Winder & Graham Roberts 2nd Edition, 2000 John Wiley and Sons, Ltd. • Java 2 CompleteGemma O’Sullivan1998, Sybex • Thinking in JavaBruce Eckel 3rd Edition (free to download) • Design PatternsErich Gamma, Richard Helm, Ralph Johnson, John Vlissides1995, Addison Wesley • Other Java Books …

  9. JDK, J2SE • J2SE, J2EE and J2ME • JFC : Java Foundation Classes • Java2D : 2D Graphics • JavaBeans : Java object component technology • Servlets : Web Server • JavaHelp : For creating help systems • etc. • javac : Compiler to Byte-Code • java : Loads a byte-code into JVM and executes • javap : Byte-code viewer • javadoc : Creates HTML documentation for the code. • jdb : Java debugger • appletviewer : To view applets from within an HMTL page

  10. Application Example • Programs • Applications • Applets • Programs are Case Sensitive • Example: HelloWorldApp.java file public class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } }

  11. Application Example • javac HelloWorldApp.java • Compiles and creates ByteCode in file “HelloWorldApp.class” • java HelloWorldApp • Looks for the file “HelloWorldApp.class” and for the class “HelloWorldApp” in it, and starts to run the static “main” function.

  12. Applet Example // HelloWorld.java file import java.applet.*; import java.awt.*; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } // Hello.html file <HTML> <HEAD><TITLE>A Simple Program</TITLE></HEAD> <BODY>Here is the output of my program: <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25></APPLET> </BODY> </HTML>

  13. JVM • Source Code • Java Compiler (javac) -> ByteCode • Java Interpreter (java) - Java VM

  14. Class Class Class Class Execution Engine JNI Adapter Interface Adapter OS JVM • java -classpath …;…;…for providing paths of class libraries, and .jar files • .jar files are a set of class files compressed into a single file

  15. Java Bytecode • Like C++ Assembly, JavaByteCode • We need to check ByteCode • For Performance and Memory Usage Tuning • Size and Execution Speed • Debugging

  16. Java Bytecode - Example class Employee { private String name ; private int idNumber ; public Employee(String strName, int num) { name = strName ; idNumber = num ; storeData(strName,num) ; } public String employeeName() { return name ; } public int employeeNumber() { return idNumber ; } private void storeData(String s, int num) { // ... } } javac Employee.java creates Employee.class javap –c Employee to print to string the byte-code in visual form

  17. Java Bytecode - Example public java.lang.String employeeName(); Code: 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturn public int employeeNumber(); Code: 0: aload_0 1: getfield #3; //Field idNumber:I 4: ireturn } Compiled from "Employee.java" class Employee extends java.lang.Object{ public Employee(java.lang.String,int); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: aload_1 6: putfield #2; //Field name:Ljava/lang/String; 9: aload_0 10: iload_2 11: putfield #3; //Field idNumber:I 14: aload_0 15: aload_1 16: iload_2 17: invokespecial #4; //MethodstoreData:(Ljava/lang/String;I)V 20: return

  18. Java Bytecode - opcodes • opcodes • a... : opcode is manipulating type object ref. • i… : opcode is manipulation type integer • b… : byte, • c... : char, • d… : double, etc.

  19. Java Bytecode - JVM • JVM • Stack-based machine • Each thread has a JVM stack which stores Frames • A frame is created each time a method is invoked • Each Frame has • Operand Stack • An array of Local variables • A reference to Runtime constant pool of the class of the current method

  20. Java Bytecode – JVM diagram

  21. Java Bytecode • Local variables • 0th : this (for constructors or instant methods) • Then parameters • Then local variables • Operand stack • LIFO stack, to pop and push values • Used to receive return values • Example public string employeeName() { return name ; } public java.lang.String employeeName(); Code: 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturn • aload_0 : pushes this pointer into stack • getfield #2 : this popped+2 added and get reference (for name) from runtime constant pool of the class. This is loaded to the stack • areturn : returns the top value of the stack (name). It is popped from operand stack and pushed to the operand stack of calling method • The real machine code is : 2A B4 00 02 B0

  22. Java Bytecode public Employee(String strName, int num) { name = strName ; idNumber = num ; storeData(strName,num) ; } public Employee(java.lang.String,int); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: aload_1 6: putfield #2; //Field name:Ljava/lang/String; 9: aload_0 10: iload_2 11: putfield #3; //Field idNumber:I 14: aload_0 15: aload_1 16: iload_2 17: invokespecial #4; //Method storeData:(Ljava/lang/String;I)V 20: return

  23. String and StringBuffer • In package java.lang • StringBuffer is modifiable version of String class • String has • length • substring extraction • Finding and matching • String comparison • Uppercase and lowercase conversion • Leading and trailing whitespace elimination • Conversion to/from char arrays • Conversion from primitive types to String • Appending strings • Inserting strings

More Related