[Link] a java program to delete the files having extension. txt.
(Use command line
arguments)
import [Link];
public class DeleteTxtFiles {
public static void main(String[] args) {
// Check if directory path is provided as a command-line argument
if ([Link] != 1) {
[Link]("Usage: java DeleteTxtFiles <directory_path>");
return;
// Get the directory path from command-line argument
String directoryPath = args[0];
File directory = new File(directoryPath);
// Check if the path is a valid directory
if (![Link]() || ![Link]()) {
[Link]("Invalid directory path: " + directoryPath);
return;
// Get list of all files in the directory
File[] files = [Link]();
if (files == null || [Link] == 0) {
[Link]("No files found in the directory.");
return;
// Loop through each file and delete if it ends with .txt
for (File file : files) {
if ([Link]() && [Link]().endsWith(".txt")) {
if ([Link]()) {
[Link]("Deleted: " + [Link]());
} else {
[Link]("Failed to delete: " + [Link]());
2. Write a java program using swing to accept the details of project (PID, PName, duration)
from user and display it by clicking on a button.
import [Link].*;
import [Link].*;
import [Link].*;
public class ProjectDetailsSwing extends JFrame implements ActionListener {
// Declare components
JTextField tPid, tPname, tDuration;
JButton btnDisplay;
JTextArea output;
public ProjectDetailsSwing() {
// Set frame title
setTitle("Project Details Form");
setSize(400, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// Create labels and text fields
add(new JLabel("Project ID:"));
tPid = new JTextField(20);
add(tPid);
add(new JLabel("Project Name:"));
tPname = new JTextField(20);
add(tPname);
add(new JLabel("Duration (in days):"));
tDuration = new JTextField(20);
add(tDuration);
// Create button
btnDisplay = new JButton("Display Details");
add(btnDisplay);
// Create output area
output = new JTextArea(5, 30);
[Link](false);
add(new JScrollPane(output));
// Add Action Listener
[Link](this);
// Make frame visible
setVisible(true);
// Action performed when button is clicked
public void actionPerformed(ActionEvent e) {
if ([Link]() == btnDisplay) {
String pid = [Link]();
String pname = [Link]();
String duration = [Link]();
// Display project details
[Link]("Project Details:\n");
[Link]("PID: " + pid + "\n");
[Link]("Project Name: " + pname + "\n");
[Link]("Duration: " + duration + " days");
public static void main(String[] args) {
new ProjectDetailsSwing();
3. Define an abstract class Shape with abstract method area ( ). Write a java
program to calculate area of Triangle, Circle.
import [Link];
// Abstract class
abstract class Shape {
abstract void area(); // Abstract method
// Triangle class inheriting Shape
class Triangle extends Shape {
double base, height;
// Constructor
Triangle(double base, double height) {
[Link] = base;
[Link] = height;
// Implement area method
void area() {
double result = 0.5 * base * height;
[Link]("Area of Triangle: " + result);
// Circle class inheriting Shape
class Circle extends Shape {
double radius;
// Constructor
Circle(double radius) {
[Link] = radius;
// Implement area method
void area() {
double result = [Link] * radius * radius;
[Link]("Area of Circle: " + result);
}
// Main class
public class ShapeArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input for Triangle
[Link]("Enter base of triangle: ");
double base = [Link]();
[Link]("Enter height of triangle: ");
double height = [Link]();
Shape t = new Triangle(base, height);
[Link]();
// Input for Circle
[Link]("\nEnter radius of circle: ");
double radius = [Link]();
Shape c = new Circle(radius);
[Link]();
[Link]();
}
4. Write a java program to change the text color of Lable to Red by clicking on a button.
import [Link].*;
import [Link].*;
import [Link].*;
public class ChangeLabelColor extends JFrame implements ActionListener {
JLabel label;
JButton button;
public ChangeLabelColor() {
// Set frame properties
setTitle("Change Label Color");
setSize(300, 200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create label
label = new JLabel("Hello, this is a label!");
add(label);
// Create button
button = new JButton("Change Color to Red");
add(button);
// Add action listener to button
[Link](this);
// Make frame visible
setVisible(true);
// Action performed when button is clicked
public void actionPerformed(ActionEvent e) {
[Link]([Link]); // Change text color to red
public static void main(String[] args) {
new ChangeLabelColor();
5. Write a java program to copy the content from one file to another file,
While copying change the case of alphabets.
import [Link].*;
public class CopyFileChangeCase {
public static void main(String[] args) {
// Check if correct number of arguments are passed
if ([Link] != 2) {
[Link]("Usage: java CopyFileChangeCase <source_file> <destination_file>");
return;
String sourceFile = args[0];
String destinationFile = args[1];
try (
FileReader fr = new FileReader(sourceFile);
FileWriter fw = new FileWriter(destinationFile);
){
int ch;
// Read each character and change case
while ((ch = [Link]()) != -1) {
char c = (char) ch;
if ([Link](c)) {
[Link]([Link](c));
} else if ([Link](c)) {
[Link]([Link](c));
} else {
[Link](c); // Copy non-alphabetic characters as is
}
}
[Link]("File copied successfully with changed case!");
} catch (IOException e) {
[Link]("Error: " + [Link]());
6. Write a java program using swing to accept details of employee (eno, ename, esal) and
display it by clicking on a button.
import [Link].*;
import [Link].*;
import [Link].*;
public class EmployeeDetailsSwing extends JFrame implements ActionListener {
// Components
JLabel lblNo, lblName, lblSal;
JTextField txtNo, txtName, txtSal;
JButton btnDisplay;
JTextArea output;
// Constructor
public EmployeeDetailsSwing() {
// Frame setup
setTitle("Employee Details Form");
setSize(400, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// Labels and TextFields
lblNo = new JLabel("Employee No:");
txtNo = new JTextField(20);
lblName = new JLabel("Employee Name:");
txtName = new JTextField(20);
lblSal = new JLabel("Employee Salary:");
txtSal = new JTextField(20);
// Button
btnDisplay = new JButton("Display Details");
// TextArea for output
output = new JTextArea(5, 30);
[Link](false);
// Add components to frame
add(lblNo); add(txtNo);
add(lblName); add(txtName);
add(lblSal); add(txtSal);
add(btnDisplay);
add(new JScrollPane(output));
// Add ActionListener
[Link](this);
// Make frame visible
setVisible(true);
// Handle button click
public void actionPerformed(ActionEvent e) {
String eno = [Link]();
String ename = [Link]();
String esal = [Link]();
[Link]("Employee Details:\n");
[Link]("Employee No: " + eno + "\n");
[Link]("Employee Name: " + ename + "\n");
[Link]("Employee Salary: " + esal);
// Main method
public static void main(String[] args) {
new EmployeeDetailsSwing();
7. Write a java program to give red color at the background of TextField by clicking on a
button.
import [Link].*;
import [Link].*;
import [Link].*;
public class TextFieldRedBackground extends JFrame implements ActionListener {
JTextField textField;
JButton button;
public TextFieldRedBackground() {
// Frame setup
setTitle("Change TextField Background Color");
setSize(350, 200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create text field
textField = new JTextField(20);
add(textField);
// Create button
button = new JButton("Change to Red");
add(button);
// Add action listener
[Link](this);
// Make frame visible
setVisible(true);
// When button is clicked
public void actionPerformed(ActionEvent e) {
[Link]([Link]); // change background to red
public static void main(String[] args) {
new TextFieldRedBackground();
}
8. Define an interface shape with abstract method area(). Inherit interface shape into the class
triangle. Write a java program to calculate area of triangle.
import [Link];
// Define interface
interface Shape {
void area(); // abstract method
// Triangle class implementing Shape interface
class Triangle implements Shape {
double base, height;
// Constructor
Triangle(double base, double height) {
[Link] = base;
[Link] = height;
// Implement area() method
public void area() {
double result = 0.5 * base * height;
[Link]("Area of Triangle: " + result);
}
// Main class
public class InterfaceShapeTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter base of triangle: ");
double base = [Link]();
[Link]("Enter height of triangle: ");
double height = [Link]();
// Create Triangle object
Shape t = new Triangle(base, height);
// Call area method
[Link]();
[Link]();
9. Write a Java Program to copy the contents form one file into another file. While copying,
change the case of cell the alphabets & replace all the digit by '*'
import [Link].*;
public class CopyFileChangeCaseReplaceDigits {
public static void main(String[] args) {
// Ensure correct arguments
if ([Link] != 2) {
[Link]("Usage: java CopyFileChangeCaseReplaceDigits <source_file>
<destination_file>");
return;
String sourceFile = args[0];
String destinationFile = args[1];
try (
FileReader fr = new FileReader(sourceFile);
FileWriter fw = new FileWriter(destinationFile);
){
int ch;
// Read character by character
while ((ch = [Link]()) != -1) {
char c = (char) ch;
// Change case
if ([Link](c)) {
c = [Link](c);
} else if ([Link](c)) {
c = [Link](c);
// Replace digits
else if ([Link](c)) {
c = '*';
// Write to destination file
[Link](c);
[Link]("File copied successfully with case changed and digits replaced!");
} catch (IOException e) {
[Link]("Error: " + [Link]());
10. Define user define exception zeronumber Exc. Write a Java program to accept a number
from user. If it is zero then throw user define exception "Number is zero" otherwise calculate
the sum of first & last digit of given number. (use Static Keyword).
import [Link];
// User-defined exception class
class ZeroNumberException extends Exception {
public ZeroNumberException(String message) {
super(message);
public class ZeroNumberExceptionDemo {
// Static method to calculate sum of first and last digit
public static int sumFirstLast(int num) {
int last = num % 10;
while (num >= 10) {
num = num / 10;
int first = num;
return first + last;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
try {
// Check for zero and throw exception
if (number == 0) {
throw new ZeroNumberException("Number is zero");
// Calculate and display sum of first and last digit
int sum = sumFirstLast([Link](number)); // handle negative numbers
[Link]("Sum of first and last digit: " + sum);
} catch (ZeroNumberException e) {
[Link]("Exception: " + [Link]());
[Link]();
11. Write a Java program to accept n number from user & store only perfect numbers into
array & display that array.
import [Link];
public class PerfectNumberArray {
// Static method to check if a number is perfect
public static boolean isPerfect(int num) {
if (num <= 0)
return false;
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0)
sum += i;
return sum == num;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter how many numbers you want: ");
int n = [Link]();
int[] input = new int[n];
int[] perfect = new int[n]; // same size (only perfect numbers will be used)
int count = 0;
// Accept n numbers
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
input[i] = [Link]();
if (isPerfect(input[i])) {
perfect[count++] = input[i];
// Display perfect numbers
[Link]("\nPerfect numbers are:");
if (count == 0)
[Link]("No perfect numbers found.");
else {
for (int i = 0; i < count; i++) {
[Link](perfect[i] + " ");
[Link]();
12. Define a class Emp with a member Eid and display() method, inherit EmP class into the
Emp Name class, Emp Name class having a member Ename & display ( ) method. Write a Java
program to accept details of employee [Eid, Ename] & display it. (Use super keyword).
import [Link];
// Base class
class Emp {
int eid;
// Constructor
Emp(int eid) {
[Link] = eid;
// Display method
void display() {
[Link]("Employee ID: " + eid);
// Derived class
class EmpName extends Emp {
String ename;
// Constructor using super to call parent constructor
EmpName(int eid, String ename) {
super(eid); // call parent constructor
[Link] = ename;
}
// Override display method
void display() {
[Link](); // call parent display()
[Link]("Employee Name: " + ename);
// Main class
public class EmployeeInheritance {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Accept details from user
[Link]("Enter Employee ID: ");
int id = [Link]();
[Link](); // clear buffer
[Link]("Enter Employee Name: ");
String name = [Link]();
// Create object and display details
EmpName e = new EmpName(id, name);
[Link]("\nEmployee Details:");
[Link]();
[Link]();