Professional Documents
Culture Documents
Java Prcatical File Mann
Java Prcatical File Mann
Java Prcatical File Mann
Practical -1
Aim:- Install JDK, write a simple “Hello World” or similar java program, compilation,
debugging, executing using java compiler and interpreter.
Step 5 : compiling
to compile the same code
start the command prompt
set the current drive where you save you source file
Ex : your source file is saved in D drive than you can write D: and press enter key.
Set the current working folder there you save the source code or file using cd commands.
Ex : D:/cd 226420307037
Compile source code using java c compiler & convert source code into class file.
Ex : javac filename.java
Code:-
class demo
{
public static void main(String args[ ])
{
System.out.println("Hello world");
}
}
Output:-
Code:-
class sum
{
public static void main(String args[])
{
int a,b,d;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
d=a+b;
System.out.println("ans="+d);
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Output:-
Code:-
class add
{
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]);
d=a+b+c;
System.out.println("ans="+d);
}
}
Output:-
Code:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
class add1
{
public static void main(String args[])
{
int a=20,b=10,c;
c=a+b;
System.out.println("add="+c);
}
}
Output:-
Code:-
class mul
{
public static void main(String args[])
{
int a=20,b=10,c;
c=a*b;
System.out.println("mul="+c);
}
}
Output:-
Code:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
class sub
{
public static void main(String args[])
{
int a=20,b=10,c;
c=a-b;
System.out.println("sub="+c);
}
}
Output:-
Code:-
class div
{
public static void main(String args[])
{
int a=20,b=10,c;
c=a/b;
System.out.println("div="+c);
}
}
Output:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical -2
Aim:- Write a program in Java to find maximum of three numbers using conditional
operator.
Code:-
class max
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
int max=(a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println("maximum number is:"+max);
}
}
Output:-
Aim:- Write a program in Java to find minimum of three numbers using conditional
operator.
Code:-
class min
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
int c=Integer.parseInt(args[2]);
int min=(a<b)?((a<c)?a:c):((b<c)?b:c);
System.out.println("minimum number is:"+min);
}
}
Output:-
Aim:- Write a program in Java to find maximum of three numbers using logical operator.
Code:-
class max1
{
public static void main(String args[])
{
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
if(a>b && a>c)
{
System.out.println("max="+a);
}
else if(b>c && b>a)
{
System.out.println("max="+b);
}
else
{
System.out.println("max="+c);
}
}
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Output:-
Aim:- Write a program in Java to find minimum of three numbers using logical operator.
Code:-
class min1
{
public static void main(String args[])
{
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
if(a<b && a<c)
{
System.out.println("min="+a);
}
else if(b<c && b<a)
{
System.out.println("min="+b);
}
else
{
System.out.println("min="+c);
}
}
}
Output:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical -3
Aim:- Write a program in Java to reverse the digits of a number using while loop
Code:-
class rev
{
public static void main(String args[ ])
{
int n,r,d=0;
n=Integer.parseInt(args[0]);
while(n!=0)
{
r=n%10;
d=(d*10)+r;
n=n/10;
}
System.out.println("reverse number="+d);
}
}
Output:-
Code:-
class area
{
public static void main(String args[])
{
int r;
r=Integer.parseInt(args[0]);
double a=3.14*r*r;
System.out.println("area of circle is:"+a);
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
}
}
Output:-
Code:-
class odd
{
public static void main(String args[])
{
int a;
a=Integer.parseInt(args[0]);
if(a%2==0)
{
System.out.println("a is even");
}
else
{
System.out.println("a is odd");
}
}
}
Output:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Arrays
import java.util.Scanner;
class arraydemo
{
public static void main (String args[])
{
int size, i;
Scanner r= new Scanner (System.in);
System.out.println("Enter size of array:");
size =r.nextInt();
int a[] = new int [size];
for (i=0; i< size; i++)
{
a[i] = r.nextInt();
}
System.out.println("printed array element:");
for (i=0; i<size; i++)
{
System.out.println(a[i]+" ");
}
}
}
Output:-
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical:4
AIM : Write a program in Java to add two 3*3 matrices.
CODE :
class matrix
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{11,12,13},{14,15,16},{17,18,19}};
int c[][]={{0,0,0},{0,0,0},{0,0,0}};
int i,j;
System.out.println("Matrix A=");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.println(a[i][j]);
}
System.out.println();
}
System.out.println("Matrix B=");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.println(b[i][j]);
}
System.out.println();
}
System.out.println("Addition of matrix A and B=");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.println(c[i][j]);
}
}
}
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical:5
AIM : Write a program in Java to generate first n prime numbers.
CODE :
class prime
{
public static void main(String args[])
{
int a,i,n,c;
a=Integer.parseInt(args[0]);
for(n=1; n<=a; n++)
{
c=0;
for(i=2; i<=n/2; i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==0 && n!=1)
{
System.out.println("Prime number is:"+n);
}
}
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical:6
AIM : Write a program in Java which has a class Student having two instance
variables enrollmentNo and name. Create 3 objects of Student class in main
method and display student’s name.
CODE :
class student
{
int enr_no;
String sname;
void getdata(int no,String name)
{
enr_no=no;
sname=name;
}
void display()
{
System.out.println("Student's enrollment number:" + enr_no);
System.out.println("Student's name:"+ sname);
}
public static void main(String args[])
{
student s1=new student();
student s2=new student();
student s3=new student();
s1.getdata(1,"Mansi");
s1.display();
s2.getdata(2,"Jiya");
s2.display();
s3.getdata(3,"Krishna");
s3.display();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 7
AIM : Write a program in Java which has a class Rectangle having two
instance variables height and weight. Initialize the class using constructor.
CODE :
class rectangle
{
int height,weight;
rectangle (int h, int w)
{
height=h;
weight=w;
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 8
CODE :
class demo4
{
int a=10;
void display()
{
int a=20;
System.out.println("Local variable a=" +a);
System.out.println("Instance variable a=" +this.a);
}
public static void main(String args[])
{
demo4 p=new demo4();
p.display();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 9
CODE :
1. STATIC VARIABLE :
class demo
{
static int c=0;
void display()
{
c++;
System.out.println("c="+c);
}
public static void main(String args[])
{
demo p1=new demo();
demo p2=new demo();
demo p3=new demo();
p1.display();
p2.display();
p3.display();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
2. STATIC METHOD :
class demo1
{
static void display1()
{
System.out.println("I am static method");
}
void display2()
{
System.out.println("I am non static method");
}
public static void main(String args[])
{
demo1 p=new demo1();
p.display2();
display1();
}
}
OUTPUT :
3. STATIC BLOCK :
class demo3
{
static
{
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 10
class circle
{
public static void main(String args[])
{
final double pi=3.14;
double r=2;
System.out.println("area of circle="+(pi*r*r));
}
}
OUTPUT :
class A
{
void display()
{
System.out.println("heelo");
}
}
class B extends A
{
void display()
{
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
System.out.println("good norning");
}
}
class demo2
{
public static void main(String args[])
{
B p=new B();
p.display();
}
}
OUTPUT :
class A
{
void display()
{
System.out.println("heelo");
}
}
class B extends A
{
void display()
{
System.out.println("good norning");
}
}
class demo2
{
public static void main(String args[])
{
B p=new B();
p.display();
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 11
AIM : *Write a program in Java which has a class Shape having 2 overloaded
methods area(float radius) and area(float length, float width). Display the area
of circle and rectangle using overloaded methods.
CODE :
class shapes
{
float r,l,w;
void area(float radius)
{
r=radius;
System.out.println("Area of circle:"+(3.14*r*r));
}
void area(float length,float width)
{
l=length;
w=width;
System.out.println("Area of rectangle:"+(l*w));
}
}
class a
{
public static void main(String args[])
{
shapes ar=new shapes();
ar.area(2);
ar.area(2,4);
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 12
CODE :
class dem
{
int a;
dem()
{
a=0;
}
dem(int x)
{
a=x;
}
dem(dem p)
{
a=p.a;
}
void display()
{
System.out.println("a="+a);
}
public static void main(String args[])
{
dem s1=new dem();
dem s2=new dem(10);
dem s3=new dem(s2);
s1.display();
s2.display();
s3.display();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 13
CODE :
1. length() :
class d1
{
public static void main(String args[])
{
String s1="hello";
System.out.println("length of string :"+s1.length());
}
}
OUTPUT :
2. charAt() :
class d2
{
public static void main(String args[])
{
String s1="India";
System.out.println(s1.charAt(1));
System.out.println(s1.charAt(4));
}
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
OUTPUT :
3. contains() :
class d3
{
public static void main(String args[])
{
String s1="hello good morning";
System.out.println(s1.contains("good"));
System.out.println(s1.contains("afternoon"));
}
}
OUTPUT :
4. split() :
class d4
{
public static void main(String args[])
{
String s1="apple,banana,mango,orange";
String f[]=s1.split(",");
for(int i=0;i<f.length;i++)
{
System.out.println(f[i]);
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
}
}
OUTPUT :
5. format() :
class d5
{
public static void main(String args[])
{
String name="Tina";
int age=17;
double salary=50000.500;
System.out.println(String.format("my name is %s, i am %d years old, my salary is
$%.2f",name,age,salary));
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 14
CODE :
class A
{
A()
{
System.out.println("I am class A");
}
}
class B extends A
{
B()
{
System.out.println("I am class B");
}
}
class single
{
public static void main(String args[])
{
B b=new B();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 15
Practical : 16
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 17
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 18
AIM : *Write a program in Java which has a class Car having two instance
variables topSpeed and name. Override toString() method in Car class. Create 5
instances of Car class and print the instances.
CODE :
class car
{
double topspeed;
String name;
car(String c,double s)
{
topspeed=s;
name=c;
}
public String toString()
{
return "carname"+name+"topspeed"+topspeed;
}
public static void main(String args[])
{
car p1=new car("il0",100);
System.out.println(p1.toString());
car p2=new car("BMW",300);
System.out.println(p2.toString());
car p3=new car("Bugatti",400);
System.out.println(p3.toString());
car p4=new car("Aston",200);
System.out.println(p4.toString());
car p5=new car("Pagani Huayra",350);
System.out.println(p5.toString());
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 19
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 20
AIM : Write a program in Java which has an abstract class Shape having three
subclasses: Triangle, Rectangle, and Circle. Define method area() in the abstract
class Shape and override area() method to calculate the area.
CODE :
abstract class shape
{
abstract void area();
}
class triangle extends shape
{
void area()
{
double b=2.0,h=3.0;
System.out.println("Area of triangle is:"+(0.5*b*h));
}
}
class rectangle extends shape
{
void area()
{
int l=5,b=3;
System.out.println("Area of rectangle is:"+(l*b));
}
}
class circle extends shape
{
void area()
{
double r=2.0;
System.out.println("Area of circle is:"+(3.14*r*r));
}
}
class demo
{
public static void main(String args[])
{
triangle t=new triangle();
rectangle r=new rectangle();
circle c=new circle();
t.area();
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
r.area();
c.area();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 21
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 22
package example;
public class a
{
public void display()
{
System.out.println("Hello");
}
}
OUTPUT :
IMPORT PACKAGE :
import example.a;
class b
{
public static void main(String args[])
{
a p=new a();
p.display();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 23
AIM : Write a program in Java to develop user defined exception for 'Divide
by Zero' error.
CODE :
class mye extends Exception
{
String e;
mye(String s)
{
e=s;
}
public String toString()
{
return e;
}
}
class abc
{
public static void main(String args[])
{
try
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
if(y<=0)
{
throw new mye("number can not devided by 0");
}
else
{
z=x/y;
System.out.println("devided="+z);
}
}
catch(mye e)
{
System.out.println(e);
}
}
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 24
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 25
AIM : Write a program that executes two threads. One thread displays
“Thread1” every 1000 milliseconds, and the other displays “Thread2” every
2000 milliseconds. Create the threads by extending the Thread class .
CODE :
class mythread extends Thread
{
public void run()
{
try
{
for(int i=0;i<=2;i++)
{
if(Thread.currentThread().getName().equals("Thread1"))
{
System.out.println("Thread1");
sleep(1000);
}
if(Thread.currentThread().getName().equals("Thread2"))
{
System.out.println("Thread2");
sleep(2000);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class demo
{
public static void main(String args[])
{
mythread t1=new mythread();
mythread t2=new mythread();
t1.setName("Thread1");
t2.setName("Thread2");
t1.start();
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
t2.start();
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 26
AIM : Write a program that executes two threads. One thread will print the
even numbers and another thread will print odd numbers from 1 to 200.
CODE :
class mythread extends Thread
{
public void run()
{
try
{
for(int i=0;i<=200;i++)
{
if(i%2!=0 && Thread.currentThread().getName().equals("odd"))
{
System.out.println(i+"odd Thread");
}
else if(i%2==0 && Thread.currentThread().getName().equals("even"))
{
System.out.println(i+"even Thread");
}
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class oe
{
public static void main(String args[])
{
mythread t1=new mythread();
mythread t2=new mythread();
t1.setName("odd");
t2.setName("even");
t1.start();
t2.start();
}
}
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
OUTPUT :
.
.
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 27
AIM : Write a program in Java to perform read and write operations on a Text
file.
CODE :
WRITE OPERATION :
import java.io.*;
public class write
{
public static void main(String args[])
{
try
{
FileWriter W=new FileWriter("D:\\java\\AOOP.txt");
W.write("hello world");
W.close();
System.out.println("successfully written...");
}
catch(IOException e)
{
System.out.println("file is not found...");
}
}
}
OUTPUT :
READ OPERATION :
import java.io.*;
import java.util.Scanner;
public class read
{
public static void main(String args[])
{
try
{
FileReader R=new FileReader("D:\\java\\AOOP.txt");
int i;
while((i=R.read())!=-1)
{
System.out.println((char)i);
}
R.close();
}
catch(IOException e)
{
System.out.println("file is not found...");
}
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 28
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 29
AIM : Write a program in Java to create a new HashSet, add colors(in string
form) and iterate through all elements using for-each loop to display the
collection.
CODE :
import java.util.HashSet;
class a
{
public static void main(String args[])
{
HashSet<String> p=new HashSet<String>();
p.add("Red");
p.add("Blue");
p.add("Black");
p.add("White");
p.add("Green");
p.add("Pink");
p.add("yellow");
System.out.println("color");
for(String color:p);
{
System.out.println(p);
}
}
}
OUTPUT :
RMS POLYTECHNIC
AOOP Patel Mann
(4340701) 226420307057
Practical : 30
AIM : Write a Java program to create a new HashMap, add 5 students’ data
(enrolment no and name). retrieve and display the student’s name from
HashMap using enrolment no.
CODE :
import java.util.HashMap;
class b
{
public static void main(String args[])
{
HashMap<Integer,String> S=new HashMap<Integer,String>();
S.put(1,"Siya");
S.put(2,"Riya");
S.put(3,"Jiya");
S.put(4,"Diya");
S.put(5,"Tiya");
System.out.println("Name of enrolmentno1="+S.get(1));
System.out.println("Name of enrolmentno2="+S.get(2));
System.out.println("Name of enrolmentno3="+S.get(3));
System.out.println("Name of enrolmentno4="+S.get(4));
System.out.println("Name of enrolmentno5="+S.get(5));
}
}
OUTPUT :