CS3391-oops-Answer Key
CS3391-oops-Answer Key
• When the child class is derived from its parent class then the keyword extends is
used.
OR
3
OR
public class Test { 12.b java package and access modifier work together to control member visibility
public static void main(String[] args) { Java packages and access modifiers work together to define the visibility and
Outer.StaticNested obj = new Outer.StaticNested(); accessibility of classes, methods, variables, and other members within a program.
obj.display(); Packages: (8 marks)
}
Nested Classes
A nested class is any class defined within another class. There are two primary types:
Definition: Packages are namespaces that organize classes and interfaces into
Static Nested Classes (as described above)
manageable units.
Non-static Nested Classes (Inner Classes)
Purpose: Helps prevent naming conflicts and provides controlled access to classes
class Outer {
and their members.
private int num = 10;
Access Modifiers:
class Inner { Java provides four access modifiers to control the visibility of class members:
void display() { 1. Private: Accessible only within the class where it is defined.
System.out.println("Inner Class, num = " + 2. Default (Package-Private): Accessible within the same package
num);
3. Protected: Accessible within the same package and also in subclasses, even if they
} }}
are in different packages.
public class Test { 4. Public: Accessible from any other class in any package.
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display(); How Packages and Access Modifiers Work Together: (8 marks)
}}
Inner Classes (4 marks) 1. within the Same Package:
An inner class is a non-static nested class. Classes in the same package can access members with public, protected, and default
Purpose: (package-private) access.
Logical Grouping: Inner classes can logically group code that will be used only in private members are not accessible outside the defining class, even within the same
association with the outer class. package.
Encapsulation: Inner classes help hide implementation details from the outside 2. Across Different Packages:
world, Public Members: Accessible to all classes, regardless of the package.
class Outer Protected Members: Accessible in subclasses, even if they are in different
{ packages.
private String message = "Hello from Outer"; Default Members: Not accessible outside their own package.
class Inner { Private Members: Not accessible outside their defining class, regardless of the
void printMessage() { package.
System.out.println(message); Key Points:
} }} Default Access: Restricts visibility to the same package.
public class Test { Protected Access: Extends visibility to subclasses in other packages.
public static void main(String[] args) { Private Access: Restricts visibility to within the defining class.
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); Public Access: Grants visibility to all classes in all packages.
inner.printMessage();
}} 13.a Explain inter thread communication in java method
4
5. Avoid Deadlocks: Mismanagement of locks can lead to deadlocks, where threads are
stuck waiting indefinitely. (8 marks)
Methods for Inter-Thread Communication (8 marks) class SharedResource {
private boolean flag = false;
5
Output
6
push(T data): Adds an element to the stack.
pop(): Removes and returns the top element.
peek(): Returns the top element without removing it.
public InvalidAgeException(String message) {
super(message); isEmpty(): Checks if the stack is empty.
}}
public class UserDefinedExceptionDemo { size(): Returns the number of elements.
public static void validateAge(int age) throws
InvalidAgeException { clear(): Clears the stack.
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above
to register.");
} System.out.println("Registration successful!"); Testing: The main method demonstrates how to use the stack with integer data. (4 marks)
}
public static void main(String[] args) {
try {
validateAge(15);
} catch (InvalidAgeException e) {
System.out.println("Exception occurred: " +
e.getMessage()); import java.util.EmptyStackException;
} }} public class GenericStack<T> {
private Node<T> top; // Top of the stack
private int size; // Size of the stack
private static class Node<T> {
private T data;
private Node<T> next;
7
Key Methods of Scanner: (4 marks)
nextLine(): Reads a whole line (including spaces).
next(): Reads a single token (word).
}
nextInt(), nextDouble(), etc.: Reads specific data types.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
public class MouseEventExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Text text = new Text("Click or Move the Mouse");
text.setOnMouseClicked(event -> {
text.setText("Mouse Clicked at (" + event.getX() + ", " +
event.getY() + ")");
});
10