User Defined Exceptions-Learning Java Language-Assignment, Exercises of Java Programming

This assignment is for Learning Java Language course by Suryavanshi Vikram at Cochin University of Science and Technology. It includes: Class, Handle, Exception, Block, Function, Call, Initialize, Null, User, Defined

Typology: Exercises

2011/2012

Uploaded on 07/12/2012

yumna
yumna 🇮🇳

4.8

(4)

12 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT No: 02
Due Date: 16th March 2012
1. Create a class of Employee and prompt the user for time. If time is later than 9 am then
throw a user defined”TooLate Exception” displaying a message that you are too late.
2. Handle above exception in a try catch block by calling a function handleException. At
the end display “Program Ended “whether exception is thrown or not.
3. Define an object and initialize it to null. Try to call a method through this reference. Now
wrap the code in a try-catch clause to catch the exception
4. Write a program which throws an ArrayOutOfBoundsException and catch specified
exception in catch block.
5. Write a program which calls func1 of class A and the func1 calls func2 of class B. func2
throws a user defined exception. Your task is to handle this exception in the main
method.
6. Create an interface RemoteControlwhich has functions :
public interface RemoteControl {
booleanpowerOnOff(); // Returns new state, on = true
intvolumeUp(int increment); // Returns new volume level
intvolumeDown(int decrement); // Returns new volume level
void mute(); // Mutes sound output
intsetChannel(int channel); // Set the channel number and return it
intchannelUp(); // Returns new channel number
intchannelDown(); // Returns new channel number
}
Write a class TV that inherits Interface RemoteControl. Provide proper implementation of
interface. You may add following datamembers of TV to provide implementation for methods:
private String make = null;
private intscreensize = 0;
private boolean power = false;
private int MIN_VOLUME = 0;
private int MAX_VOLUME = 100;
private int volume = MIN_VOLUME;
private int MIN_CHANNEL = 0;
private int MAX_CHANNEL = 999;
private int channel = 0;
Docsity.com
pf2

Partial preview of the text

Download User Defined Exceptions-Learning Java Language-Assignment and more Exercises Java Programming in PDF only on Docsity!

ASSIGNMENT No: 02

Due Date: 16

th

March 2012

  1. Create a class of Employee and prompt the user for time. If time is later than 9 am then throw a user defined”TooLate Exception” displaying a message that you are too late.
  2. Handle above exception in a try catch block by calling a function handleException. At the end display “Program Ended “whether exception is thrown or not.
  3. Define an object and initialize it to null. Try to call a method through this reference. Now wrap the code in a try-catch clause to catch the exception
  4. Write a program which throws an ArrayOutOfBoundsException and catch specified exception in catch block.
  5. Write a program which calls func1 of class A and the func1 calls func2 of class B. func throws a user defined exception. Your task is to handle this exception in the main method.
  6. Create an interface RemoteControlwhich has functions : public interface RemoteControl { booleanpowerOnOff(); // Returns new state, on = true intvolumeUp(int increment); // Returns new volume level intvolumeDown(int decrement); // Returns new volume level void mute(); // Mutes sound output intsetChannel(int channel); // Set the channel number and return it intchannelUp(); // Returns new channel number intchannelDown(); // Returns new channel number } Write a class TV that inherits Interface RemoteControl. Provide proper implementation of interface. You may add following datamembers of TV to provide implementation for methods:  private String make = null;  private intscreensize = 0;  private boolean power = false;  private int MIN_VOLUME = 0;  private int MAX_VOLUME = 100;  private int volume = MIN_VOLUME;  private int MIN_CHANNEL = 0;  private int MAX_CHANNEL = 999;  private int channel = 0;

Docsity.com

  1. Create three new types of exceptions. Write a class with a method that throws all three. In main, call the method but only use a single catch clause that will catch all three types of exceptions. Modify the program by adding a finally clause. Verify that your finally clause is executed, even if a NullPointerException is thrown.
  2. Create a class with two Integer attributes. Create a method to swap the attributes' values. Throw an exception of a new type that you define at different places within the method. The catch block has restored the attributes' values as held before calling the method. Test your code in main method.
  3. Adapt the class of the previous exercise to swap two arrays of dimension four. Test your code in main method.
  4. Read name of students from the user and write it in the file with number of the student and name of the institution appended at the end. 1

Docsity.com