Upload
vincent-harper
View
226
Download
4
Tags:
Embed Size (px)
Citation preview
SOFTWARE TECHNOLOGY - IJAVA/OOP
Wickramanayake HMKSK
Department of Electrical & Electronic EngineeringFaculty of Engineering
University of PeradeniyaPeradeniya
HISTORY● 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)James Gosling
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● Desktop computers with Java enabled web
browsers● Desktop computers with Java technology based
applications● Servers with Java applications and Servlets
JDK, SDK & JRE
Java Development Kit Tools
● appletviewer● javac● java● javap● javah● javadoc● jdb
Life of a Java Program
Types of Java Programs
● Stand-alone applicationsConsole based and GUI based applications that run of their own. Require JRE installed in the machine.
● AppletsSmall programs that run inside a Java enabled web browser (Java plug-in installed).
A Simple Program
class MyFirstProg {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.
MyFirstProg
main(args : String[])
Java Platform
Find the square root of a number/*
This program finds the square root of a number.This was written by [email protected] 2003 Version 0.1Copyright 2003
*/import java.lang.Math;
class SquareRoot {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 SquareRoot class.
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;" line tells the compiler to use the "Math" class of the "java.lang" package.
Examples:import java.net.*;// import all classes of net packageimport javax.swing.*; // import swing package classes
Two class exampleclass Person {
String name; // A string to hold the nameint age; // An integer to hold the age
void setName(String new_name) {name = new_name;
}
void setAge(int new_age) {age = new_age;
}
void showInfo() {System.out.println(name + " is " + age + " years old");
}}
Two class example...class PersonTest {
public static void main(String[] args) {Person me = new Person(); // Create an object called meme.setName("Kamal");me.setAge(26);
Person you = new Person(); // Create an object called youyou.setName("Your name here");you.setAge(85);
me.showInfo();you.showInto();
} // main() ends here
} // class PersonTest ends here
Class Diagrams
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...● Identifiers
MyProg, me, age, ...● Literals
23, "Kamal", false, ...● Operators
+, -, *, ...● Separators
(, ), {, ",", ...
Token ExerciseIdentify comments, white spaces, different types of tokens in thefollowing program.
/* Token Test Program */import java.util.*;
class DateTest {public static void main(String[] args) {
Date today = new Date(); // Create a Date objectSystem.out.println(today);
}}
Command Line Arguments
/*WARNING:This program might throw an ArrayIndexOutOfBoundsexception
*/
class MyProgram {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 MyProg {
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 fromextensive reading, but frominward inspection and reflection