SlideShare a Scribd company logo
1 of 73
Diploma in Information Technology
Module VIII: Programming with Java
Rasan Samarasinghe
ESOFT Computer Studies (pvt) Ltd.
No 68/1, Main Street, Pallegama, Embilipitiya.
Contents
1. Introduction to Java
2. Features of Java
3. What you can create by Java?
4. Start Java Programming
5. Creating First Java Program
6. Java Virtual Machine
7. Basic Rules to Remember
8. Keywords in Java
9. Comments in Java Programs
10. Printing Statements
11. Primitive Data Types in Java
12. Arithmetic Operators
13. Assignment Operators
14. Comparison Operators
15. Logical Operators
16. If Statement
17. If… Else Statement
18. If… Else if… Else Statement
19. Nested If Statement
20. While Loop
21. Do While Loop
22. For Loop
23. Reading User Input
24. Arrays
25. Two Dimensional Arrays
26. Objects and Classes
27. Java Classes
28. Java Objects
29. Methods with Return Value
30. Methods without Return Value
31. Method Overloading
32. Variable Types
33. Inheritance
34. Method Overriding
35. Access Modifiers
36. Packages
37. GUI Applications in Java
38. Java Applets
Introduction to Java
• Developed by Sun Microsystems (has merged
into Oracle Corporation later)
• Initiated by James Gosling
• Released in 1995
• Java has 3 main versions as Java SE, Java EE
and Java ME
Features of Java
 Object Oriented
 Platform independent
 Simple
 Secure
 Portable
 Robust
 Multi-threaded
 Interpreted
 High Performance
What you can create by Java?
• Desktop (GUI) applications
• Enterprise level applications
• Web applications
• Web services
• Java Applets
• Mobile applications
Start Java Programming
What you need to program in Java?
Java Development Kit (JDK)
Microsoft Notepad or any other text editor
Command Prompt
Creating First Java Program
public class MyFirstApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
MyFirstApp.java
Java Virtual Machine (JVM)
Java Virtual Machine (JVM)
1. When Java source code (.java files) is
compiled, it is translated into Java bytecodes
and then placed into (.class) files.
2. The JVM executes Java bytecodes and run
the program.
Java was designed with a concept of write once and
run anywhere. Java Virtual Machine plays the
central role in this concept.
Basic Rules to Remember
Java is case sensitive…
Hello not equals to hello
Basic Rules to Remember
Class name should be a single word and it
cannot contain symbols and should be started
with a character…
Wrong class name Correct way
Hello World HelloWorld
Java Window Java_Window
3DUnit Unit3D
“FillForm” FillForm
public class MyFirstApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Basic Rules to Remember
Name of the program file should exactly match
the class name...
Save as MyFirstApp.java
Basic Rules to Remember
Main method which is a mandatory part of
every java program…
public class MyFirstApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Basic Rules to Remember
Tokens must be separated by Whitespaces
Except ( ) ; { } . [ ] + - * /
public class MyFirstApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Keywords in Java
Comments in Java Programs
Comments for single line
// this is a single line comment
For multiline
/*
this is
a multiline
comment
*/
Printing Statements
System.out.print(“your text”); //prints text
System.out.println(“your text”); //prints text
and create a new line
System.out.print(“line onen line two”);//prints
text in two lines
Primitive Data Types in Java
Keyword Type of data the variable will store Size in memory
boolean true/false value 1 bit
byte byte size integer 8 bits
char a single character 16 bits
double double precision floating point decimal number 64 bits
float single precision floating point decimal number 32 bits
int a whole number 32 bits
long a whole number (used for long numbers) 64 bits
short a whole number (used for short numbers) 16 bits
Variable Declaration in Java
Variable declaration
type variable_list;
Variable declaration and initialization
type variable_name = value;
Variable Declaration in Java
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints,
initializing d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation
of pi.
char x = 'x'; // the variable x has the value 'x'.
Arithmetic Operators
Operator Description Example
+ Addition A + B will give 30
- Subtraction A - B will give -10
* Multiplication A * B will give 200
/ Division B / A will give 2
% Modulus B % A will give 0
++ Increment B++ gives 21
-- Decrement B-- gives 19
A = 10, B = 20
Assignment Operators
Operator Example
= C = A + B will assign value of A + B into C
+= C += A is equivalent to C = C + A
-= C -= A is equivalent to C = C - A
*= C *= A is equivalent to C = C * A
/= C /= A is equivalent to C = C / A
%= C %= A is equivalent to C = C % A
Comparison Operators
Operator Example
== (A == B) is false.
!= (A != B) is true.
> (A > B) is false.
< (A < B) is true.
>= (A >= B) is false.
<= (A <= B) is true.
A = 10, B = 20
Logical Operators
Operator Name Example
&& AND (A && B) is False
|| OR (A || B) is True
! NOT !(A && B) is True
A = True, B = False
If Statement
if(Boolean_expression){
//Statements will execute if the Boolean
expression is true
}
If Statement
Boolean
Expression
Statements
True
False
If… Else Statement
if(Boolean_expression){
//Executes when the Boolean expression is
true
}else{
//Executes when the Boolean expression is
false
}
If… Else Statement
Boolean
Expression
Statements
True
False
Statements
If… Else if… Else Statement
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the none of the above condition
is true.
}
If… Else if… Else Statement
Boolean
expression 1
False
Statements
Boolean
expression 2
Boolean
expression 3
Statements
Statements
False
False
Statements
True
True
True
Nested If Statement
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is
true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is
true
}
}
Nested If Statement
Boolean
Expression 1
True
False
Statements
Boolean
Expression 2
True
False
While Loop
while(Boolean_expression){
//Statements
}
While Loop
Boolean
Expression
Statements
True
False
Do While Loop
do{
//Statements
}while(Boolean_expression);
Do While Loop
Boolean
Expression
Statements
True
False
For Loop
for(initialization; Boolean_expression; update){
//Statements
}
For Loop
Boolean
Expression
Statements
True
False
Update
Initialization
Nested Loop
Boolean
Expression
True
False
Boolean
Expression
Statements
True
False
Reading User Input by the Keyboard
import java.io.*;
public class DemoApp{
public static void main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print(“Enter your text: “);
String txt = br.readLine();
System.out.println(“You have entered:” + txt);
}
}
Arrays
10 30 20 50 15 35
0 1 2 3 4 5
Size = 6
Element Index No
An Array can hold many values in a same
data type under a single name
A single dimensional array
Building a Single Dimensional Array
// Creating an Array
DataType[] ArrayName = new DataType[size];
// Assigning values
ArrayName[index] = value;
ArrayName[index] = value;
……..
Building a Single Dimensional Array
char[] letters = new char[4];
letters[0] = ‘a’;
letters[1] = ‘b’;
letters[2] = ‘c’;
letters[3] = ‘d’;
0 1 2 3
a b c d
0 1 2 3
letters
letters
Values in an Array can access
by referring index number
Building a Single Dimensional Array
//using an array initializer
DataType[] ArrayName = {element 1, element 2,
element 3, … element n}
int[] points = {10, 20, 30, 40, 50}; 10 20 30 40
0 1 2 3
points
50
4
Manipulating Arrays
Finding the largest
value of an Array
Sorting an Array
15
50
35
25
10
2
1
5
4
3
1
2
3
4
5
Two Dimensional Arrays
10 20 30
100 200 300
0 1 2
0
1
int[][] abc = new int[2][3];
abc[0][0] = 10;
abc[0][1] = 20;
abc[0][2] = 30;
abc[1][0] = 100;
abc[1][1] = 200;
abc[1][2] = 300;
Rows Columns
Column Index
Row Index
Java Objects and Classes
Java Classes
Method
Dog
name
color
bark()
class Dog{
String name;
String color;
public Dog(){
}
public void bark(){
System.out.println(“dog is barking!”);
}
}
Attributes
Constructor
Java Objects
Dog myPet = new Dog(); //creating an object
//Assigning values to Attributes
myPet.name = “Scooby”;
myPet.color = “Brown”;
//calling method
myPet.bark();
Methods
Method is a group of statements to perform a
specific task.
• Methods with Return Value
• Methods without Return Value
Methods with Return Value
public int num1, int num2){
int result;
if (num1 > num2){
result = num1;
}else{
result = num2;
}
return result;
}
Access modifier
Return type
Method name
parameters
Return value
Method body
Methods without Return Value
public void {
System.out.println(“your text: “ + txt)
}
Access modifier
Void represents no return value
Method name
parameter
Method
body
Method Overloading
public class Car{
public void Drive(){
System.out.println(“Car is driving”);
}
public void Drive(int speed){
System.out.println(“Car is driving in ” + speed +
“kmph”);
}
}
Variable Types
Variables in a Class can be categorize into
three types
1. Local Variables
2. Instance Variables
3. Static/Class Variables
Local Variables
• Declared in methods,
constructors, or blocks.
• Access modifiers cannot
be used.
• Visible only within the
declared method,
constructor or block.
• Should be declared with
an initial value.
public class Vehicle{
int number;
String color;
static String model;
void Drive(){
int speed = 100;
System.out.print(“vehicle
is driving in “ + speed +
“kmph”);
}
}
Instance Variables
• Declared in a class, but
outside a method,
constructor or any block.
• Access modifiers can be
given.
• Can be accessed directly
anywhere in the class.
• Have default values.
• Should be called using an
object reference to access
within static methods and
outside of the class.
public class Vehicle{
int number;
String color;
static String model;
void Drive(){
int speed = 100;
System.out.print(“vehicle
is driving in “ + speed +
“kmph”);
}
}
Static/Class Variables
• Declared with the static
keyword in a class, but
outside a method,
constructor or a block.
• Only one copy for each
class regardless how
many objects created.
• Have default values.
• Can be accessed by
calling with the class
name.
public class Vehicle{
int number;
String color;
static String model;
void Drive(){
int speed = 100;
System.out.print(“vehicle
is driving in “ + speed +
“kmph”);
}
}
Inheritance
class Vehicle{
//attributes and methods
}
class Car extends Vehicle{
//attributes and methods
}
class Van extends Vehicle{
//attributes and methods
}
Vehicle
Car Van
Method Overriding
class Vehicle{
public void drive(){
System.out.println(“Vehicle is driving”);
}
}
class Car extends Vehicle{
public void drive(){
System.out.println(“Car is driving”);
}
}
Access Modifiers
Access
Modifiers
Same
class
Same
package
Sub class
Other
packages
public Y Y Y Y
protected Y Y Y N
No access
modifier
Y Y N N
private Y N N N
Packages
A Package can be defined as a grouping of
related types (classes, interfaces,
enumerations and annotations) providing
access protection and namespace
management.
//At the top of your source code
import <package name>.*;
import <package name>.<class name>;
GUI Applications in Java
• Abstract Window Toolkit
• Frame Class
• Layouts
• Label Class
• TextField Class
• Button Class
• Events
Abstract Window Toolkit
The Abstract Window Toolkit (AWT) is a
package of JDK classes for creating GUI
components such as buttons, menus, and
scrollbars for applets and standalone
applications.
import java.awt.*;
Frame Class
Frame myFrame = new Frame(“My Frame”);
myFrame.setSize(300,200);
myFrame.setVisible(true);
Layouts
myFrame.setLayout(new FlowLayout()); myFrame.setLayout(new GridLayout(2,3));
myFrame.setLayout(null);
Label Class
Label lbl = new Label("Hello World");
lbl.setBounds(120,40,120,25);
myFrame.add(lbl);
TextField Class
TextField txt = new TextField();
txt.setBounds(100,90,120,25);
myFrame.add(txt);
Button Class
Button btn = new Button("OK");
btn.setBounds(120,150,60,25);
myFrame.add(btn);
Events
An event is when something special happens within a
Graphical User Interface.
Things like buttons being clicked, the mouse moving,
text being entered into text fields, the program closing,
etc.. then an event will trigger.
How Events Handling Work?
Java Applets
An applet is a Java program that runs in a Web
browser.
Creating a Java Applet
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);
}
}
<applet code="HelloWorldApplet.class" width="320"
height="120"></applet>
Write a Java Applet and
compile it
Embed it in a HTML file
The End
http://twitter.com/rasansmn

More Related Content

What's hot

Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Vadim Dubs
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2Prerna Sharma
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1Prerna Sharma
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does notSergey Bandysik
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc csKALAISELVI P
 

What's hot (20)

Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python The basics
Python   The basicsPython   The basics
Python The basics
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
02basics
02basics02basics
02basics
 
M C6java2
M C6java2M C6java2
M C6java2
 
Python programming
Python  programmingPython  programming
Python programming
 
M C6java3
M C6java3M C6java3
M C6java3
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
M C6java7
M C6java7M C6java7
M C6java7
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
 
Javascript
JavascriptJavascript
Javascript
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 

Viewers also liked

DISE - Introduction to Software Engineering
DISE - Introduction to Software EngineeringDISE - Introduction to Software Engineering
DISE - Introduction to Software EngineeringRasan Samarasinghe
 
DIWE - Introduction to Web Technologies
DIWE - Introduction to Web TechnologiesDIWE - Introduction to Web Technologies
DIWE - Introduction to Web TechnologiesRasan Samarasinghe
 
DITEC - Expose yourself to Internet & E-mail (second update)
DITEC - Expose yourself to Internet & E-mail (second update) DITEC - Expose yourself to Internet & E-mail (second update)
DITEC - Expose yourself to Internet & E-mail (second update) Rasan Samarasinghe
 
DITEC - Expose yourself to Internet & E-mail (updated)
DITEC - Expose yourself to Internet & E-mail (updated)DITEC - Expose yourself to Internet & E-mail (updated)
DITEC - Expose yourself to Internet & E-mail (updated)Rasan Samarasinghe
 
DITEC - Expose yourself to Internet & E-mail
DITEC - Expose yourself to Internet & E-mailDITEC - Expose yourself to Internet & E-mail
DITEC - Expose yourself to Internet & E-mailRasan Samarasinghe
 
SeminaronEmpoweringSMEsThroughICTIntervention (1)
SeminaronEmpoweringSMEsThroughICTIntervention (1)SeminaronEmpoweringSMEsThroughICTIntervention (1)
SeminaronEmpoweringSMEsThroughICTIntervention (1)Sainath P
 
Java book for beginners_first chapter
Java book for beginners_first chapterJava book for beginners_first chapter
Java book for beginners_first chapterAamir Mojeeb
 
1.3 computer system devices&peripherals
1.3 computer system devices&peripherals1.3 computer system devices&peripherals
1.3 computer system devices&peripheralsFrya Lora
 
Monitoring web application response times, a new approach
Monitoring web application response times, a new approachMonitoring web application response times, a new approach
Monitoring web application response times, a new approachMark Friedman
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET PresentationRasel Khan
 
DITEC - Fundamentals in Networking
DITEC - Fundamentals in NetworkingDITEC - Fundamentals in Networking
DITEC - Fundamentals in NetworkingRasan Samarasinghe
 
DISE - Software Testing and Quality Management
DISE - Software Testing and Quality ManagementDISE - Software Testing and Quality Management
DISE - Software Testing and Quality ManagementRasan Samarasinghe
 

Viewers also liked (20)

DITEC - E-Commerce & ASP.NET
DITEC - E-Commerce & ASP.NETDITEC - E-Commerce & ASP.NET
DITEC - E-Commerce & ASP.NET
 
DITEC - Software Engineering
DITEC - Software EngineeringDITEC - Software Engineering
DITEC - Software Engineering
 
DISE - Introduction to Software Engineering
DISE - Introduction to Software EngineeringDISE - Introduction to Software Engineering
DISE - Introduction to Software Engineering
 
DIWE - Introduction to Web Technologies
DIWE - Introduction to Web TechnologiesDIWE - Introduction to Web Technologies
DIWE - Introduction to Web Technologies
 
DITEC - Expose yourself to Internet & E-mail (second update)
DITEC - Expose yourself to Internet & E-mail (second update) DITEC - Expose yourself to Internet & E-mail (second update)
DITEC - Expose yourself to Internet & E-mail (second update)
 
DITEC - Expose yourself to Internet & E-mail (updated)
DITEC - Expose yourself to Internet & E-mail (updated)DITEC - Expose yourself to Internet & E-mail (updated)
DITEC - Expose yourself to Internet & E-mail (updated)
 
DITEC - Expose yourself to Internet & E-mail
DITEC - Expose yourself to Internet & E-mailDITEC - Expose yourself to Internet & E-mail
DITEC - Expose yourself to Internet & E-mail
 
SeminaronEmpoweringSMEsThroughICTIntervention (1)
SeminaronEmpoweringSMEsThroughICTIntervention (1)SeminaronEmpoweringSMEsThroughICTIntervention (1)
SeminaronEmpoweringSMEsThroughICTIntervention (1)
 
Java book for beginners_first chapter
Java book for beginners_first chapterJava book for beginners_first chapter
Java book for beginners_first chapter
 
Drawing 1 Module
Drawing 1 ModuleDrawing 1 Module
Drawing 1 Module
 
Network
NetworkNetwork
Network
 
DISE - Programming Concepts
DISE - Programming ConceptsDISE - Programming Concepts
DISE - Programming Concepts
 
DISE - OOAD Using UML
DISE - OOAD Using UMLDISE - OOAD Using UML
DISE - OOAD Using UML
 
Basic Elements of Java
Basic Elements of JavaBasic Elements of Java
Basic Elements of Java
 
DISE - Database Concepts
DISE - Database ConceptsDISE - Database Concepts
DISE - Database Concepts
 
1.3 computer system devices&peripherals
1.3 computer system devices&peripherals1.3 computer system devices&peripherals
1.3 computer system devices&peripherals
 
Monitoring web application response times, a new approach
Monitoring web application response times, a new approachMonitoring web application response times, a new approach
Monitoring web application response times, a new approach
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET Presentation
 
DITEC - Fundamentals in Networking
DITEC - Fundamentals in NetworkingDITEC - Fundamentals in Networking
DITEC - Fundamentals in Networking
 
DISE - Software Testing and Quality Management
DISE - Software Testing and Quality ManagementDISE - Software Testing and Quality Management
DISE - Software Testing and Quality Management
 

Similar to DITEC - Programming with Java (20)

Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
The Ultimate FREE Java Course Part 2
The Ultimate FREE Java Course Part 2The Ultimate FREE Java Course Part 2
The Ultimate FREE Java Course Part 2
 

More from Rasan Samarasinghe

Managing the under performance in projects.pptx
Managing the under performance in projects.pptxManaging the under performance in projects.pptx
Managing the under performance in projects.pptxRasan Samarasinghe
 
Agile project management with scrum
Agile project management with scrumAgile project management with scrum
Agile project management with scrumRasan Samarasinghe
 
Application of Unified Modelling Language
Application of Unified Modelling LanguageApplication of Unified Modelling Language
Application of Unified Modelling LanguageRasan Samarasinghe
 
Advanced Web Development in PHP - Understanding REST API
Advanced Web Development in PHP - Understanding REST APIAdvanced Web Development in PHP - Understanding REST API
Advanced Web Development in PHP - Understanding REST APIRasan Samarasinghe
 
Advanced Web Development in PHP - Understanding Project Development Methodolo...
Advanced Web Development in PHP - Understanding Project Development Methodolo...Advanced Web Development in PHP - Understanding Project Development Methodolo...
Advanced Web Development in PHP - Understanding Project Development Methodolo...Rasan Samarasinghe
 
Advanced Web Development in PHP - Code Versioning and Branching with Git
Advanced Web Development in PHP - Code Versioning and Branching with GitAdvanced Web Development in PHP - Code Versioning and Branching with Git
Advanced Web Development in PHP - Code Versioning and Branching with GitRasan Samarasinghe
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan Samarasinghe
 
DIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image ManipulationDIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image ManipulationRasan Samarasinghe
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningRasan Samarasinghe
 
DIWE - Multimedia Technologies
DIWE - Multimedia TechnologiesDIWE - Multimedia Technologies
DIWE - Multimedia TechnologiesRasan Samarasinghe
 
DISE - Introduction to Project Management
DISE - Introduction to Project ManagementDISE - Introduction to Project Management
DISE - Introduction to Project ManagementRasan Samarasinghe
 

More from Rasan Samarasinghe (14)

Managing the under performance in projects.pptx
Managing the under performance in projects.pptxManaging the under performance in projects.pptx
Managing the under performance in projects.pptx
 
Agile project management with scrum
Agile project management with scrumAgile project management with scrum
Agile project management with scrum
 
Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agile
 
IT Introduction (en)
IT Introduction (en)IT Introduction (en)
IT Introduction (en)
 
Application of Unified Modelling Language
Application of Unified Modelling LanguageApplication of Unified Modelling Language
Application of Unified Modelling Language
 
Advanced Web Development in PHP - Understanding REST API
Advanced Web Development in PHP - Understanding REST APIAdvanced Web Development in PHP - Understanding REST API
Advanced Web Development in PHP - Understanding REST API
 
Advanced Web Development in PHP - Understanding Project Development Methodolo...
Advanced Web Development in PHP - Understanding Project Development Methodolo...Advanced Web Development in PHP - Understanding Project Development Methodolo...
Advanced Web Development in PHP - Understanding Project Development Methodolo...
 
Advanced Web Development in PHP - Code Versioning and Branching with Git
Advanced Web Development in PHP - Code Versioning and Branching with GitAdvanced Web Development in PHP - Code Versioning and Branching with Git
Advanced Web Development in PHP - Code Versioning and Branching with Git
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
DIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image ManipulationDIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image Manipulation
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web Designing
 
DIWE - Multimedia Technologies
DIWE - Multimedia TechnologiesDIWE - Multimedia Technologies
DIWE - Multimedia Technologies
 
DISE - Introduction to Project Management
DISE - Introduction to Project ManagementDISE - Introduction to Project Management
DISE - Introduction to Project Management
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 

DITEC - Programming with Java

  • 1. Diploma in Information Technology Module VIII: Programming with Java Rasan Samarasinghe ESOFT Computer Studies (pvt) Ltd. No 68/1, Main Street, Pallegama, Embilipitiya.
  • 2. Contents 1. Introduction to Java 2. Features of Java 3. What you can create by Java? 4. Start Java Programming 5. Creating First Java Program 6. Java Virtual Machine 7. Basic Rules to Remember 8. Keywords in Java 9. Comments in Java Programs 10. Printing Statements 11. Primitive Data Types in Java 12. Arithmetic Operators 13. Assignment Operators 14. Comparison Operators 15. Logical Operators 16. If Statement 17. If… Else Statement 18. If… Else if… Else Statement 19. Nested If Statement 20. While Loop 21. Do While Loop 22. For Loop 23. Reading User Input 24. Arrays 25. Two Dimensional Arrays 26. Objects and Classes 27. Java Classes 28. Java Objects 29. Methods with Return Value 30. Methods without Return Value 31. Method Overloading 32. Variable Types 33. Inheritance 34. Method Overriding 35. Access Modifiers 36. Packages 37. GUI Applications in Java 38. Java Applets
  • 3. Introduction to Java • Developed by Sun Microsystems (has merged into Oracle Corporation later) • Initiated by James Gosling • Released in 1995 • Java has 3 main versions as Java SE, Java EE and Java ME
  • 4. Features of Java  Object Oriented  Platform independent  Simple  Secure  Portable  Robust  Multi-threaded  Interpreted  High Performance
  • 5. What you can create by Java? • Desktop (GUI) applications • Enterprise level applications • Web applications • Web services • Java Applets • Mobile applications
  • 6. Start Java Programming What you need to program in Java? Java Development Kit (JDK) Microsoft Notepad or any other text editor Command Prompt
  • 7. Creating First Java Program public class MyFirstApp{ public static void main(String[] args){ System.out.println("Hello World"); } } MyFirstApp.java
  • 9. Java Virtual Machine (JVM) 1. When Java source code (.java files) is compiled, it is translated into Java bytecodes and then placed into (.class) files. 2. The JVM executes Java bytecodes and run the program. Java was designed with a concept of write once and run anywhere. Java Virtual Machine plays the central role in this concept.
  • 10. Basic Rules to Remember Java is case sensitive… Hello not equals to hello
  • 11. Basic Rules to Remember Class name should be a single word and it cannot contain symbols and should be started with a character… Wrong class name Correct way Hello World HelloWorld Java Window Java_Window 3DUnit Unit3D “FillForm” FillForm
  • 12. public class MyFirstApp{ public static void main(String[] args){ System.out.println("Hello World"); } } Basic Rules to Remember Name of the program file should exactly match the class name... Save as MyFirstApp.java
  • 13. Basic Rules to Remember Main method which is a mandatory part of every java program… public class MyFirstApp{ public static void main(String[] args){ System.out.println("Hello World"); } }
  • 14. Basic Rules to Remember Tokens must be separated by Whitespaces Except ( ) ; { } . [ ] + - * / public class MyFirstApp{ public static void main(String[] args){ System.out.println("Hello World"); } }
  • 16. Comments in Java Programs Comments for single line // this is a single line comment For multiline /* this is a multiline comment */
  • 17. Printing Statements System.out.print(“your text”); //prints text System.out.println(“your text”); //prints text and create a new line System.out.print(“line onen line two”);//prints text in two lines
  • 18. Primitive Data Types in Java Keyword Type of data the variable will store Size in memory boolean true/false value 1 bit byte byte size integer 8 bits char a single character 16 bits double double precision floating point decimal number 64 bits float single precision floating point decimal number 32 bits int a whole number 32 bits long a whole number (used for long numbers) 64 bits short a whole number (used for short numbers) 16 bits
  • 19. Variable Declaration in Java Variable declaration type variable_list; Variable declaration and initialization type variable_name = value;
  • 20. Variable Declaration in Java int a, b, c; // declares three ints, a, b, and c. int d = 3, e, f = 5; // declares three more ints, initializing d and f. byte z = 22; // initializes z. double pi = 3.14159; // declares an approximation of pi. char x = 'x'; // the variable x has the value 'x'.
  • 21. Arithmetic Operators Operator Description Example + Addition A + B will give 30 - Subtraction A - B will give -10 * Multiplication A * B will give 200 / Division B / A will give 2 % Modulus B % A will give 0 ++ Increment B++ gives 21 -- Decrement B-- gives 19 A = 10, B = 20
  • 22. Assignment Operators Operator Example = C = A + B will assign value of A + B into C += C += A is equivalent to C = C + A -= C -= A is equivalent to C = C - A *= C *= A is equivalent to C = C * A /= C /= A is equivalent to C = C / A %= C %= A is equivalent to C = C % A
  • 23. Comparison Operators Operator Example == (A == B) is false. != (A != B) is true. > (A > B) is false. < (A < B) is true. >= (A >= B) is false. <= (A <= B) is true. A = 10, B = 20
  • 24. Logical Operators Operator Name Example && AND (A && B) is False || OR (A || B) is True ! NOT !(A && B) is True A = True, B = False
  • 25. If Statement if(Boolean_expression){ //Statements will execute if the Boolean expression is true }
  • 27. If… Else Statement if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false }
  • 29. If… Else if… Else Statement if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true }else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true }else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true }else { //Executes when the none of the above condition is true. }
  • 30. If… Else if… Else Statement Boolean expression 1 False Statements Boolean expression 2 Boolean expression 3 Statements Statements False False Statements True True True
  • 31. Nested If Statement if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true } }
  • 32. Nested If Statement Boolean Expression 1 True False Statements Boolean Expression 2 True False
  • 40. Reading User Input by the Keyboard import java.io.*; public class DemoApp{ public static void main(String [] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print(“Enter your text: “); String txt = br.readLine(); System.out.println(“You have entered:” + txt); } }
  • 41. Arrays 10 30 20 50 15 35 0 1 2 3 4 5 Size = 6 Element Index No An Array can hold many values in a same data type under a single name A single dimensional array
  • 42. Building a Single Dimensional Array // Creating an Array DataType[] ArrayName = new DataType[size]; // Assigning values ArrayName[index] = value; ArrayName[index] = value; ……..
  • 43. Building a Single Dimensional Array char[] letters = new char[4]; letters[0] = ‘a’; letters[1] = ‘b’; letters[2] = ‘c’; letters[3] = ‘d’; 0 1 2 3 a b c d 0 1 2 3 letters letters Values in an Array can access by referring index number
  • 44. Building a Single Dimensional Array //using an array initializer DataType[] ArrayName = {element 1, element 2, element 3, … element n} int[] points = {10, 20, 30, 40, 50}; 10 20 30 40 0 1 2 3 points 50 4
  • 45. Manipulating Arrays Finding the largest value of an Array Sorting an Array 15 50 35 25 10 2 1 5 4 3 1 2 3 4 5
  • 46. Two Dimensional Arrays 10 20 30 100 200 300 0 1 2 0 1 int[][] abc = new int[2][3]; abc[0][0] = 10; abc[0][1] = 20; abc[0][2] = 30; abc[1][0] = 100; abc[1][1] = 200; abc[1][2] = 300; Rows Columns Column Index Row Index
  • 47. Java Objects and Classes
  • 48. Java Classes Method Dog name color bark() class Dog{ String name; String color; public Dog(){ } public void bark(){ System.out.println(“dog is barking!”); } } Attributes Constructor
  • 49. Java Objects Dog myPet = new Dog(); //creating an object //Assigning values to Attributes myPet.name = “Scooby”; myPet.color = “Brown”; //calling method myPet.bark();
  • 50. Methods Method is a group of statements to perform a specific task. • Methods with Return Value • Methods without Return Value
  • 51. Methods with Return Value public int num1, int num2){ int result; if (num1 > num2){ result = num1; }else{ result = num2; } return result; } Access modifier Return type Method name parameters Return value Method body
  • 52. Methods without Return Value public void { System.out.println(“your text: “ + txt) } Access modifier Void represents no return value Method name parameter Method body
  • 53. Method Overloading public class Car{ public void Drive(){ System.out.println(“Car is driving”); } public void Drive(int speed){ System.out.println(“Car is driving in ” + speed + “kmph”); } }
  • 54. Variable Types Variables in a Class can be categorize into three types 1. Local Variables 2. Instance Variables 3. Static/Class Variables
  • 55. Local Variables • Declared in methods, constructors, or blocks. • Access modifiers cannot be used. • Visible only within the declared method, constructor or block. • Should be declared with an initial value. public class Vehicle{ int number; String color; static String model; void Drive(){ int speed = 100; System.out.print(“vehicle is driving in “ + speed + “kmph”); } }
  • 56. Instance Variables • Declared in a class, but outside a method, constructor or any block. • Access modifiers can be given. • Can be accessed directly anywhere in the class. • Have default values. • Should be called using an object reference to access within static methods and outside of the class. public class Vehicle{ int number; String color; static String model; void Drive(){ int speed = 100; System.out.print(“vehicle is driving in “ + speed + “kmph”); } }
  • 57. Static/Class Variables • Declared with the static keyword in a class, but outside a method, constructor or a block. • Only one copy for each class regardless how many objects created. • Have default values. • Can be accessed by calling with the class name. public class Vehicle{ int number; String color; static String model; void Drive(){ int speed = 100; System.out.print(“vehicle is driving in “ + speed + “kmph”); } }
  • 58. Inheritance class Vehicle{ //attributes and methods } class Car extends Vehicle{ //attributes and methods } class Van extends Vehicle{ //attributes and methods } Vehicle Car Van
  • 59. Method Overriding class Vehicle{ public void drive(){ System.out.println(“Vehicle is driving”); } } class Car extends Vehicle{ public void drive(){ System.out.println(“Car is driving”); } }
  • 60. Access Modifiers Access Modifiers Same class Same package Sub class Other packages public Y Y Y Y protected Y Y Y N No access modifier Y Y N N private Y N N N
  • 61. Packages A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations) providing access protection and namespace management. //At the top of your source code import <package name>.*; import <package name>.<class name>;
  • 62. GUI Applications in Java • Abstract Window Toolkit • Frame Class • Layouts • Label Class • TextField Class • Button Class • Events
  • 63. Abstract Window Toolkit The Abstract Window Toolkit (AWT) is a package of JDK classes for creating GUI components such as buttons, menus, and scrollbars for applets and standalone applications. import java.awt.*;
  • 64. Frame Class Frame myFrame = new Frame(“My Frame”); myFrame.setSize(300,200); myFrame.setVisible(true);
  • 65. Layouts myFrame.setLayout(new FlowLayout()); myFrame.setLayout(new GridLayout(2,3)); myFrame.setLayout(null);
  • 66. Label Class Label lbl = new Label("Hello World"); lbl.setBounds(120,40,120,25); myFrame.add(lbl);
  • 67. TextField Class TextField txt = new TextField(); txt.setBounds(100,90,120,25); myFrame.add(txt);
  • 68. Button Class Button btn = new Button("OK"); btn.setBounds(120,150,60,25); myFrame.add(btn);
  • 69. Events An event is when something special happens within a Graphical User Interface. Things like buttons being clicked, the mouse moving, text being entered into text fields, the program closing, etc.. then an event will trigger.
  • 71. Java Applets An applet is a Java program that runs in a Web browser.
  • 72. Creating a Java Applet import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello World", 25, 50); } } <applet code="HelloWorldApplet.class" width="320" height="120"></applet> Write a Java Applet and compile it Embed it in a HTML file

Editor's Notes

  1. double avg = 45.6567; System.out.println(String.format("%.2f", avg)); // rounds and limit 2 decimal places
  2. Void welcome() ///Hello world ///Void welcome(“saman”) ////Hello Saman