0% found this document useful (0 votes)
32 views

JavaLab EditVer2

Uploaded by

ARIF K F
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

JavaLab EditVer2

Uploaded by

ARIF K F
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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 java.util.Scanner;
class Precedence_Associativity
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b,c,res1,res2,res3,res4;
try{
System.out.println("Enter a, b and c values: ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();

//Precedence
try{
res1 = a+b/c;
res2 = a-b*c;
res3 = a/b-c;
res4 = a%b+c;

System.out.println("Precedence:");
System.out.println("a+b/c :"+ res1);
System.out.println("a-b*c :"+ res2);
System.out.println(" a/b-c :"+ res3);
System.out.println("a%b+c :"+ res4);
}catch(ArithmeticException e){
System.out.println(“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;

System.out.println("Associativity:");
System.out.println("a+b-c :"+ res1);
System.out.println("a-b+c :"+ res2);
System.out.println("a/b*c :"+ res3);
Bangalore University Page 1
OOP’s LAB PROGRAMS

System.out.println("a%b*c :"+ res4);


}catch(ArithmeticException e){
System.out.println(“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;

System.out.println("Precedence Overridden:");
System.out.println("a*(b-c) :"+ res1);
System.out.println("(a-b)/c) :"+ res2);
System.out.println("a/(b*c) :"+ res3);
System.out.println("(a+b)%c :"+ res4);
}catch(ArithmeticException e){
System.out.println(“Don’t divide a number by zero”);
}
}catch(InputMismatchException i){
System.out.println(“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 java.util.Scanner;
class DateChecker
{
public static void main(String args[])
{
int day=01,month=01,year=2000;
Scanner sc = new Scanner(System.in);
do{
try{
System.out.println("Enter day:");
day = sc.nextInt();
if(day<=0 || day>31)
throw new Exception("Invalid Day");

System.out.println("Enter month:");
month = sc.nextInt();
if(month<=0 || month>12)
throw new Exception("Invalid Month");

System.out.println("Enter year:");
year = sc.nextInt();

String yearInString = Integer.toString(year);


int lengthOfYear = yearInString.length();

if(year<=0 || lengthOfYear < 4)


throw new Exception("Invalid Year");

boolean result = date_checker(day,month,year);

if(result)
System.out.println("Date " + day +":"+month+":" + year+ " is Valid");
else
System.out.println("Date " + day +":"+month+":" + year+ " is Invalid");

}catch(Exception e){
System.out.println("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 java.util.*;
class Tri_Pattern
{
public static void main(String args[])
{
int i,j,k,row = -1;
Scanner sc = new Scanner(System.in);
while(row<=0)
{
try
{
System.out.println("Enter row size: ");
row = sc.nextInt();
if(row==0 || row<0)
System.out.println("Row value should be greater than one");
}
catch(InputMismatchException e)
{
System.out.println("Enter an integer value");
sc.next();
}
}
for(i=1; i<=row; i++){
for(j=row; j>=i; j--){
System.out.print(" ");
}
for(k=1; k<=i; k++){
System.out.print(i+" ");
}
System.out.println();
}
}
}

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 java.util.Scanner;
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(System.in);
while(fib_num<0)
{
try{
System.out.println("Enter a length of fibonacci series:");
fib_num = sc.nextInt();
}catch(Exception e)
{
System.out.println(“Enter an integer:”);
sc.next();
}
}
for(int i=0; i<fib_num; i++){
System.out.print(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 java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int m=0,n=0;
do{
System.out.println("Enter m value:");
try{
m = sc.nextInt();
if(m<=0){
System.out.println("Enter a number which is greater than zero:");}
}catch(Exception e)
{
System.out.println("Enter an integer value"+" ");
sc.next();
}
}while(m<=0);
do{
System.out.println("Enter n value:");
try{
n = sc.nextInt();
if(n<m){
System.out.println("Enter n value greater than m value:");}
}catch(Exception f)
{
System.out.println("Enter an integer:"+" ");
sc.next();
}
}while(n<m);
for(int i=1;i<=10;i++)
{
System.out.println(" ");
for(int j=m;j<=n;j++){
int mul = j*i;
System.out.print(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()
{
System.out.println("SL.No:"+slno);
System.out.println("Title:"+title);
System.out.println("Price:"+price);
System.out.println();
}
}

class Book
{
public static void main(String args[])
{
try{
Details d = new Details();
d.setValues(1,"Haunting",649);
d.display();
d.setValues(2,"Heir",999);
d.display();
}catch(OutofMemoryError e)
{
System.out.println(“Out of memory error”+e.getMessage());
}
}
}
OUTPUT:
SL.No:1
Title:Haunting
Price:649
SL.No:2
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();
System.out.println(c1.getCount());
System.out.println(c1.getVCount());
Counter c2 = new Counter();
System.out.println(c2.getCount());
System.out.println(c2.getVCount());
System.out.println(Counter.getCount());
}
}
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()
{
System.out.println("Brand: "+name);
}

class Model
Bangalore University Page 11
OOP’s LAB PROGRAMS

{
String mod;
Model(String ch)
{
mod = ch;
}
void displayMod()
{
System.out.println("Model: "+mod);
}
}
public void start()
{
Model m = new Model("CB 350");
m.displayMod();
}
}

public class Bike


{
public static void main(String args[])
{
try{
MotorCycle b = new MotorCycle("Honda");
b.showName();
b.start();
}catch(IllegalAccessException m)
{
System.out.println(“Illegal Access”+m.getMessage());
}
}
}
OUTPUT:
Brand: Honda
Model: CB 350
09 Write a JAVA program to demonstrate dynamic method dispatch.

class Marvel
{
void SuperHero()
{
System.out.println("Spider-man");
}
}
Bangalore University Page 12
OOP’s LAB PROGRAMS

class DC extends Marvel


{
void SuperHero()
{
System.out.println("Batman");
}
void SuperVillain()
{
System.out.println("Joker");
}
}
class Hero
{
public static void main(String args[])
{
try{
Marvel m = new Marvel();
m.SuperHero();
Marvel d = new DC();
d.SuperHero();
DC D = new DC();
D.SuperVillain();
}catch(Exception e){
System.out.println(“Error occurred while accessing overridden
method”+e.getMessage());
}
}
}
OUTPUT:
Spider-man
Batman
Joker
10 Write a JAVA program to implement inheritance and demonstrate method
overriding.

class College1
{
void Student()
{
System.out.println("Name: Gopi");
System.out.println("Course: MCA");
}
}

Bangalore University Page 13


OOP’s LAB PROGRAMS

class College2 extends College1


{
public void Student()
{
System.out.println("Name: Rahul");
System.out.println("Course: BCA");
}
}

class Overriding
{
public static void main(String args[])
{
try{
College1 c1 = new College1();
c1.Student();
College2 c2 = new College2();
c2.Student();
}catch(Exception e)
{
System.out.println(“An error occurred while accessing Student
method”+e.getMessage());
}
}
}
OUTPUT:
Name: Gopi
Course: MCA
Name: Rahul
Course: BCA

Bangalore University Page 14

You might also like