Professional Documents
Culture Documents
Table of Contents
Sr.
No.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Experiment Description
Experiment Page
Remarks/Signature
Date
No.
Introduction
Introduction About Java
The Java Buzzwords: Simple, Secure,
Portable, Object-oriented, Robust,
Multithreaded, Architecture-neutral,
Interpreted, High performance, Distributed,
Dynamic
Basic Java Programs
A Program To Generate And Display n
Fibonacci Numbers
A Program To Find The Greatest Common
Divisor Of Two Numbers
A Program To Reverse The Digits Of A
Number
A Program To Remove Duplicates From An
Array
A Program To Search For A Number In A
List Of Numbers Using Binary Search
A Program To Find
sin(x) = x/1! x3/3! + x5/5!
Class And Object
A program to Create a class Rectangle. The
class has attributes length and width, each
which defaults to 1. It has methods that
calculate the perimeter and the area of the
rectangle. It has set and get methods for both
length and width. The set method should
verify that length and width are each floating
point numbers larger than 0.0 and less than
20.0. Create a class to test this Rectangle class
A program to create a class IntegerSet. Each
IntegerSet object can hold integers in the
range 0-100. The set is represented by an
array of Booleans. Array element a[i] is true if
integer I is present in the set and false
otherwise. The no argument constructor
initializes the array to the empty set. Provide
methods for union, intersection to create a
new set. Also provide method insertElement
and deleteElement to insert and delete
elements from the set
Inheritance
Program to create a class MyPoint to represent
a point with x-coordinates and y-coordinates.
The class contains:
Set - A
Two data fields x and y that represents the
coordinates
A no-args constructor that creates a point
(0,0)
A constructor that constructs a point with
specified coordinates
Two get (accessor) methods for data fields
x and y respectively
A method named distance that returns the
distance from this point to another point of the
MyPoint type
A method named distance that returns the
distance between this point to another point
with specified x- and y- coordinates.
12.
13.
14.
15.
Set-A
Page |1
Java is a high level Object oriented programming language. It was conceived by James
Gosling, Patrik Naughton, Chris Wrath, Ed Frank and Mike Sheridan at Sun Microsystems,
Inc (which has since been acquired by Oracle Corporation.) in 1991. It was initially called
oak but was renamed Java in 1995. Java technology is both platform and programming
language.
Java platform was initially developed to address the problems of building software for
networked consumer electronic devices e.g. washing machine, Refrigerator. Sun released
the first public implementation as Java 1.0 in 1995. It promised Write Once, Run Anywhere
(WORA), providing no-cost run-times on popular platforms.
The primary impetus for Java was the need for platform independent (Architecture Neutral)
language that could be used to create software for various devices. In an attempt to find
such a solution, Gosling and others began work on a portable and platform independent
language. This effort ultimately led to the creation of Java.
The secondary impetus for Javas creation was World Wide Web. With the emergence of the
World Wide Web, Java was propelled to the forefront of computer language design, because
the Web too, demanded portable programs. The most striking feature of the language is that
it is a platform-neutral language.
On 13 November 2006, Sun released much of Java as free and open source software under
the terms of the GNU General Public License (GPL). On 8 May 2007, Sun finished the
process, making all of Java's core code free and open-source, aside from a small portion of
code to which Sun did not hold the copyright.
Set-A
Page |2
Simple
Object Oriented
Platform Independent
Secured
Robust
Architecture Neutral
Portable
Dynamic
Interpreted
High Performance
Multithreaded
Distributed
Set-A
Page |3
Platform Independent :- Java code can be run on multiple platforms. Java code is compiled
by the compiler and converted into Bytecode. Bytecode is an platform independent file,
which can be run on many platform. The Bytecode is interpreted by Java Virtual Machine on
whichever platform it is being run.
Secured:- Java is secured because,
There is no explicit pointer in Java.
Java programs runs inside Virtual Machine Sandbox.
During execution, Java programs uses runtime environment of its own.
Classloader: - Adds security by separating the packages for the class of the
local file system from those that are imported from network sources.
Bytecode Verifier: - Checks code fragments for illegal code that can violates
access rights to objects.
Robust: - Java has been designed for writing highly reliable or robust software. All the
following point makes Java robust
Java uses strong memory management.
There is lack of explicit pointers that avoids security problem.
There are Exception handling and type checking mechanism in Java.
Architecture Neutral :- Java compiler generates an architecture neutral object file , which
makes the compiled to be executable on many processors, with the presence of Java Run
Time.
Portable: - We may carry the java programs (Bytecode) to any platform without any
modification or changes.
Dynamic:- Java programs can carry extensive amount of run-time information that can be
used to verify and resolve accesses to objects at run-time. This makes it possible to
dynamically link code in safe and expedient manner.
Set-A
Page |4
Interpreted: - Java enables the creation of cross platform programs by compiling sourcecode into an intermediate representation called Java Bytecode. Java Bytecode is translated
(interpreted) on the fly to native machine instruction by JVM. The development process is
more rapid and analytical since the linking is an incremental and lightweight process. Thus,
Java is said to be interpreted.
High-Performance: - With the use of Just-In-Time Compiler, Java enables high performance.
Multithreaded :- With Javas multithreaded feature, it is possible to write programs that can
do many tasks simultaneously. The main advantage of Multithreading is that it shares the
same memory area.
Distributed:- Java is designed as a distributed language for creating applications networks.
RMI and EJB are used for creating distributed application. By distributed programming, we
may access files by calling the methods from any machine on the networks (Internet).
Set-A
Page |5
Set-A
Page |6
Output:-
C:\Users\DEll\Java>java Fibonacci
#######################Fibonacci Series#######################
1 1 2 3 5 8 13 21 34
Set-A
Page |7
if(num1>num2)
min = num2;
else
min = num1;
Set-A
Page |8
}catch(Exception e){
System.out.println("\n\t"+e);
}
}}
Output:C:\Users\DEll\Java>java GCD
Enter first number :-> 8
Enter second number :-> 4
GCD: 4
Set-A
Page |9
rem = Dnum%10;
rev = rev*10+rem;
Dnum = Dnum/10;
}
System.out.println("\n\tReversed Number = "+rev);
}else{
System.out.println("\n\tInvalid Input:- Number Must be larger than one digit !");
}
}catch(Exception e){
System.out.println(e);
}
Set-A
P a g e | 10
}}
Output:-
C:\Users\DEll\Java>java Reverse
Enter number :-> 476
Reversed Number = 674
Set-A
P a g e | 11
Set-A
P a g e | 12
if(dup == 0){
Narray[cout] = array[i];
cout++;
}
}
/*Printing Refined array */
Set-A
P a g e | 13
Output:-
C:\Users\DEll\Java>java RemoveDuplicate
Enter 10 elements in array
array[0] = 4
array[1] = 5
array[2] = 3
array[3] = 9
array[4] = 8
array[5] = 7
array[6] = 2
array[7] = 1
array[8] = 3
array[9] = 4
Refined Array
array[0] = 5
array[1] = 9
array[2] = 8
array[3] = 7
array[4] = 2
array[5] = 1
array[6] = 3
array[7] = 4
Set-A
P a g e | 14
Set-A
P a g e | 15
l = mid+1;
}
else if(sNum<arr[mid]){
u = mid-1;
}
}
if(p==-1)
{
System.out.println("Number Does not Exist");
}
else{
System.out.println("Number Exist in the array index at "+p);
}
}catch(Exception e){
System.out.println("\n\t"+e);
}
}}
Set-A
P a g e | 16
Output:C:\Users\DEll\Java>javac binary_search.java
C:\Users\DEll\Java>java binary_search
Enter Ten numbers in Array
Warning! :- Numbers should be in ascending order and they should not same
arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4
arr[5] = 5
arr[6] = 6
arr[7] = 7
arr[8] = 8
arr[9] = 9
Set-A
P a g e | 17
class Sin_x{
public static void main(String args[])
{
try{
double x,n,f,m=1,sum=0;
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
for(double i=1.0;i<=n;i=i+2)
{
f=1;
for(double j=1.0;j<=i;j++)
{
f=f*j;
}
Set-A
P a g e | 18
if(m==1)
{
sum=(sum+ (Math.pow(x,i))/f) ;
m=0;
}
else
{
sum=(sum- (Math.pow(x,i))/f) ;
m=1;
}
}
System.out.printf("\n\tSum = %.5f\n",sum);
}catch(Exception e){
System.out.println("\n\t"+e);
}
}}
Output:-
C:\Users\DEll\Java>javac Sin_x.java
C:\Users\DEll\Java>java Sin_x
Enter the value of X -> 2
Enter the last limit -> : 5
Sum = 0.93333
Set-A
P a g e | 19
class rectangle{
double length;
double width;
rectangle(){
length = 1;
width = 1;
}
double getWidth(){
return width;
}
double getLength(){
return length;
}
Set-A
P a g e | 20
return 0;
}else{
System.out.println("\n\tInvalid Length");
return 1;
}
}
double perimeter(){
return 2*(getWidth()+getLength());
}
Set-A
P a g e | 21
double area(){
return getWidth()*getLength();
}
}
class testRect{
public static void main(String args[]){
try{
System.out.print("\n Enter Width of Rectangle -> ");
wid = Double.parseDouble(br.readLine());
System.out.print("\n Enter Length of Rectangle -> ");
len = Double.parseDouble(br.readLine());
ret_wid = obj.SetWidth(wid);
ret_len = obj.SetLength(len);
if(ret_wid == 1 || ret_len==1){
System.exit(0);
}
else{
double perimeter = obj.perimeter();
double area = obj.area();
Set-A
P a g e | 22
System.out.println("\n Perimeter = "+perimeter);
System.out.println("\n Area = "+area);
}catch(Exception e){
System.out.println("\n\t"+e);
}
}}
Output:C:\Users\DEll\Java>java testRect
Perimeter = 30.0
Area = 50.0
Set-A
P a g e | 23
class IntegerSet
{
private boolean a[] = new boolean[10];
public IntegerSet()
{
for(int i=0; i<10; i++)
{
a[i] = false;
}
}
Set-A
P a g e | 24
{
System.out.printf("\nIndex %d is already true", j);
System.exit(0);
}
Set-A
P a g e | 25
int opt, n, index_i, index_d, index_i1;
IntegerSet I3 = new IntegerSet();
Set-A
P a g e | 26
for(int i=0; i < n; i++)
{
System.out.printf("\nPlease enter the position to where you want to insert
Set-A
P a g e | 27
if(a[j] == true && I1.a[j] == true)
{
I2.a[(int) (j)] = true;
}
}
return (I2);
class Integer_Set
{
/*main() method*/
public static void main(String args[])throws IOException
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String inS1, inS2, inS3, inS4;
int opt, n, index_i, index_d, index_i1;
IntegerSet I1 = new IntegerSet();
IntegerSet I2 = new IntegerSet();
IntegerSet I3 = new IntegerSet();
Set-A
P a g e | 28
do
{
System.out.println("\n\n\t\tPlease select your option.\n");
System.out.printf("\n\tPress 1 for INSERT ELEMENT.\n");
System.out.printf("\n\tPress 2 for DELETE ELEMENT.\n");
System.out.printf("\n\tPress 3 for UNION OF TWO SETS.\n");
System.out.printf("\n\tPress 4 for INTERSECTION OF TWO SETS.\n");
System.out.printf("\n\tPress 5 for EXIT.\n");
System.out.print("\nEnter your choice ->");
inS1=stdin.readLine();
opt=Integer.parseInt(inS1);
switch(opt)
{
case 1:
{
System.out.printf("\nHow many times you want to put the
value(s)?: ");
inS1 = stdin.readLine();
n = Integer.parseInt(inS1);
Set-A
P a g e | 29
}
I1.display();
break;
}
case 2:
{
System.out.printf("\nPlease enter the position from where
you want to delete the value: ");
inS1 = stdin.readLine();
index_d = Integer.parseInt(inS1);
I1.deleteElement(index_d);
I1.display();
break;
}
case 3:
{
I3.union(I1, I2);
break;
}
case 4:
{
System.out.printf("\nHow many times you want to put the
value(s)?: ");
inS1 = stdin.readLine();
n = Integer.parseInt(inS1);
Set-A
P a g e | 30
case 5:
Set-A
P a g e | 31
{
System.out.printf("\nProgram Terminated...!\n");
System.exit(0);
break;
}
default:
System.out.printf("\nInvalid Input...!\n");
}
}while(opt!=5);
}
}
Output:C:\Users\DEll\Java >java Integer_Set
Please select your option.
Press 1 for INSERT ELEMENT.
Press 2 for DELETE ELEMENT.
Press 3 for UNION OF TWO SETS.
Press 4 for INTERSECTION OF TWO SETS.
Press 5 for EXIT.
Enter your choice ->1
How many times you want to put the value(s)?: 2
Please enter the position to where you want to insert the value: 3
Please enter the position to where you want to insert the value: 5
Index 0: false
Index 1: false
Index 2: false
Set-A
P a g e | 32
Index 3: true
Index 4: false
Index 5: true
Index 6: false
Index 7: false
Index 8: false
Index 9: false
Set-A
P a g e | 33
Create a class MyPoint to represent a point with x-coordinates and ycoordinates. The class contains:
Two data fields x and y that represents the coordinates
A no-args constructor that creates a point (0,0)
A constructor that constructs a point with specified coordinates.
Two get (accessor) methods for data fields x and y respectively
A method named distance that returns the distance from this
point to another point of the MyPoint type
A method named distance that returns the distance between this
point to another point with specified x- and y- coordinates.
Extend the class MyPoint to create a class ThreeDPoint with the
following additional features:
A data field named z that represents the z-coordinate
A no-args constructor that creates a point (0,0,0)
A constructor that constructs a point with three specified
coordinates.
A get method for the z value
Override the distance method to return the distance between two
points in the three dimensional space.
-------------------------------------------------------------------------------------------------------------------------------------File Name: - testCoordinate.java
Source code:import java.io.*;
class MyPoint{
MyPoint(){
X = 0;
Y = 0;
}
Set-A
P a g e | 34
MyPoint(int x, int y){
X = x;
Y = y;
}
int retX(){
return X;
}
int retY(){
return Y;
}
X2 = obj2.retX();
Y2 = obj2.retY();
Set-A
P a g e | 35
private int Z;
ThreeDPoint(){
super();
Z = 0;
}
int retZ()
{
return Z;
}
X1 = this.retX();
Y1 = this.retY();
X2 = obj2.retX();
Y2 = obj2.retY();
Z1 = Z;
Z2 = obj2.Z;
Set-A
P a g e | 36
class testCoordinate{
public static void main(String args[]){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|");
|");
Set-A
P a g e | 37
System.out.print("|Enetr y:-> ");
int y2 = Integer.parseInt(br.readLine());
System.out.println("|-----------------------------|");
|");
|");
Set-A
P a g e | 38
y2 = Integer.parseInt(br.readLine());
System.out.print("|Enetr z:-> ");
int z2 = Integer.parseInt(br.readLine());
System.out.println("|-----------------------------|");
}catch(Exception e){
System.out.println("\n\t"+e);
}
}}
Output:C:\Users\DEll\Java>java testCoordinate
|Enter x:-> 5
|Enter y:-> 10
|--------------------------------|
|---Input for Second Point----|
|
|Enter x:-> 15
Set-A
P a g e | 39
|Enter y:-> 10
|------------------------------------|
|Enter x:-> 1
|Enter y:-> 2
|Enter z:-> 3
|------------------------------|
|Enter x:-> 1
|Enter y:-> 3
|Enter z:-> 2
|-----------------------------------|
Set-A
P a g e | 40
Set-A
P a g e | 41
num1 = retMark1();
num2 = retMark2();
res = ((num1+num2)*100)/200;
System.out.println("\n\tPercentage = "+res);
}
}
class testMultiple {
public static void main(String args[]){
try{
Result obj = new Result("Ritesh Kumar",22,75,36);
Set-A
P a g e | 42
obj.percent_calc();
}catch(Exception e){
System.out.println("\n\t"+e);
}
}
}
Output:C:\Users\DEll\Java>java testMultiple
Student Information
Name: Ritesh Kumar
Roll No: 22
Mark 1: 75
Mark 2: 36
Percentage = 55.5
Set-A
P a g e | 43
/*
<applet code="multi" width=200 height=600>
</applet>
*/
public class multi extends Applet{
int res,table;
public void init(){
setBackground(Color.white);
setForeground(Color.black);
table = Integer.parseInt(JOptionPane.showInputDialog("Enter a Number"));
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(1,1,197,540);
g.drawString(" The Multiplication table of "+table,20, 40);
Set-A
P a g e | 44
for(int i = 1;i<11;i++){
res = table*i;
g.drawString(table+" x "+i+" ="+res,60,50*i+25);
}
}
}
Output:-
Set-A
P a g e | 45
An applet program to take input for the date of birth of a person and
display the age in terms of years, months and days
/*
<applet code="DOB" width=268 height=50>
</applet>
*/
public class DOB extends Applet {
int res_month, res_year, res_date, date, month, year, x, flg=0;;
public void init(){
setBackground(Color.white);
setForeground(Color.black);
try{
date = Integer.parseInt(JOptionPane.showInputDialog("Enter Your date of
birth","Date Must be valid"));
month = Integer.parseInt(JOptionPane.showInputDialog("Enter Your month of
birth","Month Must be valid"));
Set-A
P a g e | 46
if(res_month<=0)
{
if(res_year!=0)
{
res_year--;
res_month=12-res_month*(-1);
}
}
if(res_date<0)
{
if(res_month!=0)
Set-A
P a g e | 47
res_month--;
if(x==1||x==3||x==5||x==7||x==8||x==10||x==12)
res_date=31-res_date*(-1);
else if(birthdate.get(YEAR)%4==0&&x==2)
res_date=29-res_date*(-1);
else if(birthdate.get(YEAR)%4!=0&&x==2)
res_date=28-res_date*(-1);
else
res_date=30-res_date*(-1);
}
if(flg==1)
{
JOptionPane.showMessageDialog(null,"Invalid Date
!","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
Set-A
P a g e | 48
}
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(1,1,260,47);
g.drawString("You are "+res_year+" years, "+res_month+" months, "+res_date+"
days old.",20, 40);
}
}
Output:C:\Users\DEll\Java>appletviewer DOB.java
Set-A
P a g e | 49
}
public static void main(String args[]){
HeighPriority thread1 = new HeighPriority();
HeighPriority thread2 = new HeighPriority();
HeighPriority thread3 = new HeighPriority();
HeighPriority thread4 = new HeighPriority();
HeighPriority thread5 = new HeighPriority();
thread1.setName("Higher Thread-1");
thread2.setName("Higher Thread-2");
thread3.setName("Lower Thread-1");
thread4.setName("Lower Thread-2");
thread5.setName("Lower Thread-3");
thread1.setPriority(10);
thread2.setPriority(9);
thread3.setPriority(3);
Set-A
P a g e | 50
thread4.setPriority(2);
thread5.setPriority(1);
System.out.println("Thread 5 started");
thread5.start();
System.out.println("Thread 4 started");
thread4.start();
System.out.println("Thread 1 started");
thread1.start();
System.out.println("Thread 2 started");
thread2.start();
System.out.println("Thread 3 started");
thread3.start();
}
}
Output:C:\Users\DEll\Java>java HeighPriority
Thread 5 started
Thread 4 started
Thread 1 started
Thread Name: Lower Thread-3, Thread Priority: 1
Thread Name: Higher Thread-1, Thread Priority: 10
Thread 2 started
Thread Name: Lower Thread-2, Thread Priority: 2
Thread Name: Higher Thread-2, Thread Priority: 9
Thread 3 started
Thread Name: Lower Thread-1, Thread Priority: 3