Emailing JAVA_FILE_PART-II
Emailing JAVA_FILE_PART-II
import java.io.*;
public class Q11 {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("nonexistentfile.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException occurred: " + e.getMessage());
}
}
}
MyThread(String name) {
this.threadName = name;
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500); // Pause for 0.5 second
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " has finished execution.");
}
}
public class Q13 {
public static void main(String[] args) {
// Creating child threads
MyThread t1 = new MyThread("Thread-1");
MyThread t2 = new MyThread("Thread-2");
MyThread t3 = new MyThread("Thread-3");
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
int byteData;
while ((byteData = fis.read()) != -1) {
// Convert byte to character and print
System.out.print((char) byteData);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e);
} catch (IOException e) {
System.out.println("Error reading file: " + e);
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
import javax.swing.*;
import java.awt.event.*;
// Create a button
JButton button = new JButton("Click Me");
button.setBounds(100, 100, 100, 40);
// Create a label
JLabel label = new JLabel("Waiting for click...");
label.setBounds(90, 50, 200, 30);
frame.setSize(300, 250);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class employee{
String name;
int age;
String address;
void getdata(String name, int age,String address){
this.name=name;
this.age=age;
this.address=address; }
void showdata(){
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("Address: "+address); }
CJ.showdata(); }
}
import java.util.Scanner;
public class Q17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2, choice;
double result = 0;
System.out.print("Enter first number: ");
num1 = sc.nextInt()
System.out.print("Enter second number: ");
num2 = sc.nextInt();
System.out.println("\nChoose an operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.print("Enter your choice (1-4): ");
choice = sc.nextInt()
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("Result: " + result);
break;
sc.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Q18 {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
String line = "";
System.out.println("Enter text (type 'STOP' to end):");
try {
while (true) {
line = reader.readLine(); // Read a line from the keyboard
if (line.equalsIgnoreCase("STOP")) {
System.out.println("Program terminated.");
break;
}
System.out.println("You typed: " + line);
}
} catch (IOException e) {
System.out.println("An error occurred while reading input.");
e.printStackTrace(); }
}
}
// SavingsAccount Class
class SavingsAccount {
private String accountNumber;
private double balance;
// Deposit Amount
account.depositAmount(500.00);
System.out.println("Current Balance: " + account.getBalance());
// Withdraw Amount
try {
account.withdrawAmount(200.00);
System.out.println("Current Balance: " + account.getBalance());
import java.awt.*;
import javax.swing.*;
public RGBColorMixer() {
setTitle("RGB Color Mixer");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
import java.awt.event.*;
import javax.swing.*;
public SwingFormExample() {
setTitle("Simple Swing Form");
setSize(400, 400);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Name
nameLabel = new JLabel("Name:");
nameLabel.setBounds(30, 30, 80, 20);
nameField = new JTextField();
nameField.setBounds(120, 30, 200, 25);
// Hobbies
hobbyLabel = new JLabel("Hobbies:");
hobbyLabel.setBounds(30, 110, 80, 20);
readingBox = new JCheckBox("Reading");
codingBox = new JCheckBox("Coding");
musicBox = new JCheckBox("Music");
readingBox.setBounds(120, 110, 80, 20);
codingBox.setBounds(200, 110, 80, 20);
musicBox.setBounds(280, 110, 80, 20);
// Submit button
submitButton = new JButton("Submit");
submitButton.setBounds(150, 150, 100, 30);
submitButton.addActionListener(this);
// Add to frame
add(nameLabel);
add(nameField);
add(genderLabel);
add(maleButton);
add(femaleButton);
add(hobbyLabel);
add(readingBox);
add(codingBox);
add(musicBox);
add(submitButton);
add(outputArea);
setVisible(true);
}
import javax.swing.*;
import java.awt.*;
// Create components
JButton northButton = new JButton("North");
JButton southButton = new JButton("South");
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
JButton centerButton = new JButton("Center");
import java.sql.*;
public class q24 {
static final String JDBC_URL = "jdbc:mysql://localhost:3306/testdb";
static final String USER = "root"; // your DB username
static final String PASS = "3373"; // your DB password
pstmt.setString(1, name);
pstmt.setString(2, email);
pstmt.setString(1, newEmail);
pstmt.setInt(2, userId);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public q25() {
setTitle("User List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
model.addColumn("ID");
model.addColumn("Name");
model.addColumn("Email");
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("id"),
rs.getString("name"),
rs.getString("email")
});
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error: " + e.getMessage());
}
setVisible(true);
}