SlideShare a Scribd company logo
Object Oriented
Programming with JAVA
Syllabus
1. Introduction to Object Oriented Programming
2. Class, Object, Packages and Input/Output
3. Array, String and Vector
4. Inheritance
5. Exception handling and Multithreading
6. GUI programming in JAVA
Books
Textbook
• E. Balagurusamy, ‘Programming with Java’, McGraw Hill Education.
• Herbert Schildt, ‘JAVA: The Complete Reference’, Ninth Edition, Oracle
Press.
Introduction to Object Oriented Programming
• OOP concepts: objects, class, encapsulation, abstraction, inheritance,
polymorphism, message passing
• Java Virtual Machine
• Basic programming constructs: variables, data types, operators,
unsigned right shift operator, expressions, branching and looping.
Object Oriented Programming concepts
Classes & Objects
• The class is the unit of programming
• Class – user-defined datatype
• A Java program is a collection of classes
• Each class definition (usually) in its own .java file
• The file name must match the class name
• A class describes objects (instances)
• Describes their common characteristics: is a blueprint
• Thus all the instances have these same characteristics
• These characteristics are:
• Data fields for each object
• Methods (operations) that do work on the objects
Java-Intro.pptx
Object Oriented Programming concepts
Encapsulation:
• wrapping up of data under a single unit
• the variables or data of a class is hidden from any other class and can
be accessed only through any member function of own class in which
they are declared.
Abstraction:
• by virtue of which only the essential details are displayed to the user
• Eg: man driving a car, only knows how to apply a break, does not
need to know mechanism behind it.
Object Oriented Programming concepts
Inheritance
• one class can inherit the features(fields and methods) of another
class.
• Facilitates reusability
Object Oriented Programming concepts
Polymorphism
• Ability to differentiate between entities with the same name
efficiently.
• This is done by Java with the help of the signature and declaration of
these entities.
• Eg: addition operation of integer and strings
Dynamic binding
• Process of linking a method call to the executable code in response to
the call
• This process exhibited at runtime
Object Oriented Programming concepts
Message Communication
• Objects Communicate with each other
Steps:
1. Create classes that define objects
2. Create objects from class definitions
3. Establish communication among objects
Eg:
Car c;
c.speed(c);
Features of JAVA
Compiled & Interpreted
• Two stage system
• Compiler – source code into bytecode
• Interpreter – bytecode into machine instructions
Platform independent & portable
• Programs can be easily moved from one computer system to another,
anywhere & anytime.
• Ensures portability in two ways:
• Generates bytecode instructions, which can be implemented anywhere
• Size of primitive data type is machine independent
Features of JAVA
Object-Oriented
• All the Code of the java Language is written into the classes and
objects
Robust & Secure
Robust
• Eradicates the problem of memory management by performing
automatic garbage collection
Secure
• Absence of pointers ensures that programs cannot gain access to
memory locations without proper authorization
Features of JAVA
Distributed
• Program of java is compiled onto one machine can be easily
transferred to machine and executes them on another machine
• Used to create applications on network
Simple, small & familiar
• Java removes complexity because it doesn’t use pointers, storage
classes and go to statements and java doesn’t support multiple
inheritance
Multithreaded & Interactive
High Performance
JDK, JRE & JVM
• JDK is platform specific software
• JRE provides platform to execute Java
program
• JVM Provides runtime environment in
which java bytecode can be executed
• JVM is available for many hardware and
software platforms (i.e. JVM is platform
dependent).
Write Once Run
Anywhere(WORA)
Let’s Begin Programming
class Sample //Sample Program
{
public static void main(String args[])
{
System.out.println(“Hello World!!!”);
}
}
//System class is defined in java.lang.* (default imported package)
//static – interpreter uses this method before any objects are created
/*….Multi line comment */
Identifier
Keyword
Class
Object
Method
Access
Specifier
Implementation?
Creating the program
• Using any text editor (known as source file)
• Save the file
Compiling the program
• javac filename.java //creates bytecodes file (filename.class)
Running the program
• java filename //interpreter looks for the main method
Variables
• Name given to a memory location; all the operations done on the variable
effects that memory location
• Value stored in a variable can be changed during program execution
Rules:
1. Should not begin with digit
2. Case sensitive
3. Should not be a keyword
4. White space is not allowed
5. Can be of any length
Eg: total_value, mean
How to initialize variables?
Java-Intro.pptx
Java-Intro.pptx
Constants
• Variable whose value cannot
change once it has been
assigned
Syntax:
final float pi=3.14;
• Variables as constants by just
adding the keyword “final”
when we declare the
variable.
Java-Intro.pptx
Escape Sequences
Operators
Special operators
instanceof operator
• Returns true if the object on the LHS is an instance of the class given
on RHS
eg: person instanceof Student
Dot (.) operator
• Used to access the instance variables & methods of class objects
eg:
person.age
person.salary()
Java Math methods
• java.lang.Math class contains various methods for performing basic
numeric operations such as the logarithm, cube root, and
trigonometric functions etc.
Method Description Arguments
abs Returns the absolute value
of the argument
Double, float, int, long
round Returns the closed int or
long (as per the argument)
double or float
ceil Returns the smallest
integer that is greater than
or equal to the argument
Double
floor Returns the largest integer
that is less than or equal to
the argument
Double
min Returns the smallest of the
two arguments
Double, float, int, long
max Returns the largest of the
two arguments
Double, float, int, long
Method Description Arguments
Sin Returns the Sine of the
specified argument
Double
Cos Returns the Cosine of the
specified argument
double
Tan Returns the Tangent of
the specified argument
Double
Atan2 Converts rectangular
coordinates (x, y) to
polar(r, theta) and returns
theta
Double
toDegrees Converts the arguments
to degrees
Double
Sqrt Returns the square root of
the argument
Double
toRadians Converts the arguments
to radians
Double
Java Math methods
Method Description Arguments
exp Returns the base of
natural log (e) to the
power of argument
Double
Log Returns the natural
log of the argument
double
Pow Takes 2 arguments as
input and returns the
value of the first
argument raised to
the power of the
second argument
Double
floor Returns the largest
integer that is less
than or equal to the
argument
Double
Sqrt Returns the square
root of the argument
Double
Command line argument program
class Arguments
{
public static void main(String args[])
{
System.out.println("Arguments are");
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Note:
Converting string to integer
Integer.parseInt(args[0])
Multiple classes
class Room
{
float length, breadth;
void getData(float a, float b)
{
length=a;
breadth=b;
}
}
class RoomArea
{
public static void main(String args[])
{
float area;
Room r=new Room();
r.getData(4,5);
area=r.length*r.breadth;
System.out.println(“Area of a room is:”+area);
}
}
Input
Package: java.io
I/P statement:
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
Create a
object &
allocates
memory
dynamically
Class can be
used to read
data from
keyboard
Class can be
used to read
data line by
line
Input stream
which is
typically
connected to
keyboard for
input
import java.io.*;
class Test
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter your name”);
String name=br.readLine();
System.out.println(“Welcome: ”+name);
}
}
// return type of readLine() is String
Integer.parseInt(br.readLine()); //String to integer
Scanner class Input
import java.util.Scanner; // Import the Scanner class
class MyClass
{
public static void main(String args[])
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
// return type of nextLine() is String
Integer.valueOf(myObj.nextLine()); //String to integer
Scanner class Limitation
import java.util.Scanner;
class Input
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter flight number: ");
int flightNumber1 = scanner.nextInt();
System.out.print("Enter flight2: ");
int flightNumber2 = scanner.nextInt();
System.out.print("Enter departing city");
String departingCity = scanner.nextLine();
System.out.print("Enter arrival city: ");
String arrivalCity = scanner.nextLine();
}
}
Workaround
import java.util.Scanner;
class Input1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter flight number1: ");
int flightNumber1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter departing city:");
String departingCity = scanner.nextLine();
System.out.print("Enter departing city");
String departingCity = scanner.nextLine();
System.out.print("Enter arrival city: ");
String arrivalCity = scanner.nextLine();
}
}
Branching
If-else example
public class IfElse
{
public static void main(String args[])
{
int n=7;
if(n%2==0)
System.out.print(“Even”);
else
System.out.print(“Odd”);
}
}
Switch example
class SwitchDay
{
public static void main(String args[])
{
int day=7;
switch(day)
{
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
case 3:
System.out.println(“Wednesday”);
break;
case 4 :
System.out.println(“Thursday”);
break;
case 5:
System.out.println(“Friday”);
break;
case 6 :
System.out.println(“Saturday”);
break;
case 7 :
System.out.println(“Sunday”);
break;
}
}
}
Loops
For loop While loop
• WAP to print all even integers
divisible by 6 or 7 upto 50
Do while loop
• WAP to print 10 to 20

More Related Content

PPTX
Object oriented programming1 Week 1.pptx
PPTX
Java fundamentals
PPTX
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
PPTX
Introduction to oop and java fundamentals
PPTX
Java features
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
Object oriented programming1 Week 1.pptx
Java fundamentals
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Introduction to oop and java fundamentals
Java features
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB

Similar to Java-Intro.pptx (20)

PPT
Jacarashed-1746968053-300050282-Java.ppt
PPTX
UNIT 1 : object oriented programming.pptx
PPT
SMI - Introduction to Java
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
PPTX
Programming in java basics
PPTX
Android webinar class_java_review
PPTX
Introduction to java Programming Language
PPTX
JAVA programming language made easy.pptx
PDF
Java training materials for computer engineering.pdf
PPTX
ppt_on_java.pptx
PPTX
Java basics-includes java classes, methods ,OOPs concepts
PPTX
PPTX
CSL101_Ch1.pptx
PPTX
CSL101_Ch1.pptx
PPT
CSL101_Ch1.ppt
PPT
CSL101_Ch1.ppt
PPT
skill lab-java interview preparation questions.ppt
PDF
Basic Java Programming
DOCX
OOP Lab-manual btech in cse kerala technological university
PPT
a basic java programming and data type.ppt
Jacarashed-1746968053-300050282-Java.ppt
UNIT 1 : object oriented programming.pptx
SMI - Introduction to Java
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Programming in java basics
Android webinar class_java_review
Introduction to java Programming Language
JAVA programming language made easy.pptx
Java training materials for computer engineering.pdf
ppt_on_java.pptx
Java basics-includes java classes, methods ,OOPs concepts
CSL101_Ch1.pptx
CSL101_Ch1.pptx
CSL101_Ch1.ppt
CSL101_Ch1.ppt
skill lab-java interview preparation questions.ppt
Basic Java Programming
OOP Lab-manual btech in cse kerala technological university
a basic java programming and data type.ppt
Ad

More from VijalJain3 (8)

PPTX
div_span talks about the div and the span elements
PPTX
HTML elements Marquee Tag and basic elements
PPTX
CN - Lecture 2.pptx
PPTX
1. Introduction to Cyber Security.pptx
PPTX
Recursion.pptx
PPTX
Random Class & File Handling.pptx
PPTX
RSA.pptx
PPTX
AES.pptx
div_span talks about the div and the span elements
HTML elements Marquee Tag and basic elements
CN - Lecture 2.pptx
1. Introduction to Cyber Security.pptx
Recursion.pptx
Random Class & File Handling.pptx
RSA.pptx
AES.pptx
Ad

Recently uploaded (20)

PDF
High Ground Student Revision Booklet Preview
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
ACUTE NASOPHARYNGITIS. pptx
PDF
Types of Literary Text: Poetry and Prose
PPTX
How to Manage Global Discount in Odoo 18 POS
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Congenital Hypothyroidism pptx
PPTX
Strengthening open access through collaboration: building connections with OP...
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
High Ground Student Revision Booklet Preview
Renaissance Architecture: A Journey from Faith to Humanism
Cardiovascular Pharmacology for pharmacy students.pptx
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
ACUTE NASOPHARYNGITIS. pptx
Types of Literary Text: Poetry and Prose
How to Manage Global Discount in Odoo 18 POS
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Introduction and Scope of Bichemistry.pptx
UTS Health Student Promotional Representative_Position Description.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Sunset Boulevard Student Revision Booklet
Congenital Hypothyroidism pptx
Strengthening open access through collaboration: building connections with OP...
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...

Java-Intro.pptx

  • 2. Syllabus 1. Introduction to Object Oriented Programming 2. Class, Object, Packages and Input/Output 3. Array, String and Vector 4. Inheritance 5. Exception handling and Multithreading 6. GUI programming in JAVA
  • 3. Books Textbook • E. Balagurusamy, ‘Programming with Java’, McGraw Hill Education. • Herbert Schildt, ‘JAVA: The Complete Reference’, Ninth Edition, Oracle Press.
  • 4. Introduction to Object Oriented Programming • OOP concepts: objects, class, encapsulation, abstraction, inheritance, polymorphism, message passing • Java Virtual Machine • Basic programming constructs: variables, data types, operators, unsigned right shift operator, expressions, branching and looping.
  • 6. Classes & Objects • The class is the unit of programming • Class – user-defined datatype • A Java program is a collection of classes • Each class definition (usually) in its own .java file • The file name must match the class name • A class describes objects (instances) • Describes their common characteristics: is a blueprint • Thus all the instances have these same characteristics • These characteristics are: • Data fields for each object • Methods (operations) that do work on the objects
  • 8. Object Oriented Programming concepts Encapsulation: • wrapping up of data under a single unit • the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. Abstraction: • by virtue of which only the essential details are displayed to the user • Eg: man driving a car, only knows how to apply a break, does not need to know mechanism behind it.
  • 9. Object Oriented Programming concepts Inheritance • one class can inherit the features(fields and methods) of another class. • Facilitates reusability
  • 10. Object Oriented Programming concepts Polymorphism • Ability to differentiate between entities with the same name efficiently. • This is done by Java with the help of the signature and declaration of these entities. • Eg: addition operation of integer and strings Dynamic binding • Process of linking a method call to the executable code in response to the call • This process exhibited at runtime
  • 11. Object Oriented Programming concepts Message Communication • Objects Communicate with each other Steps: 1. Create classes that define objects 2. Create objects from class definitions 3. Establish communication among objects Eg: Car c; c.speed(c);
  • 12. Features of JAVA Compiled & Interpreted • Two stage system • Compiler – source code into bytecode • Interpreter – bytecode into machine instructions Platform independent & portable • Programs can be easily moved from one computer system to another, anywhere & anytime. • Ensures portability in two ways: • Generates bytecode instructions, which can be implemented anywhere • Size of primitive data type is machine independent
  • 13. Features of JAVA Object-Oriented • All the Code of the java Language is written into the classes and objects Robust & Secure Robust • Eradicates the problem of memory management by performing automatic garbage collection Secure • Absence of pointers ensures that programs cannot gain access to memory locations without proper authorization
  • 14. Features of JAVA Distributed • Program of java is compiled onto one machine can be easily transferred to machine and executes them on another machine • Used to create applications on network Simple, small & familiar • Java removes complexity because it doesn’t use pointers, storage classes and go to statements and java doesn’t support multiple inheritance Multithreaded & Interactive High Performance
  • 15. JDK, JRE & JVM • JDK is platform specific software • JRE provides platform to execute Java program • JVM Provides runtime environment in which java bytecode can be executed • JVM is available for many hardware and software platforms (i.e. JVM is platform dependent).
  • 17. Let’s Begin Programming class Sample //Sample Program { public static void main(String args[]) { System.out.println(“Hello World!!!”); } } //System class is defined in java.lang.* (default imported package) //static – interpreter uses this method before any objects are created /*….Multi line comment */ Identifier Keyword Class Object Method Access Specifier
  • 18. Implementation? Creating the program • Using any text editor (known as source file) • Save the file Compiling the program • javac filename.java //creates bytecodes file (filename.class) Running the program • java filename //interpreter looks for the main method
  • 19. Variables • Name given to a memory location; all the operations done on the variable effects that memory location • Value stored in a variable can be changed during program execution Rules: 1. Should not begin with digit 2. Case sensitive 3. Should not be a keyword 4. White space is not allowed 5. Can be of any length Eg: total_value, mean
  • 20. How to initialize variables?
  • 23. Constants • Variable whose value cannot change once it has been assigned Syntax: final float pi=3.14; • Variables as constants by just adding the keyword “final” when we declare the variable.
  • 27. Special operators instanceof operator • Returns true if the object on the LHS is an instance of the class given on RHS eg: person instanceof Student Dot (.) operator • Used to access the instance variables & methods of class objects eg: person.age person.salary()
  • 28. Java Math methods • java.lang.Math class contains various methods for performing basic numeric operations such as the logarithm, cube root, and trigonometric functions etc. Method Description Arguments abs Returns the absolute value of the argument Double, float, int, long round Returns the closed int or long (as per the argument) double or float ceil Returns the smallest integer that is greater than or equal to the argument Double floor Returns the largest integer that is less than or equal to the argument Double min Returns the smallest of the two arguments Double, float, int, long max Returns the largest of the two arguments Double, float, int, long Method Description Arguments Sin Returns the Sine of the specified argument Double Cos Returns the Cosine of the specified argument double Tan Returns the Tangent of the specified argument Double Atan2 Converts rectangular coordinates (x, y) to polar(r, theta) and returns theta Double toDegrees Converts the arguments to degrees Double Sqrt Returns the square root of the argument Double toRadians Converts the arguments to radians Double
  • 29. Java Math methods Method Description Arguments exp Returns the base of natural log (e) to the power of argument Double Log Returns the natural log of the argument double Pow Takes 2 arguments as input and returns the value of the first argument raised to the power of the second argument Double floor Returns the largest integer that is less than or equal to the argument Double Sqrt Returns the square root of the argument Double
  • 30. Command line argument program class Arguments { public static void main(String args[]) { System.out.println("Arguments are"); for(int i=0;i<args.length;i++) System.out.println(args[i]); } } Note: Converting string to integer Integer.parseInt(args[0])
  • 31. Multiple classes class Room { float length, breadth; void getData(float a, float b) { length=a; breadth=b; } } class RoomArea { public static void main(String args[]) { float area; Room r=new Room(); r.getData(4,5); area=r.length*r.breadth; System.out.println(“Area of a room is:”+area); } }
  • 32. Input Package: java.io I/P statement: BufferedReader br=new BufferedReader(new InputStreamReader(System.in)) Create a object & allocates memory dynamically Class can be used to read data from keyboard Class can be used to read data line by line Input stream which is typically connected to keyboard for input
  • 33. import java.io.*; class Test { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter your name”); String name=br.readLine(); System.out.println(“Welcome: ”+name); } } // return type of readLine() is String Integer.parseInt(br.readLine()); //String to integer
  • 34. Scanner class Input import java.util.Scanner; // Import the Scanner class class MyClass { public static void main(String args[]) { Scanner myObj = new Scanner(System.in); System.out.println("Enter username"); String userName = myObj.nextLine(); System.out.println("Username is: " + userName); } } // return type of nextLine() is String Integer.valueOf(myObj.nextLine()); //String to integer
  • 35. Scanner class Limitation import java.util.Scanner; class Input { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter flight number: "); int flightNumber1 = scanner.nextInt(); System.out.print("Enter flight2: "); int flightNumber2 = scanner.nextInt(); System.out.print("Enter departing city"); String departingCity = scanner.nextLine(); System.out.print("Enter arrival city: "); String arrivalCity = scanner.nextLine(); } }
  • 36. Workaround import java.util.Scanner; class Input1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter flight number1: "); int flightNumber1 = scanner.nextInt(); scanner.nextLine(); System.out.println("Enter departing city:"); String departingCity = scanner.nextLine(); System.out.print("Enter departing city"); String departingCity = scanner.nextLine(); System.out.print("Enter arrival city: "); String arrivalCity = scanner.nextLine(); } }
  • 38. If-else example public class IfElse { public static void main(String args[]) { int n=7; if(n%2==0) System.out.print(“Even”); else System.out.print(“Odd”); } }
  • 39. Switch example class SwitchDay { public static void main(String args[]) { int day=7; switch(day) { case 1: System.out.println(“Monday”); break; case 2: System.out.println(“Tuesday”); break; case 3: System.out.println(“Wednesday”); break; case 4 : System.out.println(“Thursday”); break; case 5: System.out.println(“Friday”); break; case 6 : System.out.println(“Saturday”); break; case 7 : System.out.println(“Sunday”); break; } } }
  • 40. Loops For loop While loop • WAP to print all even integers divisible by 6 or 7 upto 50 Do while loop • WAP to print 10 to 20