Java Lab Record
Java Lab Record
SESSION 1
EXERCISE 1:
(i)(a<<2) +(b>>2)
(ii)(a)||(b>0)
(iii)(a+b*100)/10
(iv)a&b
class s12
{
public static void main(String s[])
{
int a=10;
int b=5;
int c;
boolean d;
101
c=(a<<2) + (b>>2);
System.out.println("Answer of (a<<2) + (b>>2)
is "+c);
d=(a>0)||(b>0);
System.out.println("Answer of (a)||(b>0) is "+d);
c=0;
c=(a+b*100)/10;
System.out.println("Answer of (a+b*100)/10 is
"+c);
c=0;
c=a&b;
System.out.println("Answer of a&b is "+c);
}
}
OUTPUT
102
EXERCISE 2:
import java.io.*;
class rectangle
{
double length,width,area;
String colour;
void set_length()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the length : ");
length=Integer.parseInt(br.readLine());
}
catch(IOException e)
103
{
}
}
void set_width()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the width : ");
width=Integer.parseInt(br.readLine());
}
catch(IOException e)
{
System.out.println(e);
}
}
String set_colour()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the colour : ");
colour=br.readLine();
return(colour);
}
catch(IOException e)
{
104
return("0");
}
}
double find_area()
{
area=length*width;
System.out.println("Area of Rectangle is
"+area+"\n");
return(area);
}
}
class s31
{
public static void main(String s[])
{
double area1,area2;
String colour1,colour2;
int value;
rectangle r1=new rectangle();
rectangle r2=new rectangle();
r1.set_length();
r1.set_width();
colour1=r1.set_colour();
area1=r1.find_area();
r2.set_length();
r2.set_width();
colour2=r2.set_colour();
area2=r2.find_area();
105
OUTPUT
106
SESSION 2
EXERCISE 3
import java.io.*;
class account
{
String account_holder;
String addresses,account_type;
int balance;
int total_amt;
int account_number, initial_amount;
account(){}
account(String ah,int an,int iamt)
107
{
account_holder=ah;
account_number=an;
balance=iamt;
}
account(String ah,int an,String add,String tac,int bal)
{
account_holder=ah;
account_number=an;
addresses=add;
account_type=tac;
balance=bal;
}
void deposit(int amt)
{
balance=amt+balance;
}
void withdraw(int amt)
{
balance=balance-amt;
//System.out.println("Current balance :-
"+balance);
}
int get_balance()
{
return balance;
}
}
class s32
{
108
String nm,address,type;
int bal,acct_num,dep_amt,withdraw_amt;
System.out.println("Enter amount to be
deposited :-");
dep_amt=Integer.parseInt(br.readLine());
109
System.out.println("BALANCE IS : "
+a1.get_balance());
}
}
OUTPUT
EXERCISE 4
110
import java.io.*;
class ppd
{
int top=-1;
public static int N=5;
void push(int s[])
{
int x;
if(top>=N-1)
{
System.out.println("\nStack Overflow");
return;
}
try
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter the element
which you want to push into stack : ");
x=Integer.parseInt(br.readLine());
++top;
s[top]=x;
111
}
catch(Exception e)
{
}
}
class s33
{
public static void main(String args[])
{
112
switch(ch)
{
case 1 : p1.push(s);
break;
case 2 : p1.pop(s);
break;
case 3 : p1.display(s);
break;
113
OUTPUT
114
SESSION 4:
EXERCISE 1:
WRITE A JAVA PROGRAM TO SHOW THAT A
PRIVATE MEMBER OF SUPERCLASS CANNOT BE
ACCESSED FROM DERIVED CLASS
class private_member
{
private int a=20;
private String b="Khushvadan";
}
EXERCISE 2
115
System.out.println("Name:- "+name+"\n"+"Age:-
"+age+"\n"+"Runs:- "+runs+"\n"+"Wickets:-
"+wickets+"\n");
}
}
class football_player extends player
{
int goals;
public football_player(String n,int a,int g)
{
super(n,a);
goals=g;
}
void show_details()
{
System.out.println("Information of Football
Player\n");
System.out.println("Name:- "+name+"\n"+"Age:-
"+age+"\n"+"Goals:- "+goals+"\n");
}
}
class hokey_player extends player
{
int goals;
public hokey_player(String n,int a,int g)
{
super(n,a);
goals=g;
}
void show_details()
{
117
System.out.println("Information of Hokey
Player\n");
System.out.println("Name:- "+name+"\n"+"Age:-
"+age+"\n"+"Goals:- "+goals+"\n");
}
}
class s42
{
public static void main(String s[])
{
cricket_player cp=new cricket_player("Sachin
Tendulkar",32,11000,120);
football_player fp=new football_player("Khush
Trivedi",22,52);
hokey_player hp=new hokey_player("Hiren
Pandya",27,52);
cp.show_details();
fp.show_details();
hp.show_details();
}
}
}
OUTPUT
118
SESSION 5:
EXERCISE 1
WRITE A PROGRAM 9N JAVA TO SHOW THE
USEFULLNESS OF INTERFACES AS A PLACE TO
KEEP CONSTANT VALUES OF THE PROGRAM
interface cons
{
int i=35;
void abc();
}
interface cons1
{
void hello();
}
class int1 implements cons, cons1
119
{
public void abc()
{
System.out.println("From interface body :- "+i);
System.out.println("From abc method");
//i=20;
}
public void hello()
{
System.out.println("From hello method");
}
}
class s52
{
public static void main(String s[])
{
int1 a=new int1();
a.abc();
a.hello();
}
}OUTPUT
EXERCISE 2
120
interface Int1
{
void Division();
void Module();
}
class A implements Int1
{
public void Division()
{
int a=10;
int b=5;
System.out.println("Division of two number : -
"+a/b);
}
public void Module()
{
int a=10;
int b=5;
System.out.println("Remainder of two number : -
"+a%b);
}
}
class s53
{
public static void main(String s[])
{
A a1=new A();
121
a1.Division();
a1.Module();
}
}
OUTPUT
SESSION 6
EXERCISE 1
WRITE A PROGRAM IN JAVA TO DISPLAYT THE
NAMES AND ROLL NUMBER OF STUDENT .
INITIALIZE RESPECTIVE ARRAY OF 10 STUDENTS
HANDLE ARRAYINDEXOUTOFBOUNDEXCEPTION
SO THAT IT DOESN’T CAUSE ILLEGAL
TERMINATION OF PROGRAM
import java.io.*;
class students
{
int rno[]=new int[10];
String name[]=new String[10];
int i;
void getdata()
{
try
122
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
for(i=0;i<10;i++)
{
System.out.println("Enter the Roll
No :- ");
rno[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the Name :-
");
name[i]=br.readLine();
}
System.out.println("The roll no of 11th
Student is :- "+rno[10]);
System.out.println("The name of 11th
Student is :- "+name[10]);
}
catch(Exception e)
{
System.out.println("The Array Index can not
be bounded");
}
}
}
class s61
{
public static void main(String a[])
{
students s=new students();
s.getdata();
}
123
OUTPUT
EXERCISE 2
124
import java.io.*;
class s62
{
public static void main(String ar[])
{
int a,b,c;
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of A :- ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the value of B :- ");
b=Integer.parseInt(br.readLine());
c=a/b;
System.out.println("The division of a/b is :- "+c);
}
catch(Exception e)
{
System.out.println("The value of A can not
divide by value of B which is ZERO");
}
}
}
OUTPUT
125
SESSION 7
EXERCISE 1
WRITE A JAVA PROGRAM TO CREATE FIVE
THREADS WITH DIFFERENT PRIORITIES. SEND
TWO THREADS OF THE HIGHEST PRIORITY TO
SLEEP STATE. CHECK THE ALIVENESS OF THE
THREADS AND MARK WHICH THREAD IS LONG
LASTING.
}
public void run()
{
while(running)
{
s++;
}
}
public void stop()
{
running=false;
}
public void start()
{
t.start();
}
}
class s71
{
public static void main(String a[])
{
Thread.currentThread().setPriority(Thread.MAX_PRI
ORITY);
prio t1=new prio(Thread.NORM_PRIORITY
+2);
prio t2=new prio(Thread.NORM_PRIORITY
+2);
prio t3=new prio(Thread.NORM_PRIORITY
+2);
127
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Main Thread
Interrupted");
}
t1.stop();
t2.stop();
t3.stop();
t4.stop();
t5.stop();
try
{
t1.t.join();
t2.t.join();
t3.t.join();
t4.t.join();
t5.t.join();
128
}
catch (InterruptedException e)
{
System.out.println("InterruptedException
Caught");
}
System.out.println("t1 Thread: "+t1.s);
System.out.println("t2 Thread: "+t2.s);
System.out.println("t3 Thread: "+t3.s);
System.out.println("t4 Thread: "+t4.s);
System.out.println("t5 Thread: "+t5.s);
}
}
OUTPUT