Lab 7
Lab 7
Year 01 Semester 02
Lab Sheet 07
Perform all the tasks given below using NetBeans IDE. Observe the compiler and run time responses.
Lab07_Task01
Consider the following two classes and answer the questions given below.
Lab07_Task02
Consider the Account class given below. Write the main method in a different class to experiment with
some instances of the Account class.
public Account(int a)
{
bal=0.0;
accnum=a;
}
public void deposit(double sum)
{
if (sum>0)
bal+=sum;
else
System.out.println("Account.deposit(...):"+" cannot deposit negative amount."); }
public void withdraw(double sum)
{
if (sum>0)
bal-=sum;
else
System.out.println("Account.withdraw(...):"+" cannot withdraw negative amount.");
}
public double getBalance()
{
return bal;
}
public double getAccountNumber()
{
return accnum;
}
public String toString()
{
return "Acc " + accnum + ": " + "balance = " + bal;
}
public final void print()
{
//Don't override this,
//override the toString method
System.out.println( toString() );
}
}
Using the Account class as a base class, write two derived classes called SavingsAccount and
CurrentAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have
an interest variable and a method that adds interest to the account. A CurrentAccount object, in addition
to the attributes of an Account object, should have an overdraft limit variable. Ensure that you have
overridden methods of the Account class as necessary in both derived classes.
Now create a Bank class, an object which contains an array of Account objects. Accounts in the array
could be instances of the Account class, the SavingsAccount class, or the CurrentAccount class. Create
two test accounts of each type.
Write an update method in the bank class. It iterates through each account, updating it in the following
ways: Savings accounts get interested added (use the method created above) and CurrentAccounts get a
letter sent if they are in overdraft.
The Bank class requires methods for opening and closing accounts, and for paying a dividend into each
account.
Hints:
● Note that the balance of an account may only be modified through the deposit(double) and
withdraw(double) methods.
● The Account class should not need to be modified at all.
Lab07_Task03
Write a java program to implement inheritance and polymorphism with the following class
diagram.
Lab07_Task04
Given the following class:
Lab07_Task05
Compile and run the following Java programming code and observe the result.
(b) If statement 2 has the exception, (replace statement 2 with System.out.println (5/0);)
(d) If the exception occurs at the catch block, (remove comment //System.out.println(10/0);)
(i)
import java.io.*;
public class TestChecked1{
public static void main(String args[]){
PrintWriter pw=new PrintWriter("ABC.txt");
pw.println("Hello");
}
}
(ii)
import java.io.*;
public class TestChecked2 {
public static void main(String args[]) throws FileNotFoundException {
PrintWriter pw=new PrintWriter("ABC.txt");
pw.println("Hello");
}
}
(iii)
import java.io.*;
public class TestChecked3 {
public static void main(String args[]) throws FileNotFoundException{
PrintWriter pw=new PrintWriter("ABC.txt");
pw.println("Hello");
System.out.println(10/0);
}
}
Lab07_Task07
To print exception information to console, there are multiple methods. Compile and run the following Java
programming code and observe the result. Observe the corresponding output of each method. State what
method would be used by default exception handler internally to inform the exception to the console?
try {
int new_array[] = { 1 };
new_array[90] = 99;
} catch (ArithmeticException e) {
System.out.println("\n\tDivision by zero.");
} catch (ArrayIndexOutOfBoundsException e) {
(b) All exceptions in the try block are captured by catch block?
(c) If all exceptions are not captured, change the program to capture all exceptions.
Lab07_Task09
Compile and run the following Java programming code and observe the result.
try {
int new_array[] = { 1 };
new_array[90] = 99;
} catch (ArithmeticException e) {
System.out.println("\n\tDivision by zero.");
} catch (ArrayIndexOutOfBoundsException e) {
}
Lab07_Task11
Sometimes it will be necessary to define your own exception class that deals with specific conditions that
occur within your program. Such a class should be derived from the Exception class or one of its subclasses.
Create your own exception class called InvalidAgeException by extending Exception class as follows.
InvalidAgeException(String s){
super(s);
if(age<18)
else
System.out.println("welcome to vote");
try{
validate(8);
Based on your experience of using throw and throws keywords in question 3, question 4 and last
(a) Explain the purpose of throw, how and when do we need to use it
(b) Explain the purpose of throws, how and when do we need to use it