Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

OOP - JAVA - Lab Sabbir

This lab report summarizes code written for an Object Oriented Programming course. It includes code examples demonstrating primitive data types, operators, control structures like if/else statements and loops, arrays, command line arguments, patterns, checking prime numbers, and an example of a simple class with constructors. The full lab report contains over 30 code examples covering fundamental OOP concepts.

Uploaded by

Muntasir Nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

OOP - JAVA - Lab Sabbir

This lab report summarizes code written for an Object Oriented Programming course. It includes code examples demonstrating primitive data types, operators, control structures like if/else statements and loops, arrays, command line arguments, patterns, checking prime numbers, and an example of a simple class with constructors. The full lab report contains over 30 code examples covering fundamental OOP concepts.

Uploaded by

Muntasir Nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Jashore University of Science and Technology

Department of Computer Science and Engineering


Course Title: Object Oriented Programming

Course Code: CSE-2104

Lab report on Object Oriented Programming

Submitted To Submitted By

Atish Kumar Dipongkor Name: Sabbir Ahmed

Assistant Professor Id: 190110

Department of Computer Science and 2nd Year 1st Semester


Engineering.
Session: 2019-2020
Jashore University of Science and
Department of Computer Science and
Technology.
Engineering.

Jashore University of Science and


Technology.

Remarks:

Date of Submission: 30/04/2022


1.1 Display any message:

public class OOP_LAB {


public static void main(String[] args) {
System.out.println("LOL");
}
}

1.2 Display default values of all primitive data types:

class Oop_Lab_AS {
short s;
int i;
long l;
float f;
double d;
char c;
String ss;
Boolean b;
public static void main(String[] args) {
Oop_Lab_AS op = new Oop_Lab_AS();
System.out.println("Short = " + op.s);
System.out.println("Int = " +op.i);
System.out.println("Float = " +op.f);
System.out.println("Double = " +op.d);
System.out.println("Char = " +op.c);
System.out.println("String = " +op.ss);
System.out.println("Boolean = " +op.b);
}
}
1.3 Check two strings are equal or not:

class OOP_LAB
{
public static void main (String args [ ])
{
String str1 = "LOL";
String str2 = "LoL";
System.out.println ("Str1 :"+str1);
System.out.println("Str2 :" + str2);
System.out.print("Str1 == Str2 :");
System.out.println(str1==str2);
System.out.println ("Str1.equals(Str2): " +
str1.equals(str2));
}
}

2.1.1 Increment and Decrementing Operator:

class OOP_JAVA
{
public static void main(String args[]) {
int x = 8, y = 13;
System.out.println("x =" + x);
System.out.println("y =" + y);
System.out.println("x =" + x);
System.out.println("y =" +y);
System.out.println("++x =" + ++x);
System.out.println("y++ =" + y++);
}
}
2.1.2 Bitwise Complement Operators:

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

int x = 8;
System.out.println ("x =" +x);
int y = ~x;
System.out.println ("y =" +y);
}
}

2.1.3 Arithmetic operator

class OOP_LAB
{
public static void main(String[] args) {
{
float x = 5.5f, y = 2.3f;
System.out.println("x =" + x);
System.out.println("y =" + y);
System.out.println("x + y =" + ( x + y)) ;
System.out.println("x - y =" + (x - y)) ;
System.out.println("x * y =" +( x* y));
System.out.println("x / y =" + ( x / y));
System.out.println("x % y =" + ( x % y));
}
}
}
2.1.4 Relational Operator

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

int x = 5, y = 13, z = 2;
System.out.println("x= " + x);
System.out.println("y = " + y);
System.out.println("x < y = " + ( x < y ) );
System.out.println("x > z = "+ (x > z) );
System.out.println("x <= z = "+ (y <= z) );
System.out.println("x >= y = " + (x >= y ) );
System.out.println("y == z = " + (y ==z) );
System.out.println("x != z = " +(x != z) );
}
}

2.1.5 Bitwise Operator:

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

int x = 5, y = 6;
System.out.println("x = " +x);
System.out.println("y = " + y );
System.out.println("x&y = "+ ( x & y) ) ;
System.out.println("x|y = "+ ( x | y ) );
System.out.println("x^y = "+( x ^ y) );
}
}
2.1.6 Conditional Operator:

class OOP_JAVA
{
public static void main (String args [ ] )
{
int x = 3;
boolean isEven = false;
System.out.println ("x = " + x);
x = isEven ? 4: 7;
System.out.println ("x = " + x);
}
}

2.2.1 If statement

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

int x = 3, y = 4;
System.out.println("X = 3");
System.out.println("Y = 4");
if (x<y)
{
System.out.println("Y is greater than X");
}
else {
System.out.println("X is greater than Y");
}
}
}
2.2.2 Switch Statement:

class OOP_JAVA {
public static void main(String args[]) {
char ch = 'A';
switch (ch) {
case 'A':
System.out.println("Value is A");
break;
case 'B':
System.out.println("Value is B");
break;
default:
System.out.println("Unknown Value");
}
}
}

2.2.3 For Loop:

class OOP_JAVA {
public static void main(String args[]) {
int i;
for (i = 0; i < 10; i++) {
System.out.println(i);
}
}
}

2.2.3 While Statement:

class OOP_JAVA {
public static void main(String args[]) {
int i=0;
while (i<10){
System.out.println(i);
i++;
}
}
}

2.2.4 Do Statement:

class OOP_JAVA {
public static void main(String args[]) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
}
}

2.3.1 Length of an array:

class OOP_JAVA {
public static void main(String args[]) {
int arr1[] = { 1, 5, 6 };
System.out.println(arr1.length);
}
}

2.3.2 One Dimensional Array:

import java.util.Arrays;

class Oop_lab_as {
public static void main (String args [ ] )
{
int arr[] = new int[5],i;
arr[0] = 5 ;
arr[1] = 7 ;
arr[2] = 9 ;
arr[3] = 1;
for (i = 0; i < arr.length ; i++) {
System.out.println("Array " + (i+1) +" = " + arr[i]);
}

2.3.3 A 2D Array:

class Oop_lab_as {
public static void main (String args [ ] )
{
int arr[][] = new int[1][2], i;

for (i = 0; i < 2; i++) {


for (int j = 0; j < 4; j++) {
System.out.print("*");
}
System.out.println();
}
}

2.3.4 Multi-Dimensional Array:

class Oop_lab_as {
public static void main(String a[]) {
int sum = 0;
int invalid = 0;
for (int I = 0; I < a.length; I++) {
try {
sum += Integer.parseInt(a[I]);
} catch (NumberFormatException e) {
invalid++;
}
}
System.out.println("Total no. of arguments :" +
a.length);
System.out.println("Sum :" + sum);
System.out.println("Invalid Integers:" + invalid);
}
}

2.4 Command line argument example:

class Oop_lab_as {
public static void main(String args[]) {
System.out.println("First element passed is: "+args[0] );
}
}

2.4.1 Sum of command-line arguments:

class Oop_lab_as {
public static void main(String args[]) {
int i,sum = 0;
for (i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
System.out.println(sum);
}
}
2.4.2 To get a name using the command line:

class Oop_lab_as {
public static void main(String args[]) {
System.out.println(args[0]);
}
}

2.5.1 Pattern

import java.util.Scanner;

class Oop_lab_as {
public static void main(String args[]) {
String k = "1", l = " ", s = "1";
int m = 0;
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
l += "0";
}
System.out.println(k + l + s);
l = "";
m += 2;
}
}

2.5.2 Pattern

class Oop_lab_as {
public static void main(String args[]) {
int ll = 7;
for (int i = 1; i <= ll; i++) {
for (int j = 1; j < i -1 ; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

2.6.1 Prime number check:

class Oop_Lab_AS {
public static void main(String[] args) {
int i = 0, num = 0, count;
String prime = "";
for (i = 0; i <= 100; i++) {
count = 0;
for (num = i; num >= 1; num--) {
if (i % num == 0) {
count++;
}
}
if (count == 2) {
prime = prime + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100: ");
System.out.println(prime);
}
}

2.6.2 Sum of digits:

class Oop_lab_as {
public static void main(String args[]) {
int sum = 0, i;
for (i = 0; i < 5; i++) {
sum += i;
}
System.out.println(sum);
}
}

3.1

import java.util.Scanner;
import java.util.Arrays;

public class Room(


int roomNo;
String roomType;
float roomArea;
Boolean acMachine;

void setDate(int rno, String rtype, double d, Boolean aM) {


roomNo = rno;
roomType = rtype;
roomArea = (float) d;
acMachine = aM;
}
void displaydata() {
System.out.println("Room no: "+roomNo);
System.out.println("Room type is "+roomType);
System.out.println("Room area is "+roomArea);
String s = (acMachine)? "On":"Off";
System.out.println("Ac is "+s);
}
)
class Oop_lab_as {
public static void main(String args[]) {
Room room1 = new Room();
room1.setDate(909,"Noes", 505.0, true);
room1.displaydata();
}
}
3.2

class SimpleObject{
SimpleObject() {
System.out.println("This is a simple obj");
}
SimpleObject(int a) {
System.out.println("This is a simple o");
}
}

class Oop_lab_as {
public static void main(String args[]) {
new SimpleObject();
new SimpleObject(33);
}
}

3.3 Call by value and call by reference

//Call by reference
class Value{
int j;
void test(Value j)
{
j.j=100;
}
}
public class Oop_Lab_AS {
public static void main(String[] args) {
Value cn=new Value();
cn.j=200;
System.out.println(cn.j);
cn.test(cn);
System.out.println(cn.j);
}
}
//Call value
class Value{
void test(int j)
{
j=100;
}
}
public class Oop_Lab_AS {
public static void main(String[] args) {
Value cn=new Value();
int x=200;
cn.test(x);
System.out.println(x);
}
}

3.4

class Oop_lab_as {
int x;
int y;

public void Dis(int x, int y) {


this.x = x;
this.y = y;
}
public static void main(String args[]) {
Oop_lab_as o = new Oop_lab_as();
o.Dis(4, 3);
System.out.println(o.x);
}
}
4.1 Static var, method, and block:

class Oop_lab_as {
static int a = 3;
static int b;
static int y;

static void meth(int x) {


System.out.printf("x = ", +x);
System.out.printf("a = ", +a);
System.out.printf("b = ", +b);
}

static {
System.out.println("Static block initialized");
b = a * 4;
}

public static void main(String args[]) {


meth(44);
}
}

class StatDemo {
static int a = 55;
static int b = 45;

static void callme() {


System.out.println("a = " + a);
}

class StatName {
public static void main(String[] args) {
StatDemo.callme();
System.out.println("b = " + StatDemo.b);
}
}
}

4.2 Reuse class

public class Oop_lab_as {


Dis r;
void house() {
r = new Dis();
}
void display() {
r.dis(x, y);
}
public static void main(String[] args) {
Oop_lab_as o = new Oop_lab_as();
o.house();
o.display();
}
}

4.3 Inheritance:

class A {
int i, j;

int disp(int i, int j) {


this.i = i;
this.j = j;
System.out.println("i " + i + "j " + j);
return j+i;
}
}
class B extends A{
int k;
}
public class Inherit {
public static void main(String[] args) {
B b = new B();

System.out.println(b.disp(5, 8));
}
}

4.4 Method Override

class A {
int k, l;
int An() {
return 0;
}
int An(int k, int l) {
this.k = k;
this.l = l;
return k + l;
}
}
public class Override {
public static void main(String[] args) {
A a = new A();

System.out.println(a.An(5, 6));
}
}
4.5 Super

class Supp {
int speed = 100;
}
class Sub extends Supp {
int speed = 80;

public void sdisp() {


System.out.println(speed); //prints 80
System.out.println(super.speed); //prints 100
}
}
public class Oop_lab_AS{
public static void main(String[] args) {
Sub b = new Sub();
b.sdisp();
}
}

5.1 Polymorphism:

class Shape {
void draw() {
System.out.println("Draw");
}
void erase() {
System.out.println("Erase");
}
}
class Circle extends Shape{
void draw() {
System.out.println("Draw");
}
void erase() {
System.out.println("Erase");
}
}
class Triangle extends Shape{
void draw() {
System.out.println("Draw");
}
void erase() {
System.out.println("Erase");
}
}
class Square extends Shape{
void draw() {
System.out.println("Draw");
}
void erase() {
System.out.println("Erase");
}
}
public class Oop_lab_AS {
public static void main(String[] args) {
Shape sh = new Shape();
Circle cr = new Circle();
Triangle tr = new Triangle();
Square sq = new Square();
}
}

5.2 Abstract Class:

abstract class Abst {


int i;

void disp() {
System.out.println("This is abstract!");
}
}
class NtAbst extends Abst{

}
public class Oop_lab_AS {
public static void main(String[] args) {
NtAbst nt = new NtAbst();
nt.disp();
}
}

5.3

abstract class Debugg {


abstract void dumpp(){
// System.out.println("Debugging error: no dumpp()
defined for the class" );
// }
}
class xx extends Debugg {
private int a, b, c;
public:
x (int aaa =0,int bbb=0,int ccc=0)
{
a = aaa;
b = bbb;
c = ccc;
}
void dumpp() {
System.out.println("a = " + a + " b= " + b + " c= ");
}
}
class Y extends Debugg {
private int i, j, k;

public:
Y(int ii = 0, int jj=0,kk=0);
}
void dumpp() {
System.out.println("i = " + i + " j = " + j + " k = " +
k);
}
class Z extends Debugg {
private int p, q, r;
public:
Y (int pp = 0,int qq = 0,rr = 0){
p = pp;
q = qq;
r = rr;
}
void dumpp() {
System.out.println("p= " + p + " q= " + " r= " + r);
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
X x(1,2,3);
Y y(2,4,5);
Z z;
x.dumpp();
y.dumpp();
z.dumpp();
}
}
}}

6.1 Interface:

interface A {
void meth1();
void meth2();
}
public class Oop_lab_AS implements A {
public static void main(String[] args) {
Oop_lab_AS ola = new Oop_lab_AS();
ola.meth1();
ola.meth2();
}
public void meth1() {
System.out.println("This is meth1");
}
public void meth2() {
System.out.println("This is meth2");
}
}

6.2 Multiple Inheritance:

class A {
void tell() {
System.out.println("Tell method");
}
}

class B {
void speak() {
System.out.println("Speak method");
}
}

class Oop_lab_AS extends A,B { //Error as multiple inheritence


is not supported in JAVA
public static void main(String[] args) {

}
}
6.3 Interface Square

interface Test {
int square(int x);
}
class Arithmetic implements Test {
public int square(int x) {
return x * x;
}
}

class ToTestInt{
public static void main(String[] args) {
Arithmetic a = new Arithmetic();
System.out.println(a.square(7));
}
}

6.4 Display functions

class Outer {
void display() {
System.out.println("From outer class!");
}
}
class Inner {
void display() {
System.out.println("From inner class!");
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
Outer o = new Outer();
Inner i = new Inner();
o.display();
i.display();
}
}

7.1 RGB

7.2 Font check

import java.awt.*;

public class Oop_Lab_AS {


public static void main(String arg[]) {
Font f;
f = new Font("TimesNewRoman", Font.BOLD + Font.ITALIC, 12);
String font = f.getName();
int style = f.getStyle();
int size = f.getSize();
boolean bold = f.isBold();
boolean normal = f.isPlain();
boolean italic = f.isItalic();
System.out.println("Font : " + font + "\n Style : " + style +
"\nSize: " + size + "\n Bold : " + bold
+ "\n Italic : " + italic);
}
}

7.3.1 Factorial:

class Fact {
int fact(int n) {
if (n <= 1) {
return 1;
}
else {
return n * fact(n - 1);
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
Fact f = new Fact();
System.out.println(f.fact(5));
}
}
}

7.3.2 Fibonacci

class Fact {
int fact(int n) {
if (n <= 1) {
return 1;
}
else {
return n + fact(n - 1);
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
Fact f = new Fact();
System.out.println(f.fact(5));
}
}
}

8.1

class Cool {
public int x,y;
public Cool()
{
x=1;
y=1;
}
Cool(int x,int y)
{
this.x=x;
this.y=y;
}
void Sx(int x)
{
this.x=x;
}
void Sy(int y)
{
this.y=y;
}
void Sxy(int x,int y)
{
this.x=x;
this.y=y;
}
int Gx()
{
return x;
}
int Gy()
{
return y;
}
int Axy()
{
return x+y;
}
}
public class Oop_Lab_AS {
public static void main(String[] args) {
Cool p1=new Cool(5,7);
System.out.println(p1.Axy());
p1=new Cool();
System.out.println(p1.Axy());
p1.Sx(10);
p1.Sy(12);
System.out.println(p1.Axy());
System.out.println(p1.Gx());
}
}

8.2

class Cool {
private double nm;
Cool(double nm)
{
this.nm=nm;
}
double gnm()
{
return nm;
}
int Iszr(double nm)
{
if(nm==0)return 1;
else return 0;
}
int isPos(double nm)
{
if(nm>0)return 1;
else return 0;
}
int isNeg(double nm)
{
if(nm<0)return 1;
else return 0;
}
int isEvn(double nm)
{
if(nm%2==0)return 1;
else return 0;
}
int isOd(double nm)
{
if(nm%2==1)return 1;
else return 0;
}
int isPrimee(double nm)
{
int i;
int b=0;
for(i=2;i<nm;i++)
{
if(nm%i==0)
{
b=1;
break;
}
}
if(b==1)return 0;
else return 1;
}
int isAms(double nm)
{
double tmp =nm;
double sum=0;
while(tmp !=0)
{
double rem=tmp %10;
sum=sum+(rem*rem*rem);
tmp =tmp /10;
}
if(sum==nm)return 1;
else return 0;
}
double getFact(double nm)
{
double f=1;
for(double i=1;i<=nm;i++)
{
f=f*i;
}
return f;
}
double sqrtt(double nm)
{
double s=Math.sqrt(nm);
return s;
}
double sqr(double nm)
{
return nm*nm;
}
double sumofdig(double nm)
{
int sum=0,tmp =(int)nm;
while(tmp !=0.0)
{
int rem=tmp %10;
sum=sum+rem;
tmp =tmp /10;
}
return sum;
}
double rev(double nm)
{
int tmp =(int)nm,rev=0;
while(tmp !=0)
{
int rem=tmp %10;
rev=rev*10+rem;
tmp =tmp /10;
}
return rev;
}
public void disBinary()
{
int n=(int)nm;
System.out.println(Integer.toBinaryString(n));
}
}
public class Oop_Lab_AS {
public static void main(String[] args) {
Cool n1=new Cool(5);
System.out.println(n1.Iszr(n1.gnm()));
System.out.println(n1.isPos(n1.gnm()));
System.out.println(n1.isNeg(n1.gnm()));
System.out.println(n1.isOd(n1.gnm()));
System.out.println(n1.isEvn(n1.gnm()));
System.out.println(n1.isPrimee(n1.gnm()));
System.out.println(n1.isAms(n1.gnm()));
System.out.println(n1.getFact(n1.gnm()));
System.out.println(n1.sqrtt(n1.gnm()));
System.out.println(n1.sqr(n1.gnm()));
System.out.println(n1.sumofdig(n1.gnm()));
System.out.println(n1.rev(n1.gnm()));
n1.disBinary();
}
}

8.3 Package

package mypack;
class Circle {
final double PI=3.1316;
public void Area(int r){
double Arr = PI * r * r;
System.out.println(Arr);
}
}
public final class App {
private App() {
}
public static void main(String[] args) {
Circle c = new Circle();
c.Area(4);
}
}

8.4 Package

package p1;

class ex1 {
void disp() {
System.out.println("From ex1's method");
}
}
public class App
{
public static void main( String[] args )
{
ex1 e = new ex1();
e.disp();
}
}
9.1 Method Overriding:

class Demo {
void disp() {
System.out.println("No param!");
}
void disp(int a, int b) {
System.out.println("Int a and b: " + a + " " + b);
}
void disp(double a, double b) {
System.out.println("Double a and b: "+a +" " +b);
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
Demo d = new Demo();
d.disp();
d.disp(4, 5);
d.disp(1.2, 2.5);
}
}

9.2 2D and 3D Box

class Box {
private int length;
private int breadth;

public Box() {
length = 0;
breadth = 0;
}

public Box(int x, int y) {


length = x;
breadth = y;
}
public void setval(int x, int y) {
length = x;
breadth = y;

public int area() {


return (length * breadth);
}
}

class Box3d extends Box {


private static Object Box3d;
private int height;

public Box3d() {
super();
height = 0;
}

class Box3d(
int x,
int y,int z){
super(x,y);
height = z;

public void setval(int x, int y, int z) {


super.setval(x, y);
height z;
}

public int volume() {


return (super.area() * height);
}
}
public static void main(String[] args) {
Box b1;
Object Box = b1 = new Box();
Object b2;
Box3d = b2 = new Box3d(15,45,68);
b1.setval(20,45);
System.out.println("The area of b1 is: "+b1.area());
System.out.println("the volume of b2 is: "+b2.volume());
}
}

9.3 Vector:

import java.util.Vector;

class Oop_Lab_AS {
public static void main(String[] args) {
Vector<String> bangladesh = new Vector<>();
bangladesh.add("Khulna");
bangladesh.add("Dhaka");
bangladesh.add("Rajshahi");
bangladesh.add("Sylhet");
bangladesh.add("Jashore");
System.out.println("Bangladesh: " + bangladesh);
System.out.println("Elements: "+bangladesh.size());
}
}
10.1 Try/Catch

class Oop_Lab_AS {
public static void main(String[] args) {
try {
int arr[] = new int[-3];
System.out.println("First Element: "+arr[0]);
}
catch (NegativeArraySizeException x){
System.out.println("Generated Exception: " + x);
}
System.out.println("Outside try block!");
}
}

10.2 Multiple Try/Catch

import java.io.*;

class Oop_Lab_AS {
public static void main(String[] args) {
int a1[] = { 100, 300, 200, 500, 700 };
System.out.println("Type an index number to know its
value: ");
try {
String line;
int x;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
while ((line = br.readLine()) != null) {
if (line.equals("end"))
break;
else {
try {
x = Integer.parseInt(line);
System.out.println("Valid element is:" +
a1[x]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid elements ");
System.out.println("Exception Generated:
" + e);
} catch (NumberFormatException n) {
System.out.println("Sorry no charecter");
System.out.println("Generated exception:
" + n);
}
}
}
} catch (IOException i) {
}
}
}

10.3 File Exception

import java.io.*;

class BaseSubException {
public static void main(String a[]) throws IOException {
if(a.length == 0)
{
System.out.println("Invalid usage");
System.out.println("Usage : Java <file> file1 file2
file3...");
return;
}
for(int i=0;i<a.length;i++)
{
File f = new File(a[i]);
try{
String line;
DataInput d = new DataInputStream(new
FileInputStream(a[i]));
if(f.exists() && f.isFile()){
System.out.println("File exists");
System.out.println(f+ "is ordinary file");
System.out.println("Printing the contents of
file named : "+a[i]);
while((line =d.readLine()) != null){
System.out.println(line);
}
}
}
catch(FileNotFoundException e)
{
if (f.exists() && f.isDirectory()) {
System.out.println("Name :" + f + " is a
Directory");
System.out.println("Inside Catch");
}
else {
System.out.println("Name : " + a[i] + "Does
not exists");
System.out.println("Generated Exception:
"+e);
}
} catch (IOException p) {
System.out.println("Super class is higher up in
the program");
}
}
}
}

10.4

import java.io.*;
class Oop_Lab_AS {
public static void main(String[] args) throws IOException {
try {
BufferedInputStream bf = null;
int size = bf.available(), i;
for (i = 0; i < size; i++) {
System.out.println((char) bf.read());
}
}
catch (NullPointerException n) {
System.out.println("Exception generated: "+n);
}
finally {
System.out.println("Inside \"Finally\"");
}
}
}

10.5

class Oop_Lab_AS {
public static void main(String[] args) throws
ArithmeticException
{
System.out.println("Main function");
int i = 0;
int j = 40 / i;
System.out.println("Not printed!");
}
}

10.6 User-defined Exception:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Myexc extends Exception {


private int a;
private String[] Myexception;
Myexc(int b) {
a = b;
}
public String toString() {
return "Myexception [ " +a "]";
}
}
class UserDefException {
public int x;
final int k = 5;
void geInt() {
try{
BufferedReader d = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Guess to gen exception");
System.out.println("Enter a number between 1 to 10");
String line;
while((line = d.readLine()) != null){
x = Integer.parseInt(line);
if(x == 5){
System.out.println("Generated Exception");
throw new Myexc(x);
}
else
{
System.out.println("Wrong");
continue;
}
}
catch(Myexc m){
System.out.println("Generated Exception" +m);
}
catch(NumberFormatException n){
System.out.println("No char!");
System.out.println("Exiting");
System.out.println("Generated Exception"+n);
}
catch(IOException e){}
}
}
}
class Oop_Lab_AS {
public static void main(String[] args) {
UserDefException u = new UserDefException();
u.geInt();
}
}

You might also like