2nd Half Java File_merged (1)
2nd Half Java File_merged (1)
fruits.removeElement("Banana");
System.out.println("After removing 'Banana': " + fruits);
output:
17.Create a user defined exception named
“nomatchexception” that is fired when the string
entered by the user is not “india”.
try {
if (!input.equals("india")) {
throw new NoMatchException("Input does not match
'india'.");
} else {
System.out.println("Input matches 'india'.");
}
} catch (NoMatchException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
output:
18.Write a Java program to show even & odd numbers by thread.
even.start();
odd.start();
}
}
Output:
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100); // Red square
g.setColor(Color.GREEN);
g.fillOval(200, 50, 100, 100); // Green circle
g.setColor(Color.YELLOW);
g.fillRoundRect(100, 200, 150, 100, 30, 30); //
Yellow rounded rectangle
}
MovingBanner() {
setTitle("Moving Banner");
setSize(500, 300);
setBackground(Color.BLACK);
setForeground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Thread t = new Thread(this);
t.start();
}
Output:
22.Wri
te a
Java program to demonstrate the use of equals() and ==
in Java.
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
System.out.println(str3 == str4);
System.out.println(str3.equals(str4));
}
}
Output.
23.Write a Java program to implement all mouse events
and mouse motion events.
Sol. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
MouseEventsExample() {
label = new JLabel();
label.setBounds(20, 50, 300, 20);
add(label);
addMouseListener(this);
addMouseMotionListener(this);
setSize(400, 400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Output:
24.Write a Java program to implement keyboard events.
Sol. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
KeyboardEventsExample() {
label = new JLabel();
label.setBounds(20, 50, 300, 20);
add(label);
addKeyListener(this);
setSize(400, 400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFocusable(true);
}
public void keyTyped(KeyEvent e) {
label.setText("Key Typed: " + e.getKeyChar());
}
Output:
25.Write a Java program using AWT to create a simple
calculator.
Sol. import java.awt.*;
import java.awt.event.*;
SimpleCalculator() {
tf1 = new TextField();
tf1.setBounds(50, 50, 150, 20);
add(add);
add(sub);
add(mul);
add(div);
add(clear);
add(tf1);
add(tf2);
add(tf3);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clear.addActionListener(this);
setSize(400, 400);
setLayout(null);
setVisible(true);
setTitle("Simple Calculator");
}
public void actionPerformed(ActionEvent e) {
try {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
if (e.getSource() == add) {
tf3.setText(String.valueOf(n1 + n2));
} else if (e.getSource() == sub) {
tf3.setText(String.valueOf(n1 - n2));
} else if (e.getSource() == mul) {
tf3.setText(String.valueOf(n1 * n2));
} else if (e.getSource() == div) {
if (n2 != 0) {
tf3.setText(String.valueOf(n1 / n2));
} else {
tf3.setText("Divide by 0");
}
} else if (e.getSource() == clear) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
} catch (Exception ex) {
tf3.setText("Invalid");
}
}
LoginFormAWT() {
userLabel = new Label("Username:");
userLabel.setBounds(50, 50, 80, 20);
userText = new TextField();
userText.setBounds(150, 50, 150, 20);
add(userLabel);
add(userText);
add(passLabel);
add(passText);
add(hobbiesLabel);
add(hobby1);
add(hobby2);
add(hobby3);
add(genderLabel);
add(male);
add(female);
add(loginButton);
add(selectedHobbies);
setSize(400, 400);
setLayout(null);
setVisible(true);
setTitle("Login Form");
}
SwingLoginForm() {
userLabel = new JLabel("Username:");
userLabel.setBounds(50, 50, 80, 20);
userText = new JTextField();
userText.setBounds(150, 50, 150, 20);
add(userLabel);
add(userText);
add(passLabel);
add(passText);
add(hobbiesLabel);
add(hobby1);
add(hobby2);
add(hobby3);
add(loginButton);
add(selectedHobbies);
setSize(400, 400);
setLayout(null);
setVisible(true);
setTitle("Swing Login Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Output:
28.Write a Java program to show all layout managers. (4
layout
managers).
add(button1, BorderLayout.NORTH);
add(button2, BorderLayout.SOUTH);
add(button3, BorderLayout.EAST);
add(button4, BorderLayout.WEST);
add(button5, BorderLayout.CENTER);
// FlowLayout
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(new JButton("FlowLayout 1"));
flowPanel.add(new JButton("FlowLayout 2"));
flowPanel.add(new JButton("FlowLayout 3"));
flowPanel.add(new JButton("FlowLayout 4"));
// GridLayout
JPanel gridPanel = new JPanel(new GridLayout(2, 2));
gridPanel.add(new JButton("GridLayout 1"));
gridPanel.add(new JButton("GridLayout 2"));
gridPanel.add(new JButton("GridLayout 3"));
gridPanel.add(new JButton("GridLayout 4"));
// GridBagLayout
JPanel gridBagPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gridBagPanel.add(new JButton("GridBag 1"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gridBagPanel.add(new JButton("GridBag 2"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gridBagPanel.add(new JButton("GridBag 3"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gridBagPanel.add(new JButton("GridBag 4"), gbc);
AudioImageApp() {
audioButton = new JButton("Audio");
imageButton = new JButton("Image");
audioButton.addActionListener(this);
imageButton.addActionListener(this);
imageLabel = new JLabel();
imageLabel.setBounds(50, 100, 300, 300);
add(audioButton);
add(imageButton);
add(imageLabel);
Output.
30.Create a Java applet with three buttons ‘red’,’green’,’blue’.
Whenever user press any button the corresponding color
should be seen as background color in the window.
Sol.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public ColorSwingExample() {
setTitle("Color Change App");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
redButton.addActionListener(this);
greenButton.addActionListener(this);
blueButton.addActionListener(this);
setLayout(new FlowLayout());
add(redButton);
add(greenButton);
add(blueButton);
}
Output:
31.Write a Java program in Java to implement the concept
of ‘synchronization’ using thread.
CounterThread(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
}
}
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Sol.import java.sql.*;
Output.
33.Write a Java program in Java to create database table
using Java.
Sol.
import java.sql.*;
Output.
34.Write a Java program in Java to insert, update, delete & select
records.
Sol.
import java.sql.*;
import java.util.Scanner;
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Delete");
System.out.println("4. Select");
System.out.print("Choose option: ");
int choice = sc.nextInt();
sc.nextLine();
if (choice == 1) {
System.out.print("Enter id: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter name: ");
String name = sc.nextLine();
String sql = "INSERT INTO students (id, name) VALUES (" + id + ", '" +
name + "')";
stmt.executeUpdate(sql);
System.out.println("Record inserted.");
} else if (choice == 2) {
System.out.print("Enter id to update: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter new name: ");
String name = sc.nextLine();
String sql = "UPDATE students SET name = '" + name + "' WHERE id = "
+ id;
stmt.executeUpdate(sql);
System.out.println("Record updated.");
} else if (choice == 3) {
System.out.print("Enter id to delete: ");
int id = sc.nextInt();
String sql = "DELETE FROM students WHERE id = " + id;
stmt.executeUpdate(sql);
System.out.println("Record deleted.");
} else if (choice == 4) {
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " " + name);
}
} else {
System.out.println("Invalid option.");
}
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output.
35.Write Java program to read input from java console.
Sol.
import java.util.Scanner;
System.out.println("Hello " + name + ", you are " + age + " years
old.");
}
}
Output.
36.Write a Java program to implement file handling(both
reading & writing to a file.
Sol.
import java.io.*;
Output.
37.Write a Java program on anonymous classes.
Sol.
public class AnonymousClassExample {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
System.out.println("This is an anonymous class!");
}
};
Thread t = new Thread(r);
t.start();
}
}
Output.