11/12/2016
Object Oriented
programming
JAVA PROGRAMMING REVIEW
BY
MOHAMMED BAJALAN
What is
Programming?
1
11/12/2016
Many Aspects of Programming
● 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
o Java was developed by Sun Microsystems
(now part of Oracle)
o it is meant to run on many “platforms”
without change, from desktop to cell phones
o platform independence works quite well
o But Java isn’t sufficient by itself: many layers
of software in a modern computer
2
11/12/2016
What do you get in return?
3
11/12/2016
Program Contains
Class - A class can be defined as a template/ blue print
that describes the behaviors/states that object of its type
support.
Object - Objects have states and behaviors. Example: A
dog has states - color, name, breed as well as behaviors -
wagging, barking, eating. An object is an instance of a
class.
Methods - A method is basically a behavior. A class can contain
many methods. It is in methods where the logics are written, data is
manipulated and all the actions are executed.
Instance Variables - Each object has its unique set of instance
variables. An object's state is created by the values assigned to these
instance variables.
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
public class Lincoln
{
//-----------------------------------------------------------------
// Prints a presidential quote.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Java Program Structure
// comments about the class
public class MyProgram
{
class header
class body
Comments can be placed almost anywhere
}
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
Java Program Structure
// comments about the class
public class MyProgram
{
// comments about the method
public static void main (String[] args)
{
method header
method body
}
7
11/12/2016
Comments
// this comment runs to the end of the line
/* this comment runs to the terminating
symbol, even across line breaks */
/** this is a javadoc comment */
Basic Data Types
byte: 128 to -127
short: 32,767 to -32,768
int:- 2,147,483,648. to 2,147,483,647
long:9,223,372,036,854,775,807 to -9,223,372,036,854,775,808
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
Compare & Equal
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);
MAX & Min
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 !!
public class Smschecker {
public static void main(String args[]) {
String sms= “i miss all of you";
int len = sms.length();
System.out.println( "String Length is : " + len );
}
}
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");
}
Another type of loops
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
Another Type of loops
for(initialization; Boolean_expression; update) {
//Statements
}
EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE | EXAMPLE |
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
Any Question
16