You are on page 1of 29

100

SESSION 1

EXERCISE 1:

WRITE A JAVA PROGRAM TO FIND THE RESULT


OF FOLLOWING EXPRESSION
(ASSUME a=10, b=5)

(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:

WRITE A PROGRAM IN JAVA WITH CLASS


RECTANGLE WITH THE DATA FIELDS WIDTH
LENGTH COLOUR AREA. THE LENGTH AND
WIDTH AND AREA IS OF DOUBLE TYPE AND
COLOR IS OF STRING TYPE. THE METHODS ARE
set_length,set_width, and set_colour and find_area.
CREATE TWO OBJECTS OF RECTANGLE AND
COMPAR THEIR AREA AND COLOUR. IF AREA AND
COLOUR BOTH ARE THE SAME THEN DISPLAY
MATCHING RECTANGLES OTHERWISE NON
MATCHING RECTANGLES.

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

if(area1==area2 && colour1.equals(colour2))


{
System.out.println("Matching Rectangle");
}
else
{
System.out.println("Non-Matching
Rectangle");
}
}
}

OUTPUT
106

SESSION 2

EXERCISE 3

CREATE A CLASS ACCOUNTWITH TWO


OVERLOADED CONSTRUCTORS. THE FIRST
CONSTRUCTOR IS USED FOR INITIALISING THE
NAME OF ACCOUNT HOLDER, THE ACCOUNT
NUMBER AND THE INITIAL AMOUNT IN THE
ACCOUNT. THE SECOND CONSTRUCTOR IS USED
FOR INITIALISING THE NAME OF ACCOUNT
HOLDER , THE ACCOUN NUMBER THE
ADDRESSES, THE TYPE OF ACCOUNT, AND THE
CURRENT BALANCE. THE ACCOUNT CLASS IS
HAVING METHODS Deposit(), Withdraw(), AND
Get_Balance()
MAKE THE NECESSARY ASSUPTIONS FOR DATA
MEMBERS AND RETURN THE TYPES OF THE
METHODS. CREATE OBLECTS OF ACCOUNT CLASS
AND USE THEM.

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

public static void main(String s[]) throws IOException


{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

String nm,address,type;
int bal,acct_num,dep_amt,withdraw_amt;

System.out.println("Enter the name of account


holder :-");
nm=br.readLine();

System.out.println("Enter the address of account


holder :-");
address=br.readLine();

System.out.println("Enter the account number :-


");
acct_num=Integer.parseInt(br.readLine());

System.out.println("Enter Type of account :-");


type=br.readLine();

System.out.println("Enter current balance :-");


bal=Integer.parseInt(br.readLine());

System.out.println("Enter amount to be
deposited :-");
dep_amt=Integer.parseInt(br.readLine());
109

System.out.println("Enter withdraw amount :-");


withdraw_amt=Integer.parseInt(br.readLine());

account a1=new account(nm,acct_num,bal);


account a2=new
account(nm,acct_num,address,type,bal);

account a3=new account();


a1.deposit(dep_amt);
a1.withdraw(withdraw_amt);

System.out.println("BALANCE IS : "
+a1.get_balance());
}
}
OUTPUT

EXERCISE 4
110

WRITE A PROGRAM IN JAVA TO CREATE A STACK


CLASS OF VARIABLE SIZE AND PUSH() AND POP()
METHODS. CREATE TWO OBJECTS OF STACK
WITH 10 DATA ITEMS IN BOTH. COMPARE THE
TOP ELEMENT OF BOTH STACK AND PRINT THE
COMPARISON RESULT.

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)
{
}
}

void pop(int s[])


{
int y;
if(top==-1)
{
System.out.println("\nStack UnderFlow");
return;
}
y = s[top];
top--;
System.out.println("\nElement popped = "+y);
}

void display(int s[])


{
int i;
for(i=top;i>=0;i--)
System.out.println("\nElement = "+s[i]);
}
}

class s33
{
public static void main(String args[])
{
112

int s[]=new int[5];


int flag=1;
int ch=0;
ppd p1=new ppd();
while(flag == 1)
{
System.out.println("\n1. Push");
System.out.println("\n2. Pop");
System.out.println("\n3. Display");
System.out.println("\n4. Quit");
try
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter your choice : ");
ch=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
}

switch(ch)
{
case 1 : p1.push(s);
break;

case 2 : p1.pop(s);
break;

case 3 : p1.display(s);
break;
113

case 4 : flag =0;


break;
}
}
}
}

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";
}

class s41 extends private_member


{
public static void main(String s[])
{
private_member p=new private_member();
System.out.println("Value of a is "+p.a);
System.out.println("Value of b is "+p.b);
}
}OUTPUT

EXERCISE 2
115

WRITE A PROGRAM IN JAVA TO CREATE A


PLAYER CLASS. INHERIT THE CLASSES CRICKET
PLAYER HOCKEY PLAYER FOOT BALL PLAYER
FROM PLAYER CLASS.
class player
{
String name;
int age;

public player(String n,int a)


{
name=n;
age=a;
}
}
class cricket_player extends player
{
int runs;
int wickets;

public cricket_player(String n,int a,int r,int w)


{
super(n,a);
runs=r;
wickets=w;
}
void show_details()
{
System.out.println("Information of Cricket
Player\n");
116

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

CREATE AN INTERFACE HAVING TWO METHODS


DIVISION AND MODULES. CREATE A CLASS
WHICH OVERRIDES THESE METHODS

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

WRITE A JAVA PROGRAM TO ENABLE THE USER


TO HANDLE ANY CHANCE OF DIVIDE BY ZERO
EXCEPTION

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.

class prio implements Runnable


{
int s=0;
Thread t;
private volatile boolean running=true;
public prio(int p)
{
t=new Thread(this);
t.setPriority(p);
126

}
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

prio t4=new prio(Thread.NORM_PRIORITY


+2);
prio t5=new prio(Thread.NORM_PRIORITY
+2);

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

You might also like