Object Oriented Programming:
1.
Object Oriented Programming decomposes a program
into real world entities called as objects and then creates data and function
around these objects.
2.
Object Oriented Programming is based on 4 major
pillars which are as follows:
Abstraction:
1.
This deals with identifying the key aspects of a
system.
2.
This is normally required during requirement
analysis phase.
Encapsulation:
1.
The key aspects identified during abstraction are
secured using encapsulation.
2.
This deals with hiding the interface from
implementation. Hence its purpose is security.
Inheritance:
1.
This deals with incorporating the essential
features from a base class in a derived class.
2.
Thus it helps in reusability of methods and its
main purpose is for grouping.
Polymorphism:
1.
This helps when a same message is sent by different
objects to obtain different behaviour from a method.
Object:
1.
An object is an real-world entity that has a
structure and behaviour.
2.
Object has the characteristics as follows:
Class:
1.
A class is a template.
2.
It can also be described as a collection of data
members and member functions.
History of JAVA:
JAVA was
invented by James Gosling. The history of JAVA can be tabulated as below.
Year
|
Development
|
1990
|
Sun
Microsystems decided to develop software for consumer electronic devices
(Green Project).
|
1991
|
New
language Oak was announced.
|
1992
|
Green
Project team demonstrated the application of their new language to control a
list of home appliances using hand-held devices with a tiny touch sensitive
screen.
|
1993
|
The
World Wide Web (WWW) appeared on the Internet. Web applets were developed
using this language that could run on all type of computers connected to
Internet.
|
1994
|
Web
browser called "Hot Java" was developed to run applets on Internet.
|
1995
|
Oak
renamed to Java. Popular companies like Microsoft and Netscape announces
their support to Java.
|
1996
|
Java
became Internet programming language as well general purpose object-oriented
programming language.
|
Java versions and year:
Version
|
Year Of
Release
|
JDK Alpha and Beta (Web Runner)
|
1995
|
JDK 1.0
|
January 23,1996
|
JDK 1.1
|
February 19,1997
|
J2SE 1.2
|
December 8,1998
|
J2SE 1.3
|
May 8,2000
|
VERSION
|
YEAR
|
J2SE 1.4
|
February 6,2002
|
J2SE 5.0
|
September 30,2004
|
Java SE 6
|
December 11,2006
|
Concepts related to for-statement:
1.
General format of for statement:
General Format
|
for(initialization;
termination condition; increment)
{
statement(s)
}
|
- Types
of allowable/non-allowable formats of declaration of for-statement:
Case 1:
|
for(initialization;
termination condition; increment
statement(s)
|
Example:
public class Main {
public static void main(String[]
args) {
int[]
a={32,56,78,90,67};
for(int
i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
Case 2
|
. initialization;
for(;
termination condition; increment)
{
statement(s)
}
|
Example:
public class Main {
public static void main(String[]
args) {
int[]
a={32,56,78,90,67};
int i=0;
for(;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
Case 3
|
For (;
;)
statement(s)
|
Example:
public class Main {
public static void main(String[]
args) {
int
a=10;
for(;;)
a=a+10;
}
}
Case 4
|
for(initialization;
termination condition; increment+n)
{
statement(s)
|
Example:
public class Main {
public static void main(String[]
args) {
int[]
a={32,56,78,90,67};
for(int
i=0;i<a.length;i=i+2)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
Concepts related to while-statement:
1.
General format of while-statement:
General
Format
|
while(condition)
{
statement(s)
}
|
- Types
of allowable/non-allowable formats of declaration of while-statement:
Case1
|
//variable
is declared initially as integer
while(variable)
{
statement(s)
}
|
Example:
public
class Main {
public static void main(String[] args) {
int i=0;
while(i)
{
System.out.println("Welcome to Java");
}
}
}
Case 2:
|
//variable
is declared as character
while(variable)
{
statement(s)
}
|
Example:
public class Main {
public static void main(String[]
args) {
String
num="rad";
while(num)
{
System.out.println("Welcome to Java");
num="swami";
}
}
}
Case 3:
|
//variable
is declared as Boolean
while(variable)
{
statement(s)
}
|
Example:
public class whiledemo {
public static void main(String[]
args)
{
int n=0;
boolean i=true;
while(i)
{
n=n+1;
if(n<10)
i=true;
else
i=false;
}
System.out.println("
"+n);
}
}
Comparison between while and for statement:
While
Statements
|
For
Statements
|
While
loops are good for executing code that doesn't have a specific number of time
to execute. Getting data from a database is the most common use of While
loops because you need all the records that match certain conditions and the
number of records that you'll get is usually not known until after you have
read them all.
While
is used when we don't know when the statement is going to get end or have to
fetch status from some method
|
For
loops are usually use when you want a certain set of code to be executed a
specific amount of times. This is good for days of the week, or pulling data
out of an array since arrays usually have a specific length. There are some
other situations that has a predefined number of occurrences that would
benefit from a For loop.
|
If Statement:
1.
General format of while-statement:-
General Format
|
if(condition)
{
statement(s)
}
|
2.
Types of allowable/non-allowable formats of
declaration of while-statement:
Case 1
|
//variable
is declared initially as integer
if(variable=value)
{
statement(s)
}
|
Example:
public class Main {
public static void main(String[]
args) {
int
a=10;
if(a=10)
System.out.println("Hello World");
}
}
Case 2
|
//variable
is declared initially as integer
if(variable==value)
{
statement(s)
}
|
Example:
public class Main {
public static void main(String[]
args) {
int
a=10;
if(a==10)
System.out.println("Hello World");
}
}
Switch Statement:
1.
General format of while-statement:
General Format
|
switch(variable)
{
case 1:
statement; break;
case 2:
statement; break;
.....
case n:
statement; break;
}
|
2.
Types of allowable/non-allowable formats of
declaration of while-statement:
Case 1:
|
//variable
is declared initially as string
switch(variable)
{
Case 1:
........
Case 2:
........
......
Case n:
........
}
|
Example:
public class Main {
public static
void main(String[] args) {
String a,b;
int n=10;
switch("a")
{
case a:n=n+1;
System.out.println(n+" ");
break;
case b:n=n+2;
System.out.println(n+" ");
break;
}
}
}
Case 2:
|
//variable
is declared initially as integer
switch(variable)
{
Case 1:
........
Case 2:
........
......
Case n:
........
}
|
Example:
public class Main {
public static void main(String[]
args) {
int a=1;
int n=10;
switch(a)
{
case 1:n=n+1;
System.out.println(n+" ");
break;
case 2:n=n+2;
System.out.println(n+" ");
break;
}
Comparison between if-statement and switch-statements:
Switch
Statements
|
If
statements
|
A
switch-case statement is used to select between multiple values for a single
variable. Like having a case for 1 2 and 3 for an integer.
A
switch compiles to a jump tablein assembler and is therefore faster than
if-statement. Note that a switch statement in C has a 'follow-through'
feature (Google this) which can be circumvented with break statements.
One can
only switch on things that evaluate to integral types. In particular this
means that you cannot switch on strings: strings are not part of the natural
C language in any case. Switch is not nearly as powerful as a chain of ifs,
but it is usually faster.
|
An
If-else statement is used for evaluating an expression to either true or
false. An if / then /else checks several conditions in succession. Comparison
is not restricted to integral types as all you are testing is true (not zero)
or false (zero).
|
Comparison between || and && operators:
||
Operator
|
&&
Operator
|
|| is
similar to union
|
&&
is similar to intersection.
|
You
have (cond1||cond2) as a single expression, which could be either true or
false. What or (||) does is to say, "the expression is true when at
least one of cond1 or cond2 is true, and false otherwise." Or, you could
say, "the expression is true if cond1 is true, cond2 is true, or both
are true, and false otherwise.
|
If the
first operand to && is false then there is no point in evaluating the
second operand, since it can't change the value of the expression (false
&& true and false && false are both false)."The
expression is true when both cond1 and cond2 is true and false
otherwise."
|
Program to validate date using
java:
public class MyDate {
public static void main(String
args[])
{
int
dd,mm,yy;
Scanner
s= new Scanner(System.in);
System.out.println("Enter the date:");
dd=s.nextInt();
System.out.println("Enter the month:");
mm=s.nextInt();
System.out.println("Enter the year:");
yy=s.nextInt();
if(mm>=1 && mm<=12)
{
if(yy%4==0 &&
yy%400==0)
{
if(mm==2)
{
if(dd>=1 && dd<=29)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid date");
}
}
}
else if(mm==7 || mm==8)
{
if(dd>=1 && dd<=31)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid date");
}
}
else if(mm%2==0)
{
if(dd>=1 && dd<=30)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid date");
}
}
else
{
if(dd>=1 && dd<=31)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid date");
}
}
}
else
{
System.out.println("Invalid date");
}
}
}
OUTPUT:
Enter the date:
12
Enter the month:
11
Enter the year:
1991
Valid date
No comments:
Post a Comment