Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Lab Exercise – 1

Q1. Write a program in Java to print “Hello World”.


Ans:
class First
{
public static void main ( String args[ ])
{
System.out.println("Hello World");
}
}
Output:

Q2: Write a program to print the following patterns:


Ans:
i)
class Main
{
public static void main (String args[])
{
int a = Integer.parseInt(args[0]);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < a - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output:

1
ii)
public class Main
{
public static void main (String args[])
{
int a = Integer.parseInt(args[0]);
for (int i = a; i >= 0; i--)
{
for (int j = 0; j < a - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output:

Q3: Write a program in Java to print the table of a number received through command
line argument.
Ans:
class Main
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
for(int i = 1; i <= 10; i++)
System.out.println(a + " X " + i + " = " + a * i);
}
}

2
Lab Exercise – 2

Q1. Write a Java program to perform basic Calculator operations.


Ans:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbrs: ");
double fir = sc.nextDouble();
double sec = sc.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char oper = sc.next().charAt(0);
double res;
switch(oper)
{
case '+':
res = fir + sec;
break;

case '-':
res = fir - sec;
break;

case '*':
res = fir * sec;
break;

case '/':
res = fir / sec;
break;

default:
System.out.print("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", fir, oper, sec, res);
}
}

Q2: Write a Java program to calculate a Factorial of a number.

3
Ans:
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, f = 1;
System.out.println("Enter an integer to calculate its factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
f = f*c;

System.out.println("Factorial of "+n+" is = "+f);


}
}
}

Q3: Write a Java program to calculate Fibonacci Series up to n numbers.


Ans:
import java.util.Scanner;
class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}

4
Q4: Write a Java program to find out whether the given String is Palindrome or not.
Ans:
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it's a palindrome");


original = in.nextLine();

int length = original.length();

for (int i = length - 1; i >= 0; i--)


reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("The string is a palindrome.");
else
System.out.println("The string isn't a palindrome.");
}
}

Q5: Write a Java Program to reverse the letters present in the given String.
Ans:
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of the string: " + reverse);
}
}

5
Q6: Write a program in Java for Diamond Pattern.
Ans:
import java.util.Scanner;
class Diamond
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j <= n; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

6
Q7. Write a Java Program to check whether the given array is Mirror Inverse or not.
Ans:
class MirrorInverse
{
static boolean isMirrorInverse(int arr[])
{
for (int i = 0; i<arr.length; i++)
{
if (arr[arr[i]] != i)
return false;
}
return true;
}
public static void main(String[] args)
{
int arr[] = {3,4,2,0,1};
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
}

Q8: Write a Java program to calculate Permutation and Combination of 2 numbers.


Ans:
import java.util.Scanner;
class percomb
{
public static int fact(int num)
{
int fact=1, i;
for(i=1; i<=num; i++)
{
fact = fact*i;
}
return fact;
}
public static void main(String args[])
{
int n, r;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Value of n : ");
n = scan.nextInt();
7
System.out.print("Enter Value of r : ");
r = scan.nextInt();

System.out.print("NCR = " +(fact(n)/(fact(n-r)*fact(r))));


System.out.print("nNPR = " +(fact(n)/(fact(n-r))));
}
}

8
Lab Exercise – 3

Q1: Test Inheritance


Ans:
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
class Child extends Mother
{

}
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}
}

Q2: Test Overriding


Ans:
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
class Child extends Mother
{
void show()
{
System.out.println("I am Child");
}
}
9
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}

Q3: Test Polymorphism


Ans:
class Child extends Mother
{
void show()
{
System.out.println("I am Child");
}
}
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
public class Main
{
public static void main (String args[]){
Mother m1 = new Child();
m1.show();
}
}

Q4: Parameterized Constructor


Ans:
class One
{
int x;
public One(int a)
{
this.x = a;
}
void show()
10
{
System.out.println(x);
}
}
class Two extends One
{
public Two()
{
super(10);
}
}
public class Main
{
public static void main (String args[]){
One o = new One(3);
o.show();
Two t = new Two();
t.show();
}
}

11
Lab Exercise – 4

Q1: Using polymorphism


Ans:
package animals;
public class Animals
{
private String name,voice;
Animals(String name,String voice)
{
this.name = name; this.voice = voice;
}
protected void makeVoice()
{
System.out.println(this.name + " " + this.voice);
}
}
public class Voice
{
private Animals []animals = new Animals[5];
protected void prepareVoice()
{
animals[0] = new Animals("Cow","Moo");
animals[1] = new Animals("Dog","Bark");
animals[2] = new Animals("Pig","Oink");
animals[3] = new Animals("Goat","Bleat");
animals[4] = new Animals("Lion","Roar");
}
protected void hear()
{
for(int i=0;i<5;i++)
{
animals[i].makeVoice();
}
}
}
public class Test
{
public static void main(String[] args)
{
Voice voice = new Voice();
voice.prepareVoice();
voice.hear();
}
}

12
Q2: Writing Beautiful Code
Ans:
package animals;
public class Animals
{
private String name,voice;
Animals(String name,String voice)
{
this.name = name; this.voice = voice;
}
protected void makeVoice()
{
System.out.println(this.name + " " + this.voice);
}
}
public class Voice
{
private Animals []animals = new Animals[5];
protected void prepareVoice()
{
animals[0] = new Animals("Cow","Moo");
animals[1] = new Animals("Dog","Bark");
animals[2] = new Animals("Pig","Oink");
animals[3] = new Animals("Goat","Bleat");
animals[4] = new Animals("Lion","Roar");
}
protected void hear()
{
prepareVoice();
for(int i=0;i<5;i++)
{
animals[i].makeVoice();
}
}
}
public class Voice2
{
public static void main(String[] args)
{
Voice voice = new Voice();
voice.hear();
}
}

13
Q3:
Ans:
package com.juet;
public class Pack1
{
protected void display()
{
System.out.println("Inside package com.juet and class Pack1");
}
}
package com.jiet; import com.juet.Pack1;
public class Pack2
{ public static void main(String[] args) {
Pack1 pack = new Pack1();
pack.display();
}
}

Error

Correct:
package com.jiet;
import com.juet.Pack1;
public class Pack2 extends Pack1
{
public static void main(String[] args)
{
Pack2 pack = new Pack2();
pack.display();
}
}
To access the protected member of Pack1 we need make Pack2 as sub child of Pack1. Now through
object of Pack2 we can access display method.

14
Lab Exercise – 5

Q1. Write the missing code in the following class definitions. Write a simple client for testing.
Ans:
public class Class_1
{
private int x;
private int y;
public Class_1()
{
x = 0;
y = 0;
}

public Class_1(int x1, int y1)


{
x = x1;
y = y1;
}

public void print()


{
System.out.print(x + " " + y + " ");
}

public String toString()


{
return x + " " + y + " ";
}

public void set(int x1, int y1)


{
x = x1;
y = y1;
}
}

public class Class_2 extends Class_1


{
private int z;
public Class_2()
{
super();
this.z = 0;
}
public Class_2(int x1, int y1, int z1)
15
{
super(x1,y1);
this.z = z1;
}

public void print() {


super.print();
System.out.println(z+" ");
}
public String toString() {
return z + " ";
}
public void set(int x1, int y1, int z1) {
super.set(x1,y1);
this.z = z1;
}
}

public class Main


{
public static void main(String[] args)
{
Class_2 cl1 = new Class_2();
Class_1 cl2 = new Class_2(10,20,30);
cl1.print();
cl2.print();
cl2.set(100,200,300);
cl2.print();
}
}

16
Lab Exercise – 6
Q1. Write a program that demonstrates the use of static keywords in outer class, inner class
and methods.
Ans:
import java.util.*;
class Class2
{
public static void fun2()
{
System.out.println("Outer Class static");
}
static class Class1{
public static void fun1()
{
System.out.println("Inner class static");
}
}
}
class L6_1
{
public static void main(String args[])
{
Class2 cl2=new Class2();
Class2.Class1 cl1 = new Class2.Class1();
cl2.fun2();
cl1.fun1();
}
}

Q2. Write a program which demonstrates the use of dynamic binding,


Ans:
import java.util.*;
class Class1{
void fun1()
{
System.out.println("Mother class");
}
}
class Class2 extends Class1{
void fun1()
{
System.out.println("Child class");
}
}
class L6_2
{
public static void main(String args[])
17
{
Class1 cl1=new Class2();
cl1.fun1();
}
}

Q3. Write a program which demonstrates the use of method overriding and method overloading.
Ans:
import java.util.*;
class L6_3
{
void fun1()
{
System.out.println("Mother class_Overriding");
}
void fun2(int a)
{
System.out.println("Overloading with parameter func");
}
void fun2()
{
System.out.println("Overloading without parameter func");
}
}

class Class2 extends Class1


{
void fun1()
{
System.out.println("Child class_Overriding");
}
}

class L6_3
{
public static void main(String args[])
{
Class1 cl1 = new Class1();
cl1.fun1();
cl1.fun2(5);
cl1.fun2();
}
}

Q4. Write a program that implements method hiding. How is it different from method overriding.
Ans:
import java.util.*;
class Class1
{
public static void fun1()
{
18
System.out.println("Mother class");
}
}
class Class2 extends Class1
{
public static void fun1()
{
System.out.println("Child class");
}
}
Class L6_4
{
public static void main(String args[])
{
Class1 obj=new Class1();
Class1 obj1=new Class2();
obj.fun1();
obj1.fun1();
}
}

Q6. WAP which demonstrates the use of access specifiers in Java.


Ans:
import java.util.*;
class Class1
{
void call()
{
fun2();
}
void fun()
{
System.out.println("Access Specifier : Default");
}
public void fun1()
{
System.out.println("Access Specifier : Public");
}
private void fun2()
{
System.out.println("Access Specifier : Private");
}
protected void fun3()
{
System.out.println("Access Specifier : Protected");
}
}

class L6_6
{
public static void main(String args[])
19
{
Class1 cl1=new Class1();
cl1.fun();
cl1.fun1();
cl1.call();
cl1.fun3();
}
}

Q7. WAP which demonstrates the use of instances and static initialization blocks.
Ans:
import java.util.*;
class Class1pai
{
public static void fun1()
{
System.out.println("Mother class");
}
}

class Class2 extends Class1


{
public static void fun1()
{
System.out.println("Child class");
}
}

class L6_7
{
public static void main(String args[])
{
Class1 obj = new Class1();
Class1 obj1 = new Class2();
obj.fun1();
obj1.fun1();
}
}

20
Lab Exercise – 7
Q1. Create an abstract class 'Parent' with a method 'message'. It has two subclasses each
having a method with the same name 'message' that prints "This is first subclass" and "This is
second subclass" respectively. Call the methods 'message' by creating an object for each
subclass.
Ans:
abstract class Parent{
void message(){

}
}
class first extends Parent{
void message(){
System.out.println("This is First Subclass");
}
}
class Second extends Parent{
void message(){
System.out.println("This is Second Subclass");
}
}
class L_7{
public static void main(String[] args) {
Parent first = new first();
Parent second = new Second();
first.message();
second.message();
}
}

Q2. Create an abstract class 'Bank' with an abstract method 'getBalance'. Rs 100, Rs 150 and
Rs 200 are deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are
subclasses of class 'Bank', each having a method named 'getBalance'. Call this method by
creating an object of each of the three classes.
Ans:
abstract class Bank{
abstract void getBalance();
}
class Bank1 extends Bank{
void getBalance(){
System.out.println("Rs. 100");
}
}
class Bank2 extends Bank{
void getBalance(){
21
System.out.println("Rs. 150");
}
}
class Bank3 extends Bank{
void getBalance(){
System.out.println("Rs. 200");
}
}
class L_7{
public static void main(String[] args) {
Bank Bank1 = new Bank1();
Bank Bank2 = new Bank2();
Bank Bank3 = new Bank3();

Bank1.getBalance();
Bank2.getBalance();
Bank3.getBalance();

}
}

Q3. We have to calculate the percentage of marks obtained in three subjects (each out of 100)
by student A and in four subjects (each out of 100) by student B. Create an abstract class
'Marks' with an abstract method 'getPercentage'. It is inherited by two other classes 'A' and
'B' each having a method with the same name which returns the percentage of the students.
The constructor of student A takes the marks in three subjects as its parameters and the
marks in four subjects as its parameters for student B. Create an object for each of the two
classes and print the percentage of marks for both the students.
Ans:
abstract class Marks
{
abstract void getpercentage(int a, int b,int c);
abstract void getpercentage(int a, int b,int c,int d);
}
class A extends Marks
{
void getpercentage(int a, int b,int c)
{
int p=((a+b+c)/300)*100;
System.out.println("Student A got"+p+"Percentage");
}
}
class B extends Marks
{
void getpercentage(int a, int b,int c,int d)
{
int p=((a+b+c+d)/400)*100;
22
System.out.println("Student B got"+p+"Percentage");
}
}
class L_7
{
public static void main(String[] args)
{
Marks A =new A();
Marks B= new B();
A.getpercentage(70, 50, 80);
B.getpercentage(55, 78, 65, 96);
}
}

23
Lab Exercise – 8
Main.java

import java.util.Random;
public class Main{
public static void main(String []args){
Llists E1 = new Llists();
Llists E2 = new Llists();
Llists E3 = new Llists();
Llists E4 = new Llists();
Double prvs1 = 0.0,prvs2 = 0.0,prvs3 = 0.0,prvs4 = 0.0;
for(int i=0;i<7;i++){
Random rand = new Random();
Double time1 = rand.nextDouble();
Double time2 = rand.nextDouble();
Double time3 = rand.nextDouble();
Double time4 = rand.nextDouble();
prvs1 += time1;
prvs2 += time2;
prvs3 += time3;
prvs4 += time4;
E1.insert(prvs1,"E1");
E2.insert(prvs2,"E2");
E3.insert(prvs3,"E3");
E4.insert(prvs4,"E4");
}
Events a = new Mergell().merge(E1.head,E2.head);
Events b = new Mergell().merge(E3.head,E4.head);
Events fn = new Mergell().merge(a,b);
Events tmp = fn;
while(fn!=null){
System.out.println("Time = " + fn.time + " Events Type = " + fn.eventType);
fn = fn.next;
}
}
}

Events.java

public class Events{


double time;
String eventType;
Events next;
Events(double time,String eventType){
this.time = time;
this.eventType = eventType;
24
this.next = null;
}
}

Lists.java

public class Llists{


public Events head;
Llists(){
this.head = null;
}
void insert(double time,String eventType){
Events newNode = new Events(time,eventType);
if(head==null){
head = newNode;
return;
}
Events temp = head;
while(temp.next!=null){
temp = temp.next;
}
temp.next = newNode;
}
Events get(){
return this.head;
}
void print(){
Events temp = head;
while(temp!=null){
System.out.println("Time = " + temp.time + " Events Type = " + temp.eventType);
temp = temp.next;
}
}
}

Mergell.java

public class Mergell{


Events merge(Events headA, Events headB)
{
Events dummyNode = new Events(0.0,"");
Events tail = dummyNode;
while(true)
{
if(headA == null)
{
tail.next = headB;
25
break;
}
if(headB == null)
{
tail.next = headA;
break;
}
if(headA.time <= headB.time)
{
tail.next = headA;
headA = headA.next;
}
else
{
tail.next = headB;
headB = headB.next;
}
tail = tail.next;
}
return dummyNode.next;
}
}

26
Lab Exercise – 9
Q1. Make a class University. This class has a method ‘exam( )’. exam( ) returns an exception
‘ExamSuspendException’ if stranger things happen in the University. You can implement
this by having a Boolean variable (say boolean strangerThings) set to true or false. Create
another class ‘JUET’. JUET has a method named ‘t1( )’. t1( ) makes a call to exam( ) along
with other normal statements. Make a class ExamSuspendException and declare it
extending the class Exception. Override its printStackTrace( ) method. Make a Test class
and test your code with four possible scenarios:

a) t1( ) handles ExamSuspendException using try-catch

class University
{
boolean strangerThings = false;
void exam(boolean strangerThings) throws ExamSuspendException
{
if (strangerThings)
{
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1()
{
try
{
exam(true);
}
catch (ExamSuspendException e)
{
e.printStackTrace();
}
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace()
{
System.out.println("Exam Suspended");
}
}
public class Fa
{
public static void main(String[] args)
{
27
JUET juet = new JUET();
juet.t1();
}
}

b) t1( ) ducks Exception

class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fb
{
public static void main(String[] args) {
JUET juet = new JUET();
juet.t1();
}
}

c) main ( ) handles Exception using try-catch

class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
28
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fc
{
public static void main(String[] args) {
JUET juet = new JUET();
try {
juet.t1();
} catch (ExamSuspendException e) {
e.printStackTrace();
}
}
}

d) main ( ) ducks Exception

class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fd
{
public static void main(String[] args) throws ExamSuspendException {
JUET juet = new JUET();
juet.t1();
29
}
}

Q2. Exceptions are polymorphic Create Exception classes for Cloth, Pant, Shirt and Tshirt.
Override printStackTrace ( ). Create a class called ‘LaundryTest’. This class has a method
called unift ( ) which is a risky function and throws PantException, ShirtException,
TshirtException or ClothException, if there is an issue in respective garment type.
LaundryTest also has a main method, which calls unfit( ). Do the following one-by-one and
observe the output:

a) Create multiple catch statements, in order Tshirt, Shirt, Pant, Cloth, in the main to
handle different exceptions. The last statement inside the main method should be
System.out.println(“Last line of main”);

import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTesta {
void unfit(String type) throws TshirtException, ShirtException, PantException, ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
30
}
public static void main(String[] args) {
LaundryTesta laundryTest = new LaundryTesta();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) {
e.printStackTrace();

} catch (PantException e) {
e.printStackTrace();
} catch (ClothException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}
b) Make a catch block return nothing. Add finally block after all catch blocks. Write
something dummy inside finally as well.

import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
31
class LaundryTestb {
void unfit(String type) throws TshirtException, ShirtException, PantException,
ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestb laundryTest = new LaundryTestb();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (TshirtException e) {
} catch (ShirtException e) {
} catch (PantException e) {
} catch (ClothException e) {
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}

c) Change the order of catch blocks like ClothException, TshirtException,


ShirtException, PantException

import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
32
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTestc {
void unfit(String type) throws TshirtException, ShirtException, PantException,
ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestc laundryTest = new LaundryTestc();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (ClothException e) {
e.printStackTrace();
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) {
e.printStackTrace();
} catch (PantException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}

33

You might also like