OOP’s LAB PROGRAMS
01 Develop a JAVA program to demonstrate the precedence and associativity
among arithmetic operators. The program should also demonstrate how
the default precedence can be overridden.
import [Link];
class Precedence_Associativity
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int a,b,c,res1,res2,res3,res4;
try{
[Link]("Enter a, b and c values: ");
a = [Link]();
b = [Link]();
c = [Link]();
//Precedence
try{
res1 = a+b/c;
res2 = a-b*c;
res3 = a/b-c;
res4 = a%b+c;
[Link]("Precedence:");
[Link]("a+b/c :"+ res1);
[Link]("a-b*c :"+ res2);
[Link](" a/b-c :"+ res3);
[Link]("a%b+c :"+ res4);
}catch(ArithmeticException e){
[Link](“Don’t divide a number by zero”);
}
//Associativity
try{
res1 = a+b-c;
res2 = a-b+c;
res3 = a/b*c;
res4 = a%b*c;
[Link]("Associativity:");
[Link]("a+b-c :"+ res1);
[Link]("a-b+c :"+ res2);
[Link]("a/b*c :"+ res3);
Bangalore University Page 1
OOP’s LAB PROGRAMS
[Link]("a%b*c :"+ res4);
}catch(ArithmeticException e){
[Link](“Don’t divide a number by zero”);
}
//Precedence Overridden
try{
res1 = a*(b-c);
res2 = (a-b)/c;
res3 = a/(b*c);
res4 = (a+b)%c;
[Link]("Precedence Overridden:");
[Link]("a*(b-c) :"+ res1);
[Link]("(a-b)/c) :"+ res2);
[Link]("a/(b*c) :"+ res3);
[Link]("(a+b)%c :"+ res4);
}catch(ArithmeticException e){
[Link](“Don’t divide a number by zero”);
}
}catch(InputMismatchException i){
[Link](“Enter an integer value”);
}
}
}
OUTPUT:
Enter a, b and c values:
30
10
20
Precedence:
a+b/c :30
a-b*c : -170
a/b-c : -17
a%b+c :20
Associativity:
a+b-c :20
a-b+c :40
a/b*c :60
a%b*c :0
Precedence Overridden:
a*(b-c) : -300
(a-b)/c :1
a/(b*c) :0
(a+b)*c :0
Bangalore University Page 2
OOP’s LAB PROGRAMS
02 Write a JAVA program to validate a date. The program should accept day,
month and year and it should report whether they form a valid date or not.
import [Link];
class DateChecker
{
public static void main(String args[])
{
int day=01,month=01,year=2000;
Scanner sc = new Scanner([Link]);
do{
try{
[Link]("Enter day:");
day = [Link]();
if(day<=0 || day>31)
throw new Exception("Invalid Day");
[Link]("Enter month:");
month = [Link]();
if(month<=0 || month>12)
throw new Exception("Invalid Month");
[Link]("Enter year:");
year = [Link]();
String yearInString = [Link](year);
int lengthOfYear = [Link]();
if(year<=0 || lengthOfYear < 4)
throw new Exception("Invalid Year");
boolean result = date_checker(day,month,year);
if(result)
[Link]("Date " + day +":"+month+":" + year+ " is Valid");
else
[Link]("Date " + day +":"+month+":" + year+ " is Invalid");
}catch(Exception e){
[Link]("Error: "+e);
}
}while(day<=0 || day>31 || month<1 || month>12 || year<1);
}
public static boolean date_checker(int d,int m,int y)
Bangalore University Page 3
OOP’s LAB PROGRAMS
{
int max_day = 31;
if(d < 1 || (m < 1 || m > 12) || y < 1)
return false;
switch(m)
{
case 4: case 6: case 9: case 11:
max_day = 30;
break;
case 2: if(((y%400 == 0) || (y%100 != 0)) && (y % 4 == 0 ) )
max_day = 29;
else
max_day = 28;
break;
}
return (d<=max_day);
}
}
OUTPUT:
Enter day:
0
Enter day:
32
Enter day:
07
Enter month:
15
Enter month:
03
Enter year:
2002
Date 07:03:2002 is Valid
Enter day:
31
Enter month:
02
Enter year:
2008
Date 31:02:2008 is Invalid
03 Write a JAVA program to display the following pattern.
Bangalore University Page 4
OOP’s LAB PROGRAMS
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
import [Link].*;
class Tri_Pattern
{
public static void main(String args[])
{
int i,j,k,row = -1;
Scanner sc = new Scanner([Link]);
while(row<=0)
{
try
{
[Link]("Enter row size: ");
row = [Link]();
if(row==0 || row<0)
[Link]("Row value should be greater than one");
}
catch(InputMismatchException e)
{
[Link]("Enter an integer value");
[Link]();
}
}
for(i=1; i<=row; i++){
for(j=row; j>=i; j--){
[Link](" ");
}
for(k=1; k<=i; k++){
[Link](i+" ");
}
[Link]();
}
}
}
OUTPUT:
Bangalore University Page 5
OOP’s LAB PROGRAMS
Enter row size:
n
Enter an integer value
Enter row size:
0
Row value should be greater than one
Enter row size:
-1
Row value should be greater than one
Enter row size:
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Bangalore University Page 6
OOP’s LAB PROGRAMS
04 Write a JAVA program to print the first n members of Fibonacci series.
import [Link];
class fib_recursive
{
public static int fib(int num)
{
if(num == 0)
return 0;
if(num == 1 || num == 2)
return 1;
return fib(num-1)+fib(num-2);
}
public static void main(String args[])
{
int fib_num=-1;
Scanner sc = new Scanner([Link]);
while(fib_num<0)
{
try{
[Link]("Enter a length of fibonacci series:");
fib_num = [Link]();
}catch(Exception e)
{
[Link](“Enter an integer:”);
[Link]();
}
}
for(int i=0; i<fib_num; i++){
[Link](fib(i) +" ");
}
}
OUTPUT:
Enter a length of fibonacci series:
D
Enter an integer:
Enter a length of fibonacci series:
4
0112
Bangalore University Page 7
OOP’s LAB PROGRAMS
05 Write a program to generate the multiplication tables of a range of
numbers between m and n inclusive and m<n.
import [Link];
class MultiplicationTable
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int m=0,n=0;
do{
[Link]("Enter m value:");
try{
m = [Link]();
if(m<=0){
[Link]("Enter a number which is greater than zero:");}
}catch(Exception e)
{
[Link]("Enter an integer value"+" ");
[Link]();
}
}while(m<=0);
do{
[Link]("Enter n value:");
try{
n = [Link]();
if(n<m){
[Link]("Enter n value greater than m value:");}
}catch(Exception f)
{
[Link]("Enter an integer:"+" ");
[Link]();
}
}while(n<m);
for(int i=1;i<=10;i++)
{
[Link](" ");
for(int j=m;j<=n;j++){
int mul = j*i;
[Link](j+"X"+i+"="+mul+"\t");
}
}
}
}
Bangalore University Page 8
OOP’s LAB PROGRAMS
OUTPUT:
Enter m value:
-4
Enter a number which is greater than zero
Enter m value:
A
Enter an integer value:
Enter m value:
2
Enter n value:
K
Enter an integer:
Enter n value:
1
Enter n value greater than m value:
Enter n value:
4
2X1=2 3X1=3 4X1=4
2X2=4 3X2=6 4X2=8
2X3=6 3X3=9 4X3=12
2X4=8 3X4=12 4X4=16
2X5=10 3X5=15 4X5=20
2X6=12 3X6=18 4X6=24
2X7=14 3X7=21 4X7=28
2X8=16 3X8=24 4X8=32
2X9=18 3X9=27 4X9=36
2X10=20 3X10=30 4X10=40
06 Write a JAVA program to define a class, define instance method for setting
and retrieving values of instance variables and instantiate its objects.
class Details
{
int slno;
String title;
int price;
void setValues(int s,String t,int p)
{
slno = s;
title = t;
price = p;
}
Bangalore University Page 9
OOP’s LAB PROGRAMS
void display()
{
[Link]("[Link]:"+slno);
[Link]("Title:"+title);
[Link]("Price:"+price);
[Link]();
}
}
class Book
{
public static void main(String args[])
{
try{
Details d = new Details();
[Link](1,"Haunting",649);
[Link]();
[Link](2,"Heir",999);
[Link]();
}catch(OutofMemoryError e)
{
[Link](“Out of memory error”+[Link]());
}
}
}
OUTPUT:
[Link]
Title:Haunting
Price:649
[Link]
Title:Heir
Price:999
07 Write a JAVA program to demonstrate static member data and static
member methods.
class Counter
{
public static int count;
int vcount;
Counter()
{
count++;
vcount++;
Bangalore University Page 10
OOP’s LAB PROGRAMS
}
static int getCount()
{
return count;
}
double getVCount()
{
return vcount;
}
}
class StaticVar iable
{
public static void main(String args[])
{
Counter c1 = new Counter();
[Link]([Link]());
[Link]([Link]());
Counter c2 = new Counter();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
OUTPUT:
1
1.0
2
1.0
2
08 Write a JAVA program to demonstrate nested classes.
class MotorCycle
{
String name;
MotorCycle(String na)
{
name = na;
}
void showName()
{
[Link]("Brand: "+name);
}
class Model
Bangalore University Page 11
OOP’s LAB PROGRAMS
{
String mod;
Model(String ch)
{
mod = ch;
}
void displayMod()
{
[Link]("Model: "+mod);
}
}
public void start()
{
Model m = new Model("CB 350");
[Link]();
}
}
public class Bike
{
public static void main(String args[])
{
try{
MotorCycle b = new MotorCycle("Honda");
[Link]();
[Link]();
}catch(IllegalAccessException m)
{
[Link](“Illegal Access”+[Link]());
}
}
}
OUTPUT:
Brand: Honda
Model: CB 350
09 Write a JAVA program to demonstrate dynamic method dispatch.
class Marvel
{
void SuperHero()
{
[Link]("Spider-man");
}
}
Bangalore University Page 12
OOP’s LAB PROGRAMS
class DC extends Marvel
{
void SuperHero()
{
[Link]("Batman");
}
void SuperVillain()
{
[Link]("Joker");
}
}
class Hero
{
public static void main(String args[])
{
try{
Marvel m = new Marvel();
[Link]();
Marvel d = new DC();
[Link]();
DC D = new DC();
[Link]();
}catch(Exception e){
[Link](“Error occurred while accessing overridden
method”+[Link]());
}
}
}
OUTPUT:
Spider-man
Batman
Joker
10 Write a JAVA program to implement inheritance and demonstrate method
overriding.
class College1
{
void Student()
{
[Link]("Name: Gopi");
[Link]("Course: MCA");
}
}
Bangalore University Page 13
OOP’s LAB PROGRAMS
class College2 extends College1
{
public void Student()
{
[Link]("Name: Rahul");
[Link]("Course: BCA");
}
}
class Overriding
{
public static void main(String args[])
{
try{
College1 c1 = new College1();
[Link]();
College2 c2 = new College2();
[Link]();
}catch(Exception e)
{
[Link](“An error occurred while accessing Student
method”+[Link]());
}
}
}
OUTPUT:
Name: Gopi
Course: MCA
Name: Rahul
Course: BCA
Bangalore University Page 14