0% found this document useful (0 votes)
41 views4 pages

Assignment 5

The document contains 10 questions about Java programming concepts like inheritance, polymorphism, interfaces, exceptions, packages, and exception handling. The questions involve writing Java code to demonstrate these concepts, like creating classes that extend and override methods, implementing interfaces, throwing and catching customized exceptions, and re-throwing exceptions.

Uploaded by

Ani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Assignment 5

The document contains 10 questions about Java programming concepts like inheritance, polymorphism, interfaces, exceptions, packages, and exception handling. The questions involve writing Java code to demonstrate these concepts, like creating classes that extend and override methods, implementing interfaces, throwing and catching customized exceptions, and re-throwing exceptions.

Uploaded by

Ani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment- 5

Q.1 Write a java program to create two classes os & windows such that windows class extends
os class & overrides its performance() method using runtime polymorphism.

Q.2 Create an abstract class employee with methods getamount() which displays the amount
paid to employee. Reuse this class to calculate the amount to be paid to weekly employee
and hourly employee according to no. of hours and total hours for hourly employee and no.
of weeks and total weeks for weekly employee.

Q.3 Create an interface vehicle with method getcolor(), getnumber(), getconsumption().


Calculate the fuel consumed by the particular model, name and color of two wheeler and
four wheeler by implementing interface vehicle.

Q.4 Write a program to create your own package. Package should have more than 2 classes.
Write a class that uses he package.

Q.5 Create customized exceptions in Java.

Q.6 Explain Unreachable Catch Block error with the help of Java Program.

Q.7 Write a java program to do the following:


Put in a loop so that the user is repeatedly asked for the numerator and the divisor. For each
set of data, the program prints out the result, or an informative error message if there is a
problem (division by zero or poor input data).
1. The program continues looping, even if there is a problem
2. Exit the loop when data entered for the numerator start with characters "q" or
"Q". Don't print out an error message in this case.
3. Don't ask for the divisor if the user just asked to quit.

Here is sample output from one run:

Enter the numerator: 12


Enter the divisor: 4
12 / 4 is 3

Enter the numerator: 12


Enter the divisor : 0
You can't divide 12 by 0

Enter the numerator: glarch


You entered bad data.
Please try again.

Enter the numerator: quit


You will need to use the method charAt() from the String class.

Q 8 Write a testing class TestTrace that contains the static main() method, and another
class CallEg that contains three methods. The main() method will create aCallEg object
and call its methodA().

class CallEg
{

public void methodA() throws ArithmeticException


{
}

public void methodB() throws ArithmeticException


{
}

public void methodC() throws ArithmeticException


{
}

public class TestTrace


{

public static void main ( String[] args )


{
CallEg eg = new CallEg(); // use default constructor
try
{
eg.methodA();
}
catch ( ArithmeticException oops )
{
oops.printStackTrace();
}

The catch{} block in main() prints a stack trace.


1. Put a statement in methodA() that divides by zero to create an ArithmeticException..
Observe the output.
2. Remove the division statement from methodA(). Change the code so
that methodA() calls methodB() which calls methodC(). Put a statement inmethodC() that
divides by zero to create an ArithmeticException.. Observe the output.
3. Add to the code so that methodA() calls methodB() inside a try{} block,
and methodB() calls methodC() inside a try{} block. In methodC() put the divide by zero
statement inside a try{} block. After each try{} block put a catch{} block which catches
the exception, prints a stack trace, and throws the exception object to its caller. Observe
the output.

Q.9 Create the following program. Run it and observe that the stack trace shows only those
methods that were active at the time of the exception. (In other words, the stack trace does
not show a complete history of the calls.)

class Divider
{
public void methodA()
{
System.out.println("Result: " + 12/4 );
}

public void methodB()


{
System.out.println("Result: " + 12/3 );
}

public void methodC()


{
System.out.println("Result: " + 12/0 );
}
}

public class TestTrace


{

public static void main ( String[] args )


{
Divider dvdr = new Divider();

try
{
dvdr.methodA( );
dvdr.methodB( );
dvdr.methodC( );
}

catch ( ArithmeticException oops )


{
oops.printStackTrace();
}

Run the program and observe the stack trace.

Q.10 What is Re-throwing an exception in Java? Explain it with Java Program.

You might also like