JAVA Programs
Swing:
Write a java program using swing to accept details of employee (eno, ename, esal) and display
it on console by clicking on Display button. Clear button should clear all the controls.
import [Link].*;
import [Link].*;
import [Link].*;
public class EmployeeForm extends JFrame implements ActionListener
JLabel lno, lname, lsal;
JTextField tno, tname, tsal;
JButton btnDisplay, btnClear;
public EmployeeForm()
// Labels
lno = new JLabel("Employee No:");
lname = new JLabel("Employee Name:");
lsal = new JLabel("Employee Salary:");
// Text fields
tno = new JTextField(10);
tname = new JTextField(10);
tsal = new JTextField(10);
// Buttons
btnDisplay = new JButton("Display");
btnClear = new JButton("Clear");
// Add action listeners
[Link](this);
[Link](this);
// Layout
setLayout(new FlowLayout());
add(lno); add(tno);
add(lname); add(tname);
add(lsal); add(tsal);
add(btnDisplay); add(btnClear);
// Frame settings
setTitle("Employee Details Form");
setSize(350, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
public void actionPerformed(ActionEvent e)
if ([Link]() == btnDisplay) {
String eno = [Link]();
String ename = [Link]();
String esal = [Link]();
[Link]("Employee Details:");
[Link]("Employee No: " + eno);
[Link]("Employee Name: " + ename);
[Link]("Employee Salary: " + esal);
[Link]("----------------------------------");
else if ([Link]() == btnClear)
[Link]("");
[Link]("");
[Link]("");
public static void main(String[] args)
new EmployeeForm();
}
Exception:
Write a java program to accept employee name from user, if it is not valid then throw user
defined exception “InvalidName” otherwise display a name.
import [Link];
// User-defined exception
class InvalidName extends Exception
public InvalidName(String message)
super(message);
public class EmployeeNameCheck
// Method to validate name
static void validateName(String name) throws InvalidName
if (name == null || [Link]().isEmpty() || )
throw new InvalidName("InvalidName: Name contains invalid characters or is empty");
}
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
[Link]("Enter Employee Name: ");
String name = [Link]();
try
validateName(name);
[Link]("Valid Name: " + name);
catch (InvalidName e)
[Link]([Link]());
File:
Write a java program to display contents of file in reverse order.
import [Link].*;
public class ReverseFileContent
{
public static void main(String[] args)
BufferedReader br = null;
try
// Accept file name from user
BufferedReader input = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter file name: ");
String filename = [Link]();
// Read entire file content into a string
br = new BufferedReader(new FileReader(filename));
StringBuilder content = new StringBuilder();
String line;
while ((line = [Link]()) != null)
[Link](line).append("\n");
// Display reversed content
[Link]("Reversed File Content:");
[Link]([Link]().toString());
}
catch (Exception e)
[Link]("Error: " + [Link]());
finally
try
if (br != null)
[Link]();
catch (IOException e)
[Link]("Error closing file.");
Write a Java Program to copy the contents form one file into another file. While copying,
change the case of the alphabets & replace all the digit by '*'
import [Link].*;
public class CopyFileWithChanges
public static void main(String[] args)
BufferedReader br = null;
BufferedWriter bw = null;
try
// Input from user
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter source file name: ");
String src = [Link]();
[Link]("Enter destination file name: ");
String dest = [Link]();
br = new BufferedReader(new FileReader(src));
bw = new BufferedWriter(new FileWriter(dest));
int ch;
while ((ch = [Link]()) != -1)
char c = (char) ch;
// Replace digits with '*'
if ([Link](c))
[Link]('*');
// Change case
else if ([Link](c))
[Link]([Link](c));
else if ([Link](c))
[Link]([Link](c));
else
// Other characters remain same
[Link](c);
[Link]("File copied successfully with modifications!");
catch (Exception e)
{
[Link]("Error: " + [Link]());
finally
try
if (br != null) [Link]();
if (bw != null) [Link]();
catch (IOException e)
[Link]("Error closing files.");
Other:
Define abstract class shape with abstract method area (). Write a java program to calculate
area of rectangle. (circle, triangle)
Hint:
Rectangle: length * breadth
Triangle: 0.5 * base * height
Circle: [Link] * radius * radius
abstract class Shape
abstract void area(); // abstract method
class Rectangle extends Shape
double length, breadth;
Rectangle(double l, double b)
length = l;
breadth = b;
// Implement abstract method
void area()
double result = length * breadth;
[Link]("Area of Rectangle = " + result);
public class ShapeDemo
public static void main(String[] args)
{
Rectangle r = new Rectangle(10, 5); // sample values
[Link]();
Write a Java program to define a class ‘Doctor’ with data members doctorId, doctorName and
doctorSpecialization. Accept the data for ‘n’ objects using array of objects and display it.
import [Link];
class Doctor
int doctorId;
String doctorName;
String doctorSpecialization;
// Method to accept data
void acceptData(Scanner sc)
[Link]("Enter Doctor ID: ");
doctorId = [Link]();
[Link](); // consume newline
[Link]("Enter Doctor Name: ");
doctorName = [Link]();
[Link]("Enter Doctor Specialization: ");
doctorSpecialization = [Link]();
// Method to display data
void displayData()
[Link]("Doctor ID: " + doctorId);
[Link]("Doctor Name: " + doctorName);
[Link]("Specialization: " + doctorSpecialization);
[Link]("-------------------------------------");
public class DoctorDemo
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
[Link]("How many doctors? ");
int n = [Link]();
Doctor[] docArr = new Doctor[n];
// Accept details
for (int i = 0; i < n; i++)
[Link]("\nEnter details for Doctor " + (i + 1) + ":");
docArr[i] = new Doctor();
docArr[i].acceptData(sc);
// Display details
[Link]("\n===== Doctor Details =====");
for (int i = 0; i < n; i++)
docArr[i].displayData();