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

Java Programming Assignment Overview

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)
11 views4 pages

Java Programming Assignment Overview

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

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
{
[Link]();
}
catch ( ArithmeticException oops )
{
[Link]();
}

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()
{
[Link]("Result: " + 12/4 );
}

public void methodB()


{
[Link]("Result: " + 12/3 );
}

public void methodC()


{
[Link]("Result: " + 12/0 );
}
}

public class TestTrace


{

public static void main ( String[] args )


{
Divider dvdr = new Divider();

try
{
[Link]( );
[Link]( );
[Link]( );
}

catch ( ArithmeticException oops )


{
[Link]();
}

Run the program and observe the stack trace.

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

Common questions

Powered by AI

Customized exceptions in Java allow developers to create specific exception types that can give detailed information about particular error scenarios, beyond what standard Java exceptions provide. According to the document, creating customized exceptions involves defining new exception classes that can carry more specific context about the error situation. These exceptions offer better clarity during debugging and can be fine-tuned to the application's domain, providing precise error handling capabilities and improving error resilience by enabling custom logic when such exceptions are caught .

In the document, polymorphism is demonstrated through inheritance and the overriding of the performance() method in the 'windows' class, which extends the 'os' class. This allows the 'performance()' method to behave differently based on the actual object type that invokes it at runtime. The 'windows' class provides a specific implementation of the 'performance()' method while maintaining the interface defined by the 'os' class, showcasing runtime polymorphism .

The document describes an abstract class 'employee' with a method 'getamount()' which is used to display the amount paid to an employee. This class is then reused to calculate payment for different types of employees, such as weekly and hourly employees, by creating concrete subclasses that implement specific payment calculation algorithms based on their respective parameters like hours worked and weeks worked. This allows modeling of real-world employee payment systems using abstraction to encapsulate the shared functionality while letting subclasses add specific details .

The Unreachable Catch Block error occurs when a catch block is designed to catch an exception that can never be thrown by the try block. To avoid this, ensure that exceptions caught by a catch block are actually thrown by the code inside the try block. Avoid writing multiple catch blocks for exceptions where a more general superclass exception, like Exception, is already being caught unless specific handling is required for subclasses .

The program in the document uses exception handling to manage division operations safely when users provide inputs. By using a try-catch block, the program can handle cases of division by zero and invalid input formats without crashing. When a user enters data that could cause an exception, the catch block provides an informative error message and allows the program to continue running, enhancing robustness by preventing termination due to runtime errors and providing feedback for corrective user actions .

Exception chaining in Java refers to the technique of relating one exception to another by embedding the original cause into a higher-level exception. This helps retain the context of errors across several layers of method calls. The document hints at exception chaining during the explanations of re-throwing through various methods where each catch block might perform logging or cleanup and then pass the exception upwards. Chaining preserves the original exception details, aiding in comprehensive diagnostics and a deeper understanding of the error's origin when observed through stack traces. This allows developers to trace the error's propagation path more effectively .

The document encourages creating a package comprising more than two classes, demonstrating the use of Java packages to modularize code. Packages facilitate organization by grouping related classes together, which helps manage a large codebase by providing a clear structure. It also aids in avoiding name conflicts and controlling access through encapsulation. Using packages improves maintainability and readability, and allows for distributing software in pieces, thus leading to ease of management and deployment of a modularized application .

The document describes an interface 'vehicle' which contains methods 'getcolor()', 'getnumber()', and 'getconsumption()'. By implementing this interface, a class can represent specific vehicle types, such as two-wheelers and four-wheelers, with actual details filled in to meet interface requirements. The benefit of this design is that it enforces a contract for any class modeling a vehicle to provide these methods, facilitating loose coupling and flexibility in handling various vehicle types with a consistent interface .

Re-throwing exceptions in Java allows an exception to be caught, processed, and then re-thrown to be handled further up the call stack. This is useful when a method needs to perform cleanup or logging and then delegate the responsibility of resolving the exception to a higher-level handler. The document provides an example where methods 'A', 'B', and 'C' use try-catch blocks within each to catch and then re-throw an exception up the call stack to manage it at a higher level. Re-throwing ensures that an exception is logged or acted upon at each level of its propagation while allowing centralized handling .

The document's testing class demonstrates that a stack trace in Java records only the methods that were actively executing at the time an exception was thrown, not a complete history of all method calls. This focus on active operations helps pinpoint the exact location where the exception occurred, facilitating debugging by providing a snapshot of the program's call sequence. Observing stack traces assists developers in identifying problematic code segments and understanding the sequence of method executions leading to the exception .

You might also like