Write a program to Demonstrate use of user defined Packages
Source Code:
// Define package
package mypackage;
public class MathOperations {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
}
}
Use of mypackage in another class
// Import the user-defined package
import [Link];
public class Main {
public static void main(String[] args) {
// Create an object of the MathOperations class
MathOperations math = new MathOperations();
// Call methods from the MathOperations class
int sum = [Link](10, 5);
int difference = [Link](10, 5);
// Display the results
[Link]("Sum: " + sum);
[Link]("Difference: " + difference);
}
Aim : Write a programs to demonstrate the use of Adapter Class in
Event Handling
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class MouseAdapterExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("MouseAdapter Example");
// Create a label
JLabel label = new JLabel();
[Link](100, 100, 200, 50);
[Link]("Mouse over me");
// Add MouseAdapter to handle mouse events
[Link](new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
[Link]("Mouse clicked!");
}
@Override
public void mouseEntered(MouseEvent e) {
[Link]("Mouse entered the label!");
}
@Override
public void mouseExited(MouseEvent e) {
[Link]("Mouse exited the label!");
}
});
// Add label to frame
[Link](label);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
Write a programs to demonstrate the use of Anonymous Inner
Class in Event Handling
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class AnonymousInnerClassKeyEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Anonymous Inner Class Key Event Example");
// Create a text field
JTextField textField = new JTextField();
[Link](100, 100, 200, 50);
// Add KeyListener using an anonymous inner class
[Link](new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
[Link]("Key pressed: " + [Link]());
}
});
// Add text field to frame
[Link](textField);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a program to raise built-in exceptions and raise them as per the
requirements.
Source Code:
public class ExceptionBuildIn {
public static void main(String[] args) {
try {
// Example of ArithmeticException
int result = 10 / 0;
} catch (ArithmeticException e) {
[Link]("ArithmeticException: " + [Link]());
}
try {
// Example of NullPointerException
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("NullPointerException: " + [Link]());
}
try {
// Example of ArrayIndexOutOfBoundsException
int[] arr = new int[5];
[Link](arr[10]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("ArrayIndexOutOfBoundsException: " + [Link]());
}
try {
// Example of IllegalArgumentException
throw new IllegalArgumentException("This is an illegal argument.");
} catch (IllegalArgumentException e) {
[Link]("IllegalArgumentException: " + [Link]());
}
}
}
Write a program to define user defined exceptions and raise them as per the
requirements.
Source Code:
// Custom Exception Class
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
// Main Class
public class UserDefinedException {
public static void main(String[] args) {
try {
validateAge(15);
} catch (InvalidAgeException e) {
[Link]("Exception caught: " + [Link]());
}
}
public static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or older.");
} else {
[Link]("Age is valid.");
}
}
}
Write a java application to demonstrate 5 bouncing balls of different colors
using
threads.
Source Code:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
class Ball {
private int x, y;
private int diameter;
private Color color;
private int xSpeed, ySpeed;
public Ball(int diameter, Color color) {
[Link] = diameter;
[Link] = color;
Random random = new Random();
this.x = [Link](800 - diameter);
this.y = [Link](600 - diameter);
[Link] = [Link](5) + 1;
[Link] = [Link](5) + 1;
}
public void update() {
x += xSpeed;
y += ySpeed;
if (x < 0 || x > 800 - diameter) {
xSpeed = -xSpeed;
}
if (y < 0 || y > 600 - diameter) {
ySpeed = -ySpeed;
}
}
public void draw(Graphics g) {
[Link](color);
[Link](x, y, diameter, diameter);
}
}
class BouncingBallsPanel extends JPanel implements ActionListener {
private Ball[] balls;
private Timer timer;
public BouncingBallsPanel() {
balls = new Ball[5];
Color[] colors = {[Link], [Link], [Link], [Link],
[Link]};
for (int i = 0; i < [Link]; i++) {
balls[i] = new Ball(30, colors[i]);
}
timer = new Timer(20, this);
[Link]();
}
public void paintComponent(Graphics g) {
[Link](g);
for (Ball ball : balls) {
[Link]();
[Link](g);
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
}
public class BouncingBalls {
public static void main(String[] args) {
JFrame frame = new JFrame("Bouncing Balls");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new BouncingBallsPanel());
[Link]();
[Link](true);
}
}
Write programs for the Flow Layout
Source Code:
import [Link].*;
import [Link].*;
public class FlowLayoutExample {
JFrame frame;
FlowLayoutExample() {
frame = new JFrame();
// Creating buttons
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
// Adding buttons to the frame
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
// Setting flow layout
[Link](new FlowLayout());
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
Write programs for the Grid Layout
Source code:
import [Link].*;
import [Link].*;
public class GridLayoutExample {
JFrame frame;
GridLayoutExample() {
frame = new JFrame();
// Creating buttons
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
// Adding buttons to the frame
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
// Setting grid layout with 3 rows and 3 columns
[Link](new GridLayout(3, 3));
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
}
Write programs for the Border Layout
Source code:
import [Link].*;
import [Link].*;
public class BorderLayoutExample {
JFrame frame;
BorderLayoutExample() {
frame = new JFrame();
// Creating buttons
JButton b1 = new JButton("North");
JButton b2 = new JButton("South");
JButton b3 = new JButton("East");
JButton b4 = new JButton("West");
JButton b5 = new JButton("Center");
// Adding buttons to the frame
[Link](b1, [Link]);
[Link](b2, [Link]);
[Link](b3, [Link]);
[Link](b4, [Link]);
[Link](b5, [Link]);
// Setting border layout
[Link](new BorderLayout());
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new BorderLayoutExample();
}
}
Create a swing application that randomly changes color on button click.
Source Code:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
public class RandomColorButton extends JFrame
{
private JButton button;
public RandomColorButton() {
setLayout(new FlowLayout());
button = new JButton("Click me!");
add(button);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeColor();
}
});
setSize(300, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void changeColor() {
Random random = new Random();
int red = [Link](256);
int green = [Link](256);
int blue = [Link](256);
Color color = new Color(red, green, blue);
[Link](color);
}
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new RandomColorButton();
}
});
}
}
Create a Swing application to demonstrate use of TextArea using scrollpane to
show contest of text file in textarea selected using file chooser.
Source Code:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class TextAreaApp extends JFrame {
private JTextArea textArea;
public TextAreaApp() {
setLayout(new BorderLayout());
JFileChooser fileChooser = new JFileChooser();
[Link](JFileChooser.FILES_ONLY);
JButton button = new JButton("Select File");
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = [Link]([Link]);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = [Link]();
try {
BufferedReader reader = new BufferedReader(new FileReader(selectedFile));
String line;
StringBuilder text = new StringBuilder();
while ((line = [Link]()) != null) {
[Link](line).append("\n");
}
[Link]();
[Link]([Link]());
} catch (IOException ex) {
[Link]();
}
}
}
});
add(button, [Link]);
textArea = new JTextArea(20, 40);
[Link](false);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, [Link]);
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new TextAreaApp();
}
});
}
}
Write a programs to demonstrate the Action Event
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class ActionEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("ActionEvent Example");
// Create a button
JButton button = new JButton("Click Me");
// Set button properties
[Link](100, 100, 150, 50);
// Add ActionListener to handle action event
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button was clicked!");
}
});
// Add button to frame
[Link](button);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a programs to demonstrate the Key Event
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class KeyEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("KeyEvent Example");
// Create a text field
JTextField textField = new JTextField();
// Set text field properties
[Link](100, 100, 200, 50);
// Add KeyListener to handle key events
[Link](new KeyAdapter() {
public void keyPressed(KeyEvent e) {
[Link]("Key pressed: " + [Link]());
}
});
// Add text field to frame
[Link](textField);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a programs to demonstrate the selection Event
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class ItemEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("ItemEvent Example");
// Create a combo box
String[] options = {"Option 1", "Option 2", "Option 3"};
JComboBox<String> comboBox = new JComboBox<>(options);
// Set combo box properties
[Link](100, 100, 150, 50);
// Add ItemListener to handle selection event
[Link](new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if ([Link]() == [Link]) {
[Link]("Selected: " + [Link]());
}
}
});
// Add combo box to frame
[Link](comboBox);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a programs to demonstrate the Mouse Event
Source Code:
// Import required packages
import [Link].*;
import [Link].*;
public class MouseEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("MouseEvent Example");
// Create a label
JLabel label = new JLabel();
[Link](100, 100, 200, 50);
[Link]("Mouse over me");
// Add MouseListener to handle mouse events
[Link](new MouseListener() {
public void mouseClicked(MouseEvent e) {
[Link]("Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse entered the label!");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse exited the label!");
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
// Add label to frame
[Link](label);
// Set frame properties
[Link](400, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a programs to demonstrate the Focus Event
Source code:
// Import required packages
import [Link].*;
import [Link].*;
public class FocusEventExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("FocusEvent Example");
// Create two text fields
JTextField textField1 = new JTextField("Click here first");
[Link](50, 100, 150, 50);
JTextField textField2 = new JTextField("Now click here");
[Link](250, 100, 150, 50);
// Add FocusListener to handle focus events
[Link](new FocusListener() {
public void focusGained(FocusEvent e) {
[Link]("TextField 1 gained focus");
}
public void focusLost(FocusEvent e) {
[Link]("TextField 1 lost focus");
}
});
[Link](new FocusListener() {
public void focusGained(FocusEvent e) {
[Link]("TextField 2 gained focus");
}
public void focusLost(FocusEvent e) {
[Link]("TextField 2 lost focus");
}
});
// Add text fields to frame
[Link](textField1);
[Link](textField2);
// Set frame properties
[Link](450, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Write a program to create a class and implement a default, overloaded and
copy Constructor.
Source Code:
import [Link].*;
class Employee
{
int e;
Employee()
{
e = 10;
}
Employee(int e)
{
this.e = e;
}
Employee(Employee p)
{
this.e = p.e;
}
public static void main(String args[])
{
Employee emp1 = new Employee();
Employee emp2 = new Employee(20);
Employee emp3 = new Employee(emp2);
[Link]("Default constructor value:"+emp1.e);
[Link]("Parameterised constructor value:"+emp2.e);
[Link]("Copy constructor value:"+emp3.e);
}
}
Write a program to create a class and implement the concepts of Method
Overloading.
Source Code:
import [Link].*;
class Square
{
void find_area(int a)
{
[Link]("Area is"+(a*a));
}
void find_perimeter(int a)
{
[Link]("Perimeter is "+(4*a));
}
public static void main (String[] args)
{
Square s = new Square();
s.find_area(4);
s.find_perimeter(5);
}
}
Write a program to create a class and implement the concepts of Static
methods.
Source Code:
import [Link].*;
class Maths
{
public static int add(int a,int b)
{
return a+b;
}
public static int multiply(int a,int b)
{
return a*b;
}
public static void main( String[] args)
{
[Link]([Link](2,3));
[Link]([Link](4,5));
}
}
Write a program to implement the concepts of Inheritance and Method
overriding.
Source Code:
import [Link].*;
class Animal
{
void sound()
{
[Link]("Animal makes a sound");
}
}
class Dog extends Animal
{
@Override
void sound()
{
[Link]("Dog barks");
}
}
public class AnimalClass
{
public static void main(String args[])
{
Animal animal = new Animal();
[Link]();
Dog dog = new Dog();
[Link]();
}
}
Write a program to implement the concepts of Abstract classes and methods.
Source Code:
import [Link].*;
abstract class animal{
abstract void eat();
void print()
{
[Link]("Animal");
}
}
class cat extends animal
{
void eat()
{
[Link]("Cat can eat veg or non veg");
}
void print_cat()
{
[Link]("Animal is cat");
}
}
class rat extends animal
{
void eat()
{
[Link]("Rat can eat tomato");
}
void print_rat()
{
[Link]("Animal is rat");
}
}
class dog extends animal
{
void eat()
{
[Link]("Dog can eat Bone");
}
void print_dog()
{
[Link]("Animal is dog");
}
}
class program_1
{
public static void main(String[]args)
{
cat c=new cat();
c.print_cat();
[Link]();
[Link]();
rat r=new rat();
r.print_rat();
[Link]();
[Link]();
dog d=new dog();
d.print_dog();
[Link]();
[Link]();
}
}