Computer Languages are evolve
for two reasons
* to adapt to changes in
environment and to implement advances
in the art of programming.
Java :-
...........
* It was conceived by James
Gosling,Patrick Naughton,Chris Warth,Ed Frank and Mike Sheridan at Sun Microsystems,Inc
in 1991.
* Initially called Oak it was
renamed Java in 1995
* Primary aim is Platform
Independent(architecture neutral)
- used to produce code that would run on a
variety of CPUs under different environments
* Used to create software to
be embedded in various electronic devices such as microwave ovens and remote
controls.
* Internet version of C++
* Used to create two types of
programs
1)Application 2)Applet
Application
--------------
It is a program that runs on your
computer, under the Operating system of
that computer.
Applet
---------
* It is a program that can react to user
input and dynamically change.
True Object Oriented
Programming Language
Machine code:
*************
*)Platform dependent
*)The machine can directly
execute the code
Byte codes:
***********
It is a highly optimized set of
instructions designed to be executed by the Java run-time system, which is called the
Java Virtual Machine(JVM).
* Platform independent
* JVM(Java Virtual Machine) which is
platform independent
Java Features:-
****************
Compiled and Interpreted
Platform Independent and
portable
Object oriented
robust(program must execute
reliably in a variety of systems) and secure
distributed technology[Client
& Server Model]
MultiThreading ( allows you
to write programs that do many things simultaneously)
Architecture-Neutral -
"write once; run anywhere,any time, forever"
C and Java
....................
·
Java does not
include the C unique statement keywords sizeof and typedef.
·
Java does not
contain the data types struct and union.
·
Java does not
define the type modifiers keywords auto,extern,register,signed and unsigned.
·
Java does not
support an explicit pointer type.
·
Java does not
have a preprocessor and therefore we cannot use #define,#include, and #ifdef
statements.
·
Java requires
that the functions with no arguments must be declared with empty paranthesis
and not with the void keyword as done in c.
·
Java adds new
operators such as instanceof and >>>.
·
Java adds
labelled break and continue statements.
c++ and Java :-
..........................
·
Java does not
support operator overloading.
·
Java does not
have template classes as in C++.
·
Java does not support
multiple inheritance of classes.This is accomplished by using a new feature
called interface.
·
Java does not use
pointers.
·
Java has replaced the destructor function with a
finalize() function.
·
There are no
header files in java
JDK Environment :-(Java Developer toolKit)
***************
javac java Compiler(Translates Java sourcecode to Byte code)
java Interpreter(Byte code to Machine code)
javap disassembler
javah Java Headerfile
javadoc Java Documentation
files
jdb Java Debugger
AppletViewer run the Applet Files
API:-(Application Programming
Interface) :
- API is a set of classes
and interfaces that support operations
on collections of objects.
import java.io.*;
import.java.lang.*;
* ->
is a package(means collection of classes and Interfaces)
lang
io
util
net
sql
awt
applet
eg:-
javap java.lang.System
To Compile Java File:-
*********************
javac filename.java
eg:
----
javac Demo.java
(to create Class file)
To run:-
******
java classname
eg
---
java Demo
Datatypes
----------
Integers:
byte - 8 bits
short - 16 bits
int - 32 bits
long - 64 bits
Float:
float - 32 bits
double - 64 bits
Character:
char - 16 bits
String
Boolean:
boolean - returns true or
false
General format of a java
program
--------------------------------------
class classname
{
public static void
main(String args[ ])
{
statements;
}
}
output statement
---------------------
System.out.print("sum"+a);
System.out.println("Message");
Input
-----
types
1)Keyboard input
2)through command line arguments
3)interactive input method
--------
identifiers must start with
$, _, or a letter.
import java.io.*;
class Test
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.println("Java");
}
}
import java.io.*;
class Test
{
public static void main(String args[])throws IOException
{
int a=100;
float b=123.34f;
char c='s';
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
import java.io.*;
class Test
{
public static void main(String args[])throws IOException
{
int a=100;
float b=123.34f;
char c='s';
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}}
-----------------------------------
import java.io.*;
class Test
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int a;
float b;
char c;
String d;
System.out.println("Enter a String :");
d=(in.readLine());
System.out.println("Enter Integer
value:");
a=Integer.parseInt(in.readLine());
System.out.println("Enter Float value");
b=Float.valueOf(in.readLine()).floatValue();
System.out.println("Enter a Character
:");
c=(char)System.in.read();
System.out.println("a="+a+"\nb="+b+"\nc="+c+"\nd="+d);
}
}
import java.io.*;
class Test
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
double d;
System.out.println("Enter Double value
:");
d=Double.parseDouble(in.readLine());
System.out.println("d="+d);
}
}
import java.io.*;
import java.lang.*;
class Test
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String name;
int m1,m2,m3;
float tot,avg;
System.out.println("Enter Your Name :");
name=in.readLine();
System.out.println("Enter 3 Marks :");
m1=Integer.parseInt(in.readLine());
m2=Integer.parseInt(in.readLine());
m3=Integer.parseInt(in.readLine());
tot=m1+m2+m3;
avg=tot/3;
System.out.println("Name="+name+"\nTotal="+tot+"\nAverag="+avg);
}
}
class sum//keyboard input
{
public static void main(String args[])
{
int a=20,b=30;
System.out.println("Sum="+(a+b));
}
}
class sum//Commandline arguments
{
public static void main(String args[])
{
int a,b;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
System.out.println("Sum="+(a+b));
}
}
import java.io.*;
class sum//Interactive Input
Method
{
public static void main(String args[])throws
IOException
{
DataInputStream din=new
DataInputStream(System.in);
int a,b;
System.out.println("Enter 2 numbers
:");
a=Integer.parseInt(din.readLine());
b=Integer.parseInt(din.readLine());
System.out.println("Sum="+(a+b));
}
}
import java.io.*;
import java.lang.*;
class Exam
{
public static void main(String args[])throws
IOException
{
int a=9;
System.out.println("Sqrt(9)="+Math.sqrt(a));
System.out.println("pow(a,2)="+Math.pow(a,2));
}
}
Simple if else
========
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int a;
System.out.println("Enter a number");
a=Integer.parseInt(din.readLine());
if(a%2==0)
System.out.println("Even
Number");
else
System.out.println("Odd
Number");
}
}
Biggest between 3 numbers
---------------------------------
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int a,b,c;
System.out.println("Enter 3 number");
a=Integer.parseInt(din.readLine());
b=Integer.parseInt(din.readLine());
c=Integer.parseInt(din.readLine());
if(a>b)
{
if(a>c)
System.out.println("A is
big");
else
System.out.println("C is
big");
}
else
{
if(b>c)
System.out.println("B is
big");
else
System.out.println("C is
big");
}
}
}
else if ladder
---------------
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
String name;
int m1,m2,m3,tot,avg;
char g;
System.out.println("Enter a name");
name=(din.readLine());
System.out.println("Enter 3 Marks :");
m1=Integer.parseInt(din.readLine());
m2=Integer.parseInt(din.readLine());
m3=Integer.parseInt(din.readLine());
tot=m1+m2+m3;
avg=(tot)/3;
if(avg>=80&&avg<=100)
g='A';
else if(avg<80&&avg>=60)
g='B';
else if(avg<60&&avg>=40)
g='C';
else if(avg<40&&avg>=35)
g='D';
else
g='F';
System.out.println("Name="+name+"\nTotal="+tot+"\nAverage="+avg+"\nGrade="+g);
}
}
Using switch case
---------------------
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int a,b,c;
System.out.println("1.Sum\n2.Sub\n3.Mul\n4.Div\n5.Mod\nenter
your choice");
c=Integer.parseInt(din.readLine());
System.out.println("Enter 2 numbers :");
a=Integer.parseInt(din.readLine());
b=Integer.parseInt(din.readLine());
switch(c)
{
case 1:
System.out.println("Sum="+(a+b));
break;
case 2:
System.out.println("Sub="+(a-b));
break;
case 3:
System.out.println("Mul="+(a*b));
break;
case 4:
System.out.println("Div="+(a/b));
break;
case 5:
System.out.println("Mod="+(a%b));
break;
default:
System.out.println("Not in
case");
}
}
}
import java.io.*;
class selectcase
{
public static void main(String args[])throws
IOException
{
DataInputStream din=new
DataInputStream(System.in);
char ch;
System.out.println("Enter a
character r/g/b:");
ch=(char)System.in.read();
switch(ch)
{
case 'r':
case 'R':
System.out.println("Red");
break;
case 'g':
case 'G':
System.out.println("Green");
break;
case 'b':
case 'B':
System.out.println("Blue");
break;
default:
System.out.println("Invalid");
}
}
}
Example program for while Find
the digit sum
-----------------------------------------------------
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,s=0;
System.out.println("Enter a number :");
n=Integer.parseInt(din.readLine());
while(n>0)
{
s=s+(n%10);
n/=10;
}
System.out.println("Digit sum="+s);
}
}
Armstrong or not
---------------------
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,s=0,a,r;
System.out.println("Enter a number :");
n=Integer.parseInt(din.readLine());
a=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n/=10;
}
if(s==a)
System.out.println("Armstrong");
else
System.out.println("Not
Armstrong");
}
}
do while
========
syntax:
do
{
statement;
}while(condition);
//Find the sum of given
numbers using do while
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,s=0,i=1,c;
System.out.println("How many numbers you want
to give :");
c=Integer.parseInt(din.readLine());
do
{
System.out.println("Enter a number
:");
n=Integer.parseInt(din.readLine());
s=s+n;
i++;
}while(i<=c);
System.out.println("Sum of given numbers="+s);
}
}
for
===
syntax:
for(initialization;condition;increment or decrement)
{
statement;
}
Factorial value
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,f=1,i;
System.out.println("Enter a number :");
n=Integer.parseInt(din.readLine());
for(i=1;i<=n;i++)
f=f*i;
System.out.println("Factorial
value="+f);
}
}
Table
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,i;
System.out.println("Enter Table Number
:");
n=Integer.parseInt(din.readLine());
for(i=1;i<=15;i++)
System.out.println(i+"*"+n+"="+i*n);
}
}
Nested for loop
syntax:
for(initialization;condition;increment
or decrement)
{
statement;
for(initialization;condition;increment or
decrement)
{
statement;
}
}
Print the following triangle
1
1 2
1 2 3
1 2 3 4
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int n,i,j;
System.out.println("How many rows :");
n=Integer.parseInt(din.readLine());
for(i=1;i<=n;i++)
{
System.out.print("\n");
for(j=1;j<=i;j++)
System.out.print(j+"\t");
}
}
}
print
*
***
*****
*******
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
DataInputStream din=new
DataInputStream(System.in);
int i,j,n,k;
System.out.println("How many rows :");
n=Integer.parseInt(din.readLine());
for(i=1;i<=n;i++)
{
System.out.print("\n");
for(k=1;k<=n-i;k++)
System.out.print("
");
for(j=1;j<=2*i-1;j++)
System.out.print("*");
}
}
}
Jump Statements:
===============
break, continue and return
Using break to Exit a loop
=========================
we can force immediate
termination of a loop,by passing the
conditional expression and
any remaining code in the body of the
loop.
ex:
import java.io.*;
class sum
{
public static void main(String args[])
{
for(int i=1;i<200;i++)
{
if(i==10)
break;
System.out.println("i: "+i);
}
System.out.println("Loop
Complete.");
}
}
continue
========
continue statement causes
control to be transferred directly to
the conditional expression
that controls the loop.
ex:
import java.io.*;
class sum
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
System.out.print(i+" ");
if(i%2==0)
continue;
System.out.print("\n");
}
}
}
import java.io.*;
class sum
{
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i%2==1)
continue;
System.out.println(i);
}
}
}
return
========
The return statement
immediately terminates the method in which it
is executed.
import java.io.*;
class sum
{
public static void main(String args[])
{
boolean t=true;
System.out.println("Before the
return.");
if(t)
return;
System.out.println("This won't
execute.");
}
}
========
class SumAvgNot
{
public static void main(String args[])
{
int sum=0;
for(int i=0;i<args.length;i++)
sum+=args[i];
System.out.println("Sum is :"+sum);
System.out.println("Average is
:"+(float)sum/args.length);
}
}
error
S:\Selvarani\JAVA\Javaclass\Loops\SumavgNot.java:7:
inconvertible types
found : java.lang.String
required: int
sum+=args[i];
^
1 error
Tool completed with exit code
1
--------
class Sumavg
{
public static void main(String args[])
{
int sum=0;
for(int i=0;i<args.length;i++)
sum+=Integer.parseInt(args[i]);
System.out.println("Sum is :"+sum);
System.out.println("Average is
:"+(float)sum/args.length);
}
}
Java conditional statements
==============================
Simple if
if
---
if(condition)
{
statement;
}
--------
class app
{
public static void main(String args[])
{
int value=10;
if(value>0)
System.out.println("value="+value);
}
}
--------------
The if else statement
-------------------
if(condition)
statement;
else
statement;
class app1
{
public static void main(String args[])
{
int value=-10;
if(value>0)
System.out.println("Abs("+value+")="+value);
else
System.out.println("Abs("+value+")="+
-value);
}
}
Nested if statements:
=====================
class app2
{
public static void main(String args[])
{
double value=5;
if(value!=0){
if(value>0)
System.out.println("The
result="+(1/value));
else
System.out.println("The result
="+(-1/value));
}
}
}
--------------------
The if-else ladders
---------------
class app3
{
public static void main(String args[])
{
String day="Wednesday";
if(day=="Monday")
System.out.println("It's
Monday");
else if(day=="Tuesday")
System.out.println("It's
Tuesday");
else if(day=="Wednesday")
System.out.println("Wednesday");
else if(day=="Thursday")
System.out.println("Thursday");
else if(day=="Friday")
System.out.println("Friday");
else if(day=="Saturday")
System.out.println("Saturday");
else if(day=="Sunday")
System.out.println("Sunday");
}
}
--------------------
The switch Statement
-------------------------
switch(expression)
case value1:
statement;
break;
case value2:
statement;
break;
case value3:
statement;
.
.
.
.
default:
default_statement;
}
Here, the value of
expression, which must be of type byte,char,short, or int.
class app4
{
public static void main(String args[])
{
int day=3;
switch(day){
case 0:
System.out.println("It's
Monday");
break;
case 1:
System.out.println("It's
Tuesday");
break;
case 2:
System.out.println("Wednesday");
break;
case 3:
System.out.println("Thursday");
break;
case 4:
System.out.println("Friday");
break;
case 5:
System.out.println("Saturday");
break;
case 6:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid");
}
}
}
------------------
The While Loop
----------------
general form
-----------
while(condition)
statement;
if condition is not true,the body of the loop is not even
executed once.
//Display 10 to 1
class app5
{
public static void main(String args[])
{
int value=10;
while(value>0)
System.out.println("Current value="+value--);
}
}
//Find the factorial value
class app5
{
public static void main(String args[])
{
int value=5,factorial=1,temp;
temp=value;
while(temp>0)
{
factorial*=temp;
temp--;
}
System.out.println(value+"!="+factorial);
}
}
-------------------
The do-while Loop
---------------------
The do-while loop is just
like a while loop,except that the test condition is evaluated at the end of the
loop, not at the beginning.
general form
do{
statement
}while(condition);
1)
The biggest reason to use a
do-while loop instead of a while loop is when we need the body of the loop to
be run at least once.
2)
On the other hand, there are
times when we should use a while loop instead of a do-while loop- when the body
of the loop shouldn't even run once if the condition is not true.
class app5
{
public static void main(String args[])
{
double value=0;
do{
System.out.println("Ans="+1/value);
}while(value>0);
}
}
The for Loop
--------------
general form
---------------
for(initialization;condition;increment
or decrement)
statement;
class app8
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
System.out.println(i);
}
}
---------------
we don't have to give a for
loop any body at all - in fact, we can use a null statement.
class app9
{
public static void main(String args[])
{
int i,sum=0;
for(i=0;i<=5;sum+=i,i++);
System.out.println("Sum of first 10
numbers="+sum);
}
}
---------------------
class app_10
{
public static void main(String args[])
{
int
value=6,factorial=1,temp;
temp=value;
for(;temp>0;)
factorial*=temp--;
System.out.println(value+"!="+factorial);
}
}
-------------------
Nested Loops
================
syntax:
for(initialization;condition;increment or decrement)
{
for(initialization;condition;increment
or decrement)
statement;
}
class app_11
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=4;i++)
{
System.out.println();
for(j=1;j<=i;j++)
System.out.print(i+"\t");
}
}
}
-------------------
class app_12
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=4;i++)
{
System.out.println();
for(j=i;j<=4;j++)
System.out.print(". ");
}
}
}
------------------------
Jump Statement
================
break,continue, and return.
These statements transfer
control to another part of our program.
Using break
--------------
1. It terminates a statement sequence in a
switch statement.
2. It can be used to exit a loop.
Using break to Exit a Loop
class app_13
{
public static void main(String args[])
{
int i;
for(i=1;i<=40;i++)
{
if(i==10)
break;
System.out.println("i :"+i);
}
System.out.print("Loop
complete.");
}
}
Using continue
----------------
A continue statement causes
control to be transferred directly to the conditional expression that controls
the loop.
class app_14
{
public static void main(String args[])
{
int i;
for(i=0;i<10;i++)
{
System.out.print(i+" ");
if(i%2==0)
continue;
System.out.println("");
}
}
}
return
--------
The return statement is used
to explicitly return from a method.
class app_15
{
public static void main(String args[])
{
boolean t=true;
System.out.println("Before the return");
if(t)
return;
System.out.println("This won't
execute.");
}
}
No comments:
Post a Comment