Professional Documents
Culture Documents
Java 0 1 1
Java 0 1 1
Java 0 1 1
Object Oriented
programming
BY
MOHAMMED BAJALAN
What is
Programming?
1
11/12/2016
● Programming is controlling
o computer does exactly what you tell it to
● Programming is teaching
o computer can only “learn” to do new things if you tell it how
● Programming is problem solving
o always trying to make computer do something useful, e.g. finding an optimal travel route
● Programming is creative
o must find a good solution out of many possibilities
● Programming is modelling
o describe salient (relevant) properties and behaviors of a system of components (objects)
● Programming is abstraction
o identify important features without getting lost in detail
● Programming is concrete
o must provide detailed instructions to complete task
● Programming is a craft
o A bit like architecture, engineering - disciplined and creative craft for building artifacts
Java Programs
2
11/12/2016
3
11/12/2016
Program Contains
4
11/12/2016
Basic Syntax:
Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would have different
meaning in Java.
Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Example class MyFirstJavaClass
Method Names - All method names should start with a Lower Case letter.
If several words are used to form the name of the method, then each inner word's first letter should be in
Upper Case.
Example public void myMethodName()
Program File Name - Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name (Remember Java is case sensitive) and append
'.java' to the end of the name (if the file name and the class name do not match your program will not
compile).
Example : Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved
as'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing starts from the main() method
which is a mandatory part of every Java program..
Source File
CLASS
Method1 Method1
5
11/12/2016
Lincoln.java
class body
6
11/12/2016
Name
Opening
Class of Class
Every one Classs
can access
it Arguments
public class Lincoln {
public static void main (String[] args)
{
Name of
System.out.println (“Hello Lovely Students:"); method
No return
Value
}
}
What we
Print the
wanna
family
print
7
11/12/2016
Comments
float:32-bit
double:64-bit
boolean: true or false
char:single 16-bit Unicode character.
String: string !!
8
11/12/2016
Numbers
Calculation
int x = 100;
int y =10;
int z= -312;
System.out.println(Math.pow(y,x));
System.out.println(Math.abs(z));
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
System.out.println(x+y);
System.out.println(x-y);
9
11/12/2016
Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
Integer y = 10;
System.out.println(x.equals(y));
System.out.println(x.equals(x));
Boolean m = x.equals(y);
System.out.println(Math.max(100, 50));
System.out.println(Math.max(40, 80));
System.out.println(Math.min(12.123, 12.456));
10
11/12/2016
String !!
Decision Making
11
11/12/2016
if(Boolean_expression) {
//Statements will execute if the Boolean expression
is true
}
EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE |
int x = 10;
if( x < 50 ){
System.out.print(“my mark is less than 50 !!");
}
int x = 60;
if( x < 50 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
12
11/12/2016
int x = 60;
if( x < 50 )
System.out.print("This is if statement");
System.out.print(“1");
System.out.print(“2");
else
System.out.print("This is else statement");
Switch !!
switch(expression){
case value : //Statements break;
Other value : //Statements break;
//You can have any number of case statements.
default : //Optional //Statements
}
13
11/12/2016
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
Loop
14
11/12/2016
Loop Example
while(Boolean_expression) {
//Statements
}
EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE |
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
do {
//Statements
}while(Boolean_expression);
EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE |
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
15
11/12/2016
Any Question
16