Java 4 Marks SOlution
Java 4 Marks SOlution
Exception handling in Java is a powerful mechanism to handle runtime errors, maintaining the
normal flow of the application.
Q.6
Q.7
1. The validateAge method checks if the given age is less than 18.
2. If the condition is true (i.e., the age is less than 18), the method uses the
throw keyword to explicitly throw an IllegalArgumentException with a
custom error message.
3. The exception is caught in the catch block of the main method, where the
exception message is printed.
Q.8
Java provides the Thread class’s setPriority() method to set the priority
of a thread. The priority values range from Thread.MIN_PRIORITY (1)
to Thread.MAX_PRIORITY (10), with the default being
Thread.NORM_PRIORITY (5).
System.out.println(Thread.currentThread().getPriority());
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.NORM_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
Q.9
The primary purpose of the Runnable interface is to define the task that is executed by a
thread. You implement the Runnable interface when you want to run a piece of code in a
separate thread.
void run();
o The run() method contains the code you want the thread to execute.
o The method doesn't return anything and doesn't throw any checked exceptions.
Once you implement the Runnable interface, you can pass it to a Thread object, which
will execute the run() method in a new thread.
Explanation:
Q.10
Q.11
Q.13
In Java AWT (Abstract Window Toolkit), the TextField class is a single-line text
input field where users can enter text.
Constructors
TextField()
This constructor creates an empty TextField object with no initial text.
TextField(String text)
Methods
1. setText(String text)
This method sets the text content of the TextField. It replaces any existing
text with the new one.
textField.setText("Hello, World!");
2. getText()
This method retrieves the current text from the TextField.
3. setColumns(int columns)
This method sets the number of columns (width) of the TextField. The
number of columns defines how wide the field is.
5. setEditable(boolean editable)
This method enables or disables editing in the TextField. If set to false, the
user cannot change the text.
6. setEchoChar(char echoChar):
7. getEchoChar()
This method returns the character that is used as the echo character when
typing in the TextField.
Q.14
1. Create a Panel: You can create a Panel object either by using the default
constructor or by specifying a layout manager.
2. Add Components to the Panel: After creating the Panel, you can add
various components like buttons, text fields, labels, etc., to it.
3. Add the Panel to a Frame: Finally, the Panel is added to a Frame or other
containers.
Here’s a simple example to demonstrate how to create and use a Panel in Java:
import java.awt.*;
import java.awt.event.*;
panel.add(button1);
panel.add(button2);
panel.add(textField);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
Q.15
import javax.swing.*;
import java.awt.*;
frame.setLayout(new BorderLayout());
frame.add(new JButton("North Button"), BorderLayout.NORTH);
frame.add(new JButton("South Button"), BorderLayout.SOUTH);
frame.add(new JButton("East Button"), BorderLayout.EAST);
frame.add(new JButton("West Button"), BorderLayout.WEST);
frame.add(new JButton("Center Button"), BorderLayout.CENTER);
frame.setVisible(true);
}
}
Explanation:
Q.18
Q.19
1. Import Necessary Packages: Ensure you import the Swing components and
layout managers you'll use.
import javax.swing.*;
import java.awt.*;
2. Create the JFrame: Initialize a JFrame to serve as the main window for
your application.
5. Add Tabs to the JTabbedPane: Add each panel to the JTabbedPane with a
title (and optionally, an icon).
6. Add the JTabbedPane to the JFrame: Set the layout of the frame and add
the JTabbedPane to it.
frame.setLayout(new BorderLayout());
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setVisible(true);
Complete Example:
import javax.swing.*;
import java.awt.*;
frame.setLayout(new BorderLayout());
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
Q.23
In Java, the ItemListener interface is part of the java.awt.event package and is used to
handle item events, which occur when the state of an item—such as a checkbox, radio button, or
item in a list—changes.
Implementing this interface allows developers to respond to user interactions with these
components.
This method is invoked whenever the state of an item changes. The ItemEvent parameter
provides information about the event, such as the source component and the state change.
Demo Program
import javax.swing.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.*;
checkBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
label.setText("Feature is enabled");
}
else
{
label.setText("Feature is disabled");
}
}
});
frame.add(checkBox);
frame.add(label);
frame.setVisible(true);
}
}
Q.24
The WindowEvent class represents low-level events that indicate changes in the
status of a window.
/* Demo Program */
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowListener()
System.out.println("Window Opened");
System.out.println("Window Closing");
frame.dispose();
{
System.out.println("Window Closed");
System.out.println("Window Minimized");
System.out.println("Window Restored");
System.out.println("Window Activated");
{
System.out.println("Window Deactivated");
});
frame.setVisible(true);