PROGRAM 1: Swing GUI - Accept & Display Student Details Q: Design GUI to accept student details (Roll, Name, Percentage) with Display and Clear buttons import javax.swing.*; import java.awt.event.*; class StudentForm extends JFrame implements ActionListener { JLabel l1, l2, l3; JTextField t1, t2, t3; JButton b1, b2; StudentForm() { l1 = new JLabel("Roll No:"); l2 = new JLabel("Name:"); l3 = new JLabel("Percentage:"); t1 = new JTextField(); t2 = new JTextField(); t3 = new JTextField(); b1 = new JButton("Display"); b2 = new JButton("Clear"); b1.addActionListener(this); b2.addActionListener(this); setLayout(null); l1.setBounds(50, 50, 100, 30); t1.setBounds(150, 50, 150, 30); l2.setBounds(50, 100, 100, 30); t2.setBounds(150, 100, 150, 30); l3.setBounds(50, 150, 100, 30); t3.setBounds(150, 150, 150, 30); b1.setBounds(50, 200, 100, 30); b2.setBounds(200, 200, 100, 30); add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(b1); add(b2); setSize(400, 300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource() == b1) { System.out.println("Roll No: " + t1.getText()); System.out.println("Name: " + t2.getText()); System.out.println("Percentage: " + t3.getText()); } if(e.getSource() == b2) { t1.setText(""); t2.setText(""); t3.setText(""); } } public static void main(String args[]) { new StudentForm(); } } PROGRAM 2: Change Label Color on Button Click Q: Write program to change Label text color to RED by clicking button import javax.swing.*; import java.awt.*; import java.awt.event.*; class ColorChange extends JFrame implements ActionListener { JLabel l; JButton b; ColorChange() { l = new JLabel("Hello Java"); b = new JButton("Change Color"); b.addActionListener(this); setLayout(new FlowLayout()); add(l); add(b); setSize(300, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { l.setForeground(Color.RED); } public static void main(String args[]) { new ColorChange(); } } PROGRAM 3: File Copy - Change Case of Alphabets Q: Copy content from one file to another, change case while copying import java.io.*; class FileCopyCase { public static void main(String args[]) { try { FileReader fr = new FileReader("input.txt"); FileWriter fw = new FileWriter("output.txt"); int ch; while((ch = fr.read()) != -1) { if(Character.isUpperCase((char)ch)) fw.write(Character.toLowerCase((char)ch)); else if(Character.isLowerCase((char)ch)) fw.write(Character.toUpperCase((char)ch)); else fw.write(ch); } fr.close(); fw.close(); System.out.println("File copied with case changed!"); } catch(Exception e) { System.out.println(e); } } } PROGRAM 4: File Copy - Replace Digits with * Q: Copy file content, replace all digits with * import java.io.*; class FileCopyDigit { public static void main(String args[]) { try { FileReader fr = new FileReader("input.txt"); FileWriter fw = new FileWriter("output.txt"); int ch; while((ch = fr.read()) != -1) { if(Character.isDigit((char)ch)) fw.write('*'); else fw.write(ch); } fr.close(); fw.close(); System.out.println("File copied with digits replaced!"); } catch(Exception e) { System.out.println(e); } } } PROGRAM 5: Interface Shape - Triangle Area Q: Define interface Shape with area(). Calculate area of Triangle import java.util.Scanner; interface Shape { void area(); } class Triangle implements Shape { double base, height; public void area() { Scanner sc = new Scanner(System.in); System.out.print("Enter base: "); base = sc.nextDouble(); System.out.print("Enter height: "); height = sc.nextDouble(); double a = 0.5 * base * height; System.out.println("Area of Triangle: " + a); } public static void main(String args[]) { Triangle t = new Triangle(); t.area(); } } PROGRAM 6: Abstract Class Shape - Circle Area Q: Define abstract class Shape with area(). Calculate area of Circle import java.util.Scanner; abstract class Shape1 { abstract void area(); } class Circle extends Shape1 { double radius; void area() { Scanner sc = new Scanner(System.in); System.out.print("Enter radius: "); radius = sc.nextDouble(); double a = 3.14 * radius * radius; System.out.println("Area of Circle: " + a); } public static void main(String args[]) { Circle c = new Circle(); c.area(); } } PROGRAM 7: User-Defined Exception - ZeroNumberExc Q: Create exception for zero. If zero throw exception, else calculate sum of first & last digit (use static) import java.util.Scanner; class ZeroNumberExc extends Exception { public String toString() { return "Number is zero"; } } class TestException { static void check(int n) throws ZeroNumberExc { if(n == 0) throw new ZeroNumberExc(); else { int first = n; while(first >= 10) first = first / 10; int last = n % 10; int sum = first + last; System.out.println("Sum of first & last digit: " + sum); } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter number: "); int num = sc.nextInt(); try { check(num); } catch(ZeroNumberExc e) { System.out.println(e); } } } PROGRAM 8: Inheritance with super Keyword - Emp Class Q: Class Emp with Eid & display(). EmpName extends Emp with Ename & display(). Use super keyword import java.util.Scanner; class Emp { int eid; void display() { System.out.println("Employee ID: " + eid); } } class EmpName extends Emp { String ename; void display() { super.display(); System.out.println("Employee Name: " + ename); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); EmpName e = new EmpName(); System.out.print("Enter Employee ID: "); e.eid = sc.nextInt(); System.out.print("Enter Employee Name: "); e.ename = sc.next(); e.display(); } }