all experiment of java

19
Mukesh kumar C.S.E. 1110751908 5 th Semester INTRODUCTION TO JAVA Java language was developed by Sun Microsystem in the year 1991 as a part of green project. GREEN PROJECT: a research group was working to develop software to control consumer electronics devices, during this time they developed a device called " star 7" for which the operating system was planned to be developed in "c++" however one of the team member "James Gosling " was not very satisfy with the performance of c++ hence he developed a new language for start he named it as "oak" .he used to see an oak outside his office in 1995 Sun Microsystem renamed this language as java. FEATURES OF JAVA: 1) Object Oriented Programming 2) Platform Independent 3) Robust 4) Reliable, Safe & Secure 5) Compiler and Interpreter 6) Dynamic 7) Multithreaded 8) High perfomance 9) Portable 10) Simple & Easy to learn

Upload: guru-janbheshver-university-hisar

Post on 17-Jul-2015

101 views

Category:

Education


1 download

TRANSCRIPT

Mukesh kumar C.S.E. 1110751908 5th Semester

INTRODUCTION TO JAVA

Java language was developed by Sun Microsystem in the year 1991 as a part of green project.

GREEN PROJECT:

a research group was working to develop software to control consumer electronics devices, during

this time they developed a device called " star 7"

for which the operating system was planned to be developed in "c++"

however one of the team member "James Gosling " was not very satisfy with the performance of

c++ hence he developed a new language for start he named it as "oak"

.he used to see an oak outside his office

in 1995 Sun Microsystem renamed this language as java.

FEATURES OF JAVA:

1) Object Oriented Programming

2) Platform Independent

3) Robust

4) Reliable, Safe & Secure

5) Compiler and Interpreter

6) Dynamic

7) Multithreaded

8) High perfomance

9) Portable

10) Simple & Easy to learn

Mukesh kumar C.S.E. 1110751908 5th Semester

Object Oriented

o Object oriented throughout - no coding outside of class definitions, including main().

o An extensive class library available in the core language packages.

Platform Independence

o The Write-Once-Run-Anywhere ideal has not been achieved (tuning for different platforms usually required), but closer than with other languages.

Robust

o Exception handling built-in, strong type checking (that is, all data must be declared an explicit type), local variables must be initialized.

Security

o No memory pointers o Programs runs inside the virtual machine sandbox. o Array index limit checking

security manager - determines what resources a class can access such as reading and

writing to the local disk

Compiler/Interpreter

o Code is compiled to bytecodes that are interpreted by a Java virtual machines (JVM) .

o This provides portability to any machine for which a virtual machine has been written.

o The two steps of compilation and interpretation allow for extensive code checking and improved security.

Dynamic Binding

o The linking of data and methods to where they are located, is done at run-time. o New classes can be loaded while a program is running. Linking is done on the fly. o Even if libraries are recompiled, there is no need to recompile code that uses classes

in those libraries. This differs from C++, which uses static binding. This can result in fragile classes for cases where linked code is changed and memory pointers then point to the wrong addresses.

Mukesh kumar C.S.E. 1110751908 5th Semester

High Performance

o Interpretation of bytecode slowed performance in early versions, but advanced virtual machines with adaptive and just-in-time compilation and other techniques now typically provide performance up to 50% to 100% the speed of C++ programs.

Threading

o Lightweight processes, called threads, can easily be spun off to perform multiprocessing.

o Can take advantage of multiprocessors where available o Great for multimedia displays.

What is the Java Virtual Machine?

The JVM is the software that executes Java bytecode. A Java program, written in a file with a

.java extension, is compiled into class files that have a .class extension. The class files are

written in bytecode. To execute these files, the computer uses the JVM to interpret the

bytecode.

A browser that is capable of executing Java applets has the JVM built into it. To run a Java

application, the Java Runtime Environment (JRE) must be installed. The JRE contains the

files in the Java Development Kit minus the development tools, only the files necessary to

run a Java application are present.

The bytecode is the same for all platforms, but the JVM will be different on different

platforms because it needs to execute programs using the native code of the machine it is

running on.

Development Tools: The development tools provide everything you'll need for compiling, running,

monitoring, debugging, and documenting your applications. As a new developer, the main tools

you'll be using are the javac compiler, the java launcher, and the javadoc documentation tool.

What is an Object

Object is instance of class, it contain data as well as behavior of the functions how to manipulate

that data.

What is a Class

Class is a collection of similar objects.

A class is a blueprint or prototype from which objects are created.

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.1

Aim:-- Write a program to display welcome note.

class W

{

public static void main(String args[])

{

System .out.println("welcome");

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.2

Aim:-- Write a program to compute the circle of area.

class mkk

{

public static void main(String args[])

{

int r;

r = 5;

float pi = 3.14f;

System.out.println(pi * r * r);

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.3

Aim:-- Write a program add two number using command line argument .

class mss

{

public static void main(String args[])

{

int a,b,c;

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]);

c=a+b;

System.out.println("addition="+c);

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.4

Aim:-- Write a program to display menu 1(Addition), 2(subtraction), 3(Multiplication),

4(Division).

class mss { public static void main(String args[]) { int a,b,c,d; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); c=Integer.parseInt(args[2]); switch(a) { case 1: { d=b+c; System.out.println("addition="+d); break; } case 2: { d=b-c; System.out.println("substraction="+d); break; } case 3: { d=b*c; System.out.println("multiplication="+d); break; } case 4: { d=b/c; System.out.println("division="+d); break; } default: { System.out.println("invalid"); } } } }

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.5

Aim:-- Write a program to print all the odd number 1 to 50.

class P

{

public static void main(String args[] )

{

for(int i=1;i<=50;i=i+2)

{

System.out.println(i);

}

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.6

Aim:-- Write a program to find wheather the number is prime or not.

class mk

{

public static void main(String args[])

{

int i;

int no=Integer.parseInt(args[0]);

for(i=2;i<=no;i++)

{

if(no % i==0)

break;

}

if(no==i)

{

System.out.println("prime number");

}

else

{

System.out.println("not prime number");

}

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.7

Aim:-- Write a program to show fibonsci series from 1 to 21.

class ms

{

public static void main(String args[])

{

int a=1,b=0,t;

for(int i=1;i<=21;i++)

{

System.out.println(a);

t=a+b;

b=a;

a=t;

}

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--

Mukesh kumar C.S.E. 1110751908 5th Semester

EXPERIMENT NO.8

Aim:-- Write a program print sum and average of 5 number in single array .

class array

{

public static void main(String args[])

{

int a []=new int[5],i,s=0,h=0;

for(i=0;i<=4;i++)

a[i]=Integer.parseInt(args[i]);

for(i=0;i<=4;i++)

{

s=s+a[i];

}

System .out.println("sum is"+s);

h=s/5;

System .out.println("ave is"+h);

}

}

Mukesh kumar C.S.E. 1110751908 5th Semester

Output:--