SOFTWARE TECHNOLOGY I JAVAOOP Wickramanayake HMKSK kamalee pdn

  • Slides: 22
Download presentation
SOFTWARE TECHNOLOGY - I JAVA/OOP Wickramanayake HMKSK kamal@ee. pdn. ac. lk Department of Electrical

SOFTWARE TECHNOLOGY - I JAVA/OOP Wickramanayake HMKSK kamal@ee. pdn. ac. lk Department of Electrical & Electronic Engineering Faculty of Engineering University of Peradeniya

HISTORY ● ● ● James Gosling ● Developed by Sun Microsystems in 1991 Originally

HISTORY ● ● ● James Gosling ● Developed by Sun Microsystems in 1991 Originally called Oak Originally designed for Tvs, VCRs, Toasters, etc Mostly based on C & C++ Simple and reliable Platform-neutral (WORA)

JAVA FEATURES ● Compiled and Interpreted ● Platform Independent and Portable ● Object Oriented

JAVA FEATURES ● Compiled and Interpreted ● Platform Independent and Portable ● Object Oriented ● Robust and Secure ● Distributed ● Familiar, Simple and Small ● Multithreaded and Interactive ● High Performance ● Dynamic and Extensible

Places where Java is used ● Screen phones ● Mobile phones ● ● ●

Places where Java is used ● Screen phones ● Mobile phones ● ● ● Desktop computers with Java enabled web browsers Desktop computers with Java technology based applications Servers with Java applications and Servlets

JDK, SDK & JRE

JDK, SDK & JRE

Java Development Kit Tools ● appletviewer ● javac ● javap ● javah ● javadoc

Java Development Kit Tools ● appletviewer ● javac ● javap ● javah ● javadoc ● jdb

Life of a Java Program

Life of a Java Program

Types of Java Programs ● Stand-alone applications Console based and GUI based applications that

Types of Java Programs ● Stand-alone applications Console based and GUI based applications that run of their own. Require JRE installed in the machine. ● Applets Small programs that run inside a Java enabled web browser (Java plug-in installed).

A Simple Program class My. First. Prog { public static void main(String[] args) {

A Simple Program class My. First. Prog { public static void main(String[] args) { System. out. println("I am Kamal"); } } Exercises: Modify the above program to print your name below my name. Replace System. out. println("I am Kamal") by System. out. print("I am Kamal") and see the output.

Java Platform

Java Platform

Find the square root of a number /* This program finds the square root

Find the square root of a number /* This program finds the square root of a number. This was written by kamal@ee. pdn. ac. lk. 22 -January 2003 Version 0. 1 Copyright 2003 */ import java. lang. Math; class Square. Root { public static void main(String[] args) { double x = 5. 0; // Declare, initialize double y; // Just declare y = Math. sqrt(x); System. out. println("Square root of " + x + " is " + y); } } Exercise: Draw a class diagram for the Square. Root class.

Important points. . . ● Comments /* This is a multiple line comment. .

Important points. . . ● Comments /* This is a multiple line comment. . . . / // This is an end of line comment /** This is a documentation comment. . . . */

Important points. . . (cont. ) ● import statement "import java. lang. Math; "

Important points. . . (cont. ) ● import statement "import java. lang. Math; " line tells the compiler to use the "Math" class of the "java. lang" package. Examples: import java. net. *; // import all classes of net package import javax. swing. *; // import swing package classes

Two class example class Person { String name; int age; // A string to

Two class example class Person { String name; int age; // A string to hold the name // An integer to hold the age void set. Name(String new_name) { name = new_name; } void set. Age(int new_age) { age = new_age; } void show. Info() { System. out. println(name + " is " + age + " years old"); } }

Two class example. . . class Person. Test { public static void main(String[] args)

Two class example. . . class Person. Test { public static void main(String[] args) { Person me = new Person(); // Create an object called me me. set. Name("Kamal"); me. set. Age(26); Person you = new Person(); // Create an object called you. set. Name("Your name here"); you. set. Age(85); } } me. show. Info(); you. show. Into(); // main() ends here // class Person. Test ends here

Class Diagrams

Class Diagrams

Java Program Structure ● Documentation Section ● Package Statement ● Import Statements ● Interface

Java Program Structure ● Documentation Section ● Package Statement ● Import Statements ● Interface Statements ● Class Definitions ● Main Method Class

Java Tokens ● ● ● Reserved Keywords class, int, public, static, etc. . .

Java Tokens ● ● ● Reserved Keywords class, int, public, static, etc. . . Identifiers My. Prog, me, age, . . . Literals 23, "Kamal", false, . . . Operators +, -, *, . . . Separators (, ), {, ", ", . . .

Token Exercise Identify comments, white spaces, different types of tokens in the following program.

Token Exercise Identify comments, white spaces, different types of tokens in the following program. /* Token Test Program */ import java. util. *; class Date. Test { public static void main(String[] args) { Date today = new Date(); // Create a Date object System. out. println(today); } }

Command Line Arguments /* WARNING: This program might throw an Array. Index. Out. Of.

Command Line Arguments /* WARNING: This program might throw an Array. Index. Out. Of. Bounds exception */ class My. Program { public static void main(String[] args) { System. out. println("No of arguments: " + args. length); System. out. println("First argument: " + args[0]); System. out. println("Second argument: " + args[1]); } }

Command Line Arguments. . . class My. Prog { public static void main(String[] args)

Command Line Arguments. . . class My. Prog { public static void main(String[] args) { int i = 0; while(i < args. length) { System. out. printn(args[i]); i = i + 1; } } } Exercises: Write a program to print integers from 0 to 10 using a while loop. Write a program to print integers from 10 to 0 using a while loop.

Wisdom comes not from extensive reading, but from inward inspection and reflection

Wisdom comes not from extensive reading, but from inward inspection and reflection