J2SE Lab Sessions – PG-DAC 2013
Use the below command to upload your assignments
scp -r dac02001_11 [email protected]:/course/submit/assignment3
dac02001_11 is a folder which contains the java source code files.
1. Write Programs which models a library which contains many kinds of items:
Printed: books, journals, magazines, and documents
Multimedia: CDs, and DVDs.
Every item in the library must have an ID number and title. Every printed item must have a number
of pages, and every multimedia item must have a length, in seconds.
Define the classes Item, Printed, and Multimedia, making sure they have the appropriate
relationships in the class hierarchy, and the appropriate private fields.
Create constructors for the three classes, using super() where necessary.
Create toString() methods for each of the three classes. For an item, the string should have its id
followed by its title. For printed and multimedia items, the id and title should be followed by the
number of pages or running length with the appropriate unit (pages or seconds).
For example(Sample Output) :
7985 Alice in Wonderland (105 pages)
3565 In a Sentimental Mood (597 seconds)
Remember that the id and title fields in the Item class are private, so you'll need to call the
toString() method belonging to the Item class for the other two toString() methods.
2. Write a program that calls a method to divide two integers entered from the keyboard and throws
an user defined exception of type ArithmeticException when trying to divide by zero.The program
should take the user input from the console and based on the condition should throw an user
defined exception which prints a message.
Sample Output :
Enter the first number:
10
Enter the second number:
0
Exception Occurred :Trying to divide 10/0 .Division by zero is not allowed
3. Implement a Class BankAccount with two members account_number and balance.It also has two
member methods createAccount(acc_num,bal) and withdraw(amount).
Implement an Exception class which handles the following exceptions.
MinimumBalanceExcpetion: If an account is started with an amount less than the minimum
balance,throw this exception.
InSufficientFundException: If a person tries to withdraw an amount more than his/her actual
balance, throw this exception.
Write a ExDemoClass with main() that tests the same.Include a finally block which prints “In
finally block”.
4. Write a superclass Worker with subclasses HourlyWorker and SalariedWorker.Every worker has a
name and a salary rate.An hourly worker gets paid the hourly wage for the actual number of hours
worked, if the hours is atmost 40.If the hourly woker worked more than 40 hours,the excess is paid at
time and a half.The salaried worker gets paid the hourly wage for 40 hours no matter what the actual
number of hours is.Write a static method computePay which takes a parameter as Worker and uses
polymorphism to compute the pay of any worker.Supply a test program that tests these classes and
methods.
5. Write a class that implements the CharSequence interface found in the java.lang package. Your
implementation should return the string backwards. Select any sentence. Write a small main method to
test your class.