23
Algorithm Programming 1 (using Java) 89-210 Bar-Ilan University 2007-2008 חחח"חby Moshe Fresko

Algorithm Programming 1 (using Java) 89-210

  • Upload
    norina

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Algorithm Programming 1 (using Java)  89-210

Algorithm Programming 1(using Java) 89-210

Bar-Ilan University

תשס"ח 2007-2008

by Moshe Fresko

Page 2: Algorithm Programming 1 (using Java)  89-210

Course objectives Learning Professional Programming

by Algorithmic Examples Learning Java Object Oriented Programming Design Patterns Multithreaded Programming GUI Run-time Class Information

Page 3: Algorithm Programming 1 (using Java)  89-210

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 %

Page 4: Algorithm Programming 1 (using Java)  89-210

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

Page 5: Algorithm Programming 1 (using Java)  89-210

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

Page 6: Algorithm Programming 1 (using Java)  89-210

Object Oriented Reminders Abstraction Encapsulation Inheritance Abstract classes Polymorphism

By Inheritance By Overloading Interfacing

Constructors and Finalizers

Page 7: Algorithm Programming 1 (using Java)  89-210

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)

Page 8: Algorithm Programming 1 (using Java)  89-210

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 Software

Russel 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 …

Page 9: Algorithm Programming 1 (using Java)  89-210

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

Page 10: Algorithm Programming 1 (using Java)  89-210

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!"); }

}

Page 11: Algorithm Programming 1 (using Java)  89-210

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.

Page 12: Algorithm Programming 1 (using Java)  89-210

Applet Example// HelloWorld.java fileimport 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>

Page 13: Algorithm Programming 1 (using Java)  89-210

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

Page 14: Algorithm Programming 1 (using Java)  89-210

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

ClassClass Class Class

Execution Engine

Adapter Interface

Adapter

OS

JNI

Page 15: Algorithm Programming 1 (using Java)  89-210

Java Bytecode

Like C++ Assembly, JavaByteCode

We need to check ByteCode For Performance and Memory Usage Tuning Size and Execution Speed Debugging

Page 16: Algorithm Programming 1 (using Java)  89-210

Java Bytecode - Exampleclass 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.classjavap –c Employee to print to string the byte-code in visual form

Page 17: Algorithm Programming 1 (using Java)  89-210

Java Bytecode - ExampleCompiled 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

public java.lang.String employeeName(); Code: 0: aload_0 1: getfield #2;

//Field name:Ljava/lang/String;

4: areturnpublic int employeeNumber(); Code: 0: aload_0 1: getfield #3;

//Field idNumber:I 4: ireturn}

Page 18: Algorithm Programming 1 (using Java)  89-210

Java Bytecode - opcodes opcodes

a... : opcode is manipulating type object ref. i… : opcode is manipulation type integer b… : byte, c... : char, d… : double, etc.

Page 19: Algorithm Programming 1 (using Java)  89-210

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

Page 20: Algorithm Programming 1 (using Java)  89-210

Java Bytecode – JVM diagram

Page 21: Algorithm Programming 1 (using Java)  89-210

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

Examplepublic 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

Page 22: Algorithm Programming 1 (using Java)  89-210

Java Bytecodepublic 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

Page 23: Algorithm Programming 1 (using Java)  89-210

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