0% found this document useful (0 votes)
9 views2 pages

Java Packages and Exception Handling Guide

This study guide covers three Java programs demonstrating key concepts: the use of packages and sub-packages, creating multiple classes within a package, and the functionality of the finally block in exception handling. The first program illustrates package structure and class importation, the second focuses on area calculations for shapes using classes, and the third emphasizes the execution of the finally block regardless of exceptions. Each program includes explanations of the concepts and code snippets.

Uploaded by

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

Java Packages and Exception Handling Guide

This study guide covers three Java programs demonstrating key concepts: the use of packages and sub-packages, creating multiple classes within a package, and the functionality of the finally block in exception handling. The first program illustrates package structure and class importation, the second focuses on area calculations for shapes using classes, and the third emphasizes the execution of the finally block regardless of exceptions. Each program includes explanations of the concepts and code snippets.

Uploaded by

v0975569
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programs Study Guide

Program 1: Packages & Sub-packages


// File: letmecalculate/Multiply/[Link]
package [Link];

public class Multiplication {


public int product(int a, int b) {
return a * b;
}
}

// File: [Link]
import [Link];

public class Test {


public static void main(String[] args) {
Multiplication m = new Multiplication();
int result = [Link](5, 4);
[Link]("Result: " + result);
}
}

Explanation: This program demonstrates the use of packages and sub-packages in Java. - `package` keyword
defines a package. - `import` keyword is used to access classes from another package. - Sub-packages are like
folders inside folders ([Link]).

Program 2: Packages (shapes)


// File: shapes/[Link]
package shapes;

public class Circle {


public void area(double radius) {
[Link]("Area of Circle: " + (3.14 * radius * radius));
}
}

// File: shapes/[Link]
package shapes;

public class Square {


public void area(double side) {
[Link]("Area of Square: " + (side * side));
}
}

// File: [Link]
import [Link];
import [Link];

class MainDemo {
public static void main(String[] args) {
Circle c = new Circle();
Square s = new Square();
[Link](5);
[Link](4);
}
}
Explanation: This program demonstrates creating multiple classes inside a package. - Packages improve reusability
and organization of code. - We can import individual classes (`import [Link]`) or all at once (`import shapes.*`).

Program 3: Finally Block in Exception Handling


class FinallyDemo {
public static void main(String[] args) {
try {
[Link]("Inside try block...");
int result = 10 / 0; // ArithmeticException
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
} finally {
[Link]("Finally block executed: Cleanup done.");
}
[Link]("Program continues after try-catch-finally.");
}
}

Explanation: The `finally` block in Java always executes, whether an exception occurs or not. It is mainly used for
cleanup activities like closing files, releasing resources, etc.

You might also like