Java Practical File (11-25)
Java Practical File (11-25)
import java.util.Scanner;
public EventHandlingDemo()
{ setTitle("Event Handling Demo");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
setLayout(new FlowLayout());
add(label);
add(button);
setVisible(true);
}
int data;
while ((data = bis.read()) != -1)
{ System.out.print((char) data);
}
bis.close();
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
}
13. Write a program to demonstrate creation of multiple child threads.
class MyThread extends Thread { public void run() {
System.out.println(Thread.currentThread().getName() + " is running.");
}
}
int data;
while ((data = fr.read()) != -1)
{ System.out.print((char) data);
}
fr.close();
} catch (FileNotFoundException e) {
thread1.start();
thread2.start();
}
}
21. Write program that uses swings to display combination of RGB using 3 scrollbars
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
public RGBColorChooser()
{ setTitle("RGB Color Chooser");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
redScrollBar.addAdjustmentListener(new ScrollbarListener());
greenScrollBar.addAdjustmentListener(new ScrollbarListener());
blueScrollBar.addAdjustmentListener(new ScrollbarListener());
}
public SwingControlsDemo()
{ setTitle("Swing Controls Demo");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(northButton, BorderLayout.NORTH);
add(southButton, BorderLayout.SOUTH);
add(eastButton, BorderLayout.EAST);
add(westButton, BorderLayout.WEST);
add(centerButton, BorderLayout.CENTER);
}
public static void main(String[] args)
{ SwingUtilities.invokeLater(new Runnable() {
public void run() {
BorderLayoutDemo demo = new BorderLayoutDemo();
demo.setVisible(true);
}
});
}
}
19. Write a program declaring a Java class called SavingsAccount with members
``accountNumber`` and ``Balance``. Provide member functions as ``depositAmount ()`` and
``withdrawAmount ()``. If user tries to withdraw an amount greater than their balance then throw a
user-defined exception.
class InsufficientFundsException extends Exception
{ public InsufficientFundsException(String message) {
super(message);
}
}
public class SavingsAccount
{ private int accountNumber;
private double balance;
public SavingsAccount(int accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error reading the file: " + e.getMessage());
}
}
}
24. Write a java program to insert and update details data in the database.
25. Write a java program to retrieve data from database and display it on GUI.