Java (2023 24)
Java (2023 24)
2(a) What is compile time polymorphism and its importance? Use the
compile time polymorphism in the Java program to create the objects
Answer:-
Importance:
Example:-
Class Calculator {
Return a + b;
Return a + b + c;
Return a + b;
Answer :-
Example:-
Class MyClass {
Int value;
This.value = value;
// Copy constructor
This.value = other.value;
System.out.println(obj1.value); // Output: 10
System.out.println(obj2.value); // Output: 10
Answer:-
Example:-
Import javax.swing.JButton;
Import javax.swing.JFrame;
Import javax.swing.JPanel;
Import javax.swing.JLabel;
Import java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import java.awt.event.ActionListener;
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setSize(300, 200);
Frame.setLayout(new BorderLayout());
Button.addActionListener(new ActionListener() {
@Override
System.out.println(“Button clicked!”);
Frame.add(label, BorderLayout.SOUTH);
Frame.revalidate();
Frame.repaint();
});
Panel.add(button);
Frame.add(panel, BorderLayout.CENTER);
Frame.setVisible(true);
In this example:
This example demonstrates the basic usage of JButton and how to handle
button clicks using an ActionListener
(E) Write an AWT program to create a Frame and close the frame using event Delegation model.
Answer:-
Import java.awt.*;
Import java.awt.event.*;
Public class FrameClose extends Frame {
Public FrameClose() {
setSize(300, 200);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
});
New FrameClose();
(F) With the help of Java programs, explain the different use of 'super
keyword.
Answer:-
The super keyword in Java is used to refer to the superclass (parent class) of
the current class. It’s primarily used in three scenarios: accessing superclass
members (variables and methods), calling superclass constructors, and
resolving naming conflicts between superclass and subclass members.
String model;
Vehicle(String model) {
This.model = model;
String color;
This.color = color;
Void display() {
Car.display();
}
3. Resolving Naming Conflicts: super helps differentiate between
members of the superclass and subclass when they have the same
name, preventing ambiguity. This is particularly useful when a subclass
overrides a method from its superclass.
e.g:-
class A {
void display() {
System.out.println(“Class A display”);
}
}
Class B extends A {
Void display() {
System.out.println(“Class B display”);
}
Void show() {
Super.display(); // Calling superclass method
Display(); // Calling subclass method
}
}
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // false
equalsIgnoreCase(): Similar to equals(), but ignores case differences.
e.g:- String str1 = “Hello”;
String str3 = “hello”;
System.out.println(str1.equalsIgnoreCase(str3)); // true
compareTo(): Compares two strings lexicographically (based on Unicode values).
o It returns:0 if the strings are equal.
o A negative value if the first string is lexicographically less than the second.
o A positive value if the first string is lexicographically greater than the second.
e.g:- String str1 = “apple”;
String str2 = “banana”;
String str3 = “apple”;
System.out.println(str1.compareToIgnoreCase(str3)); // 0
regionMatches():Compares specific regions of two strings. It offers flexibility with case
sensitivity.
e.g:- String str1 = “Hello World”;
String str2 = “World”;
System.out.println(str1.startsWith(“Hello”)); // true
System.out.println(str1.endsWith(“World”)); // true
(H) Develop a java package named 'envelope package' with a class Even containing a static
method that checks whether the number is even or not and returns that information. Import this
package in an another class and used to check a number is Even or odd
Answer:-
Step 1: Create the Package and Class
File: TestEven.java
If (Even.isEven(num)) {
System.out.println(num + “ is Even”);
} else {
System.out.println(num + “ is Odd”);
}
}
}
(I) Create a user defined exception as ‘InvalidAgeException’. Write Java program that
takes a does a common line argument.Raise the Exception “InvalidAgeException’ if
age is less than 18
Answer:-
Step 1: Create the Custom Exception
// File: InvalidAgeException.java
Public class InvalidAgeException extends Exception {
Public InvalidAgeException(String message) {
Super(message);
}
}
Step 2: Main Program to Use the Exception
// File: AgeCheck.java
Public class AgeCheck {
Public static void main(String[] args) {
Try {
If (args.length == 0) {
System.out.println(“Please provide age as a command-line argument.”);
Return;
}
(J) What do youunderstand by thread? Describe the complete life cycle of a thread.
Answer:-
What is a Thread in Java?
A thread is a lightweight subprocess, the smallest unit of execution in a program. Java allows
concurrent execution of two or more threads, enabling multitasking. Each thread runs
independently but shares the same memory space of the process.
1. New
t.start();
3. Running
The scheduler picks it from the Runnable pool and executes it.
4. Blocked/Waiting
It cannot proceed until some other thread notifies or releases the resource.
5. Timed Waiting
Sleep(time)
Join(time)
Wait(time)
6. Terminated (Dead)
The thread has finished execution or has been stopped due to an exception.
e.g:-
class MyThread extends Thread {
public void run() {
System.out.println(“Thread is running...”);
}
}