Programming with Java Woochang Lim SNPL 1 Java

  • Slides: 8
Download presentation
Programming with Java Woochang Lim SNPL 1

Programming with Java Woochang Lim SNPL 1

Java Merits of Java Language Object-Oriented Programming Language Interprete: Short development cycle Portable: No

Java Merits of Java Language Object-Oriented Programming Language Interprete: Short development cycle Portable: No modification of codes for a different system. Robust: Memory management by Java run-time system. Pointer doesn’t need. Multithreading: More powerful graphic applications Adaptable: Dynamic loading. Adaptable for any systems Secure: Including the security for virus and tampering. Compile and Run Source code *. java SNPL Compiler javac. exe Bytecode files *. class Interpreter java. exe Run 2

Basic Systems of Java java. lang – Basic imported set. Include the definition of

Basic Systems of Java java. lang – Basic imported set. Include the definition of objects and classes for the inheritance of the class. java. io – Standard I/O library. java. net support the network I/O such as socket, telnet interface, and URL. java. util - Container and utility classes such as Dictionary, Hash. Table, Stack, cryptograph, Date, and Time classes. java. awt – Abstract windowing toolkit. Include basic interfaces (such as events, colors, and fonts) and classes for buttons and stroll bar. Import these basic systems in the top of your code to use the class and utilities. e. g. import java. io. *; SNPL 3

Generic Program Java Application: Run in the console public class Hello { public static

Generic Program Java Application: Run in the console public class Hello { public static void main(String args[]){ System. out. println(“Hello World!”); } } Java Application: Run in the Web browser. Need the HTML file to run it. import java. applet. Applet; public class Hello. Applet extends Applet{ String str; public void init(){ str = new String(“Hello World!”); } public void paint(Graphics g){ g. draw. String(str, 20); } } SNPL 4

Data Types Description Type Size Integer byte, short, int, long 1 B, 2 B,

Data Types Description Type Size Integer byte, short, int, long 1 B, 2 B, 4 B, 8 B Floating Point float, double Single, double precision Single Character char 16 bit Logical boolean 1 bit (True or False) Representation Meaning j=10 L; j=10 l; Long integer i=10. ; Decimal (double) i=10. 0 F; i=10. 0 f; Float (single) i=3. 01 e+8; Scientific i=0 x. A; Hexadecimal i=O 17; Octal SNPL 5

Array int[] i; Declare integer array double[] x = new double[10]; Declare & allocate

Array int[] i; Declare integer array double[] x = new double[10]; Declare & allocate memory (create) double[][] y = new double[8][9]; 2 D arrayname[I][k] = i*j; Assigns value to array element int a[3] = {1, 2, 3}; Assign values to array elements SNPL 6

Math Functions and Flow Control Math Functions: e. g. Math. sin(b); sin, cos, tan,

Math Functions and Flow Control Math Functions: e. g. Math. sin(b); sin, cos, tan, asin, acos, atan, exp, log, pow(x, y), sqrt, random, abs max, min, ceil, floor, etc. Flow control for (i=0; i<100; i++) {statements; } if (conditions) {statements; } else {statements; } while (conditions) {statements; } do {statements; } while (conditions) switch (month) { case 1: s=”Jan”; break; case 2: s=“Dec”; break; } SNPL 7

Input and Output import java. io. *; public class Hello { public static void

Input and Output import java. io. *; public class Hello { public static void main(String[] argv) throws IOException, File. Not. Found. Exception { int i; String str = new String("Hello World!n"); System. out. println(str); // Print to screen. // Print to file Print. Writer q = new Print. Writer(new File. Output. Stream("out. dat"), true); q. println(str); File. Input. Stream b = new File. Input. Stream("out. dat"); int s = b. read(); System. out. print((char) s); for(i=0; i<5; i++){ s = b. read(); System. out. print((char) s); } } } SNPL 8