BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI
SOFTWARE DEVELOPMENT & EDUCATIONAL TECHNOLOGY UNIT
SDET C102, CORE JAVA
LAB-4 : 12/10/07

1.      Go through the following classes and follow the instructions given below:

class Hello{

String name;

private Hello( String name){

    this.name=name;

}

public void sayHello( ){

    System.out.println("hello "+name);

}

}

Save the above class to a file. Try to create an object of this class in another class say HelloDemo (contains main method) and invoke sayHello( ) methods on that object.

note: you are not permitted to add constructors or modify the existing constructors

hint: use static

2.       

class A{

            public static void main(String[] args){

                        int a,b;

                        try{

                                    a=0;b=1;

                                    System.out.println(b/a);

                                    //return;

                        }catch(Exception){

                                    System.out.println(“Exception caught”);

                                    //return;

                        }finally{

                                    System.out.println(“in Finally”)

                        }

                                    //System.out.println(“at the end”);

}

}

Save the above class to a file and compile it and check the output. Now uncomment the comments one by one and observe the outputs and understand all the cases.

3.      Write a class called BankAccount with the following properties and methods.

Properties: name, PIN, balance

methods: deposit(int amt), withdraw(int amt)

write another class called ATM with following methods and properties:

methods:deposit(Account acc, int amt), withdraw(Account acc, int amt) ,

transfer(Account acc1, Account acc2, int amt)

Assume that an account needs to have a minimum balance of 200. If an attempt is made to withdraw, which results in balance going below 200, throw an exception called MinimumBalanceException. For this you need to create your own exception class.

Note: To create an Exception class called MyFirstException

class MyFirstException extends Exception{

      /*I never thought it’s so simple*/

}

Use throw and throws wherever necessary.