0% found this document useful (0 votes)
11 views

Java 1.2

Uploaded by

KRISHNAMOORTHY V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java 1.2

Uploaded by

KRISHNAMOORTHY V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Overview of Java

General Objective:

Construct a simple java program using conditional statements and looping.

Specific Objective
1. What is the essential element object-oriented programming?
2. Explain encapsulation and inheritance with an example.
3. Write the syntax for control statements and looping
MICRO PLANNING FOR 60 MIN
Alpha
Stimulating
Breathing;
question; 5Recap2; 2 General
Evocation; 2
Summary; 3 objective; 1

Mindmap; 5

Specific
Discussion; Objective
5 (1,2); 15

Formative
Assessment;
5 Formative
Specific Assessment;
Objective 5
(3); 10
Contents
Object-Oriented Programming
Two Paradigms
The Three OOP Principles
A First Simple Program
Two Control Statements
The if Statement The for Loop
Using Blocks of Code
Lexical Issues
Whitespace Identifiers Literals Comments Separators
The Java Keywords
The Java Class Libraries
Object-Oriented Programming
 All computer programs consist of two elements that govern how a program is
constructed : code and data.
 The first way is called the process-oriented model - characterizes a program as a series
of linear steps (code).
 The process-oriented model can be thought of as code acting on data.
 The second approach, called object-oriented programming - organizes a program
around its data (that is, objects) and a set of well-defined interfaces to that data.
 An object-oriented program can be characterized as data controlling access to code.
Abstraction
 An essential element of object-oriented programming is abstraction.
 Manage complexity through abstraction.
 Example: abstraction allows people to use a car to drive to the grocery store
without being overwhelmed by the complexity of the parts that form the car.
 An Abstract Data Type (ADT) is a user-defined data type that satisfies the
following two conditions: (Encapsulation + Information Hiding)
– The representation of, and operations on, objects of the type are defined in a
single syntactic unit; also, other program units can create objects of the type.
– The representation of objects of the type is hidden from the program units
that use these objects, so the only operations (methods) possible are those
provided in the type's definition which are known as interfaces.
Encapsulation
 Encapsulation is the mechanism that binds together code and the
data it manipulates and keeps both safe from outside interference
and misuse.
 Encapsulation is as a protective wrapper that prevents the code and
data from being arbitrarily accessed by other code defined outside
the wrapper.
 Access to the code and data inside the wrapper is tightly controlled
through a well-defined interface.
 In Java the basis of encapsulation is the class.
 A class defines the structure and behavior (data and code) that will
be shared by a set of objects.
Inheritance
 Inheritance is the process by which one object acquires the
properties of another object.
 This is important because it supports the concept of
hierarchical classification.
 The Child Class is Existing from the Parent Class (Sub Class) is
Existing from Super Class.
 Sub class contains the features of Super class
 Example: Father-Son Relationship, Son inherits the property of
his Father.
Polymorphism
 Polymorphism (from the Greek, meaning “many forms”) is a feature that
allows one interface to be used for a general class of actions.
 The specific action is determined by the exact nature of the situation.
 Consider a stack where you might have program that requires three types
of stack
 One stack is used for integer, one for floating values and one for character.
 The algorithm implementing each stack is the same, even though the data
being stored differs.
Sample Program
class Sample
{
public static void main(String args[])
{
System.out.println(“Welcome to Java Programming”);
}
}
To Compile: javac Sample.java
To Run : java Sample
A Closer Look
 All Java applications begin execution by calling main( )
 The public keyword is an access specifier
 The keyword static allows main( ) to be called without having to instantiate a particular
instance of the class. This is necessary since main( ) is called by the Java interpreter before
any objects are made
 The keyword void simply tells the compiler that main( ) does not return a value
 String args[ ] declares a parameter named args, which is an array of instances of the class
String
 System.out.println - System is a predefined class that provides access to the system, and
out is the output stream that is connected to the console, println( ) is used to display
Control Statements – if … else
Example
class ifelse
{
public static void main(String args[])
{
int i=4;
if(i<=3)
Syntax System.out.println("Lesser");
if (condition) else
{ code to be executed if condition is true } System.out.println("Greater");
else }
{ code to be executed if condition is false } }
Nested if….else Statement
Syntax: Example: Output :
if(condition) class Nested if { When i= 0
public static void main(String args[]){
{ Output : 1
int i=4;
if(condition) 2
if(i<=3)
{ Code to be executed; } When i= 4
{ System.out.println("1");
if(condition) Output : Okay
if(i<=4)
{ Code to be executed; } System.out.println("2");
else else
{Code to be executed; } System.out.println("3");
} }else
System.out.println("Okay!");
else
}
{Code to be executed;}
}
Coding:
Switch case Statement class SampleSwitch
Syntax: {
public static void main(String args[])
switch (expression)
{
{
int i =0;
case value1: switch(i)
code to be executed {
break case 0: System.out.println("i is zero");
case value2: break;
case 1: System.out.println("i is one");
code to be executed
break;
break
default: System.out.println("i is greater than two");
default value: }
default code to be executed }
} }
Looping Statements
For Statement
for (initialization ; condition; iteration )
{ statements }

While Statement
while( condition )
{ statement }

do-while Statement
do-while ( object )
{ statement }
FOR loop
class forloop {
public static void main(String args[])
{ Output:
for(int n=5; n>0; n--) tick 5
System.out.println("tick " + n); tick 4
tick 3
}
tick 2
}
tick 1
While loop
class whileloop
{
public static void main(String args[])
Output:
{ int n=5;
tick 5
while(n>0) tick 4
{ System.out.println("tick " + n); tick 3
n--; tick 2
} tick 1
}
}
Do While loop
class dowhileloop {
public static void main(String args[]) {
int n=5;
do{ Output:
System.out.println("tick " + n); tick 5
n--; tick 4
} tick 3
while(n>0); tick 2
}
tick 1
}
Lexical Issues
 Java programs are a collection of white space, identifiers, comments, literals, operators,
separators, and keywords
 In Java, white space is a space, tab, or new line.
 An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or
the underscore and dollar-sign characters.
 A constant value in Java is created by using a literal representation of it. For example, 100 |98.6
|‘X’ |“This is a test”
 There are three types of comments:
– single-line (//)
– multi line (/* */ )
– documentation (/** */)
 The most commonly used separator in Java is the semicolon.
The Java Class Libraries
 The methods are members of the System class, which is a class predefined by Java
that is automatically included in your programs.
 Java environment relies on several built-in class libraries that contain many built-in
methods that provide support for such things as I/O, string handling, networking,
and graphics.
Mind Map
Polymorphism Abstraction

Control Statements Object-Oriented


if, for Programming

Encapsulation Inheritance

Overview of Java

White space
Literals

Identifiers
Lexical Issues

Comments
Java Keywords Separators

Mathematical Modeling and Industrial research applications

You might also like