1) Write a java program which accepts student details (Sid, Sname, Saddr) from user and display it on next
frame. (Use AWT).
import [Link].*;
import [Link].*;
class StudentDetailsForm extends Frame implements ActionListener {
private TextField sidField, snameField, saddrField;
private Button submitButton;
public StudentDetailsForm() {
setTitle("Student Details Form");
setSize(300, 200);
setLayout(new FlowLayout());
setResizable(false);
Label sidLabel = new Label("Student ID:");
sidField = new TextField(15);
Label snameLabel = new Label("Student Name:");
snameField = new TextField(15);
Label saddrLabel = new Label("Student Address:");
saddrField = new TextField(15);
submitButton = new Button("Submit");
[Link](this);
add(sidLabel);
add(sidField);
add(snameLabel);
add(snameField);
add(saddrLabel);
add(saddrField);
add(submitButton);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
}
public void actionPerformed(ActionEvent ae)
if ([Link]() == submitButton) {
String sid = [Link]();
String sname = [Link]();
String saddr = [Link]();
Frame displayFrame = new Frame();
[Link]("Student Details");
[Link](300, 200);
[Link](new FlowLayout());
[Link](false);
Label sidLabel = new Label("Student ID: " + sid);
Label snameLabel = new Label("Student Name: " + sname);
Label saddrLabel = new Label("Student Address: " + saddr);
[Link](sidLabel);
[Link](snameLabel);
[Link](saddrLabel);
[Link](true);
public class Main {
public static void main(String[] args) {
StudentDetailsForm form = new StudentDetailsForm();
2) Write a package MCA which has one class student. Accept student details through parameterized
constructor. Write display() method to display details. reate a main class which will use package and calculate
Total marks and percentage
package MCA;
public class Student {
private String name;
private int rollNumber;
private int marks1;
private int marks2;
private int marks3;
public Student(String name, int rollNumber, int marks1, int marks2, int marks3) {
[Link] = name;
[Link] = rollNumber;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
public void display() {
[Link]("Student Name: " + name);
[Link]("Roll Number: " + rollNumber);
[Link]("Marks 1: " + marks1);
[Link]("Marks 2: " + marks2);
[Link]("Marks 3: " + marks3);
3) Write Java programy w which accepts string from user, if its length is less than five, then throw user defined
exception "Invalid String" otherwise display string is uppercase
import [Link];
class InvalidStringException extends Exception {
public InvalidStringException(String message) {
super(message);
}}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
try {
if ([Link]() < 5) {
throw new InvalidStringException("Invalid String");
} else {
String uppercaseString = [Link]();
[Link]("Uppercase string: " + uppercaseString);
} catch (InvalidStringException e) {
[Link]("Exception: " + [Link]()); }}}
4) Write a Java Program using Applet to create login form
import [Link];
import [Link].*;
import [Link];
import [Link];
public class LoginFormApplet extends Applet implements ActionListener {
private TextField usernameField;
private TextField passwordField;
private Button loginButton;
public void init() {
setLayout(new FlowLayout());
Label usernameLabel = new Label("Username:");
usernameField = new TextField(20);
Label passwordLabel = new Label("Password:");
passwordField = new TextField(20);
[Link]('*');
loginButton = new Button("Login");
[Link](this);
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);
public void actionPerformed(ActionEvent e) {
if ([Link]() == loginButton) {
String username = [Link]();
String password = [Link]();
if ([Link]("admin") && [Link]("password")) {
showStatus("Login successful!");
} else {
showStatus("Invalid username or password");
[Link]("");
[Link](""); } } }
5) What is recursion is Java? Write a Java Program to final factorial of a given number using recursion
public class FactorialCalculator {
public static void main(String[] args) {
int number = 5;
int factorial = calculateFactorial(number);
[Link]("Factorial of " + number + " is: " + factorial);
public static int calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * calculateFactorial(n - 1);
6) Write a java program to copy the dates from one file into another file
import [Link].*;
public class FileCopy {
public static void main(String[] args) {
String sourceFilePath = "path/to/source/[Link]";
String destinationFilePath = "path/to/destination/[Link]";
try {
File sourceFile = new File(sourceFilePath);
File destinationFile = new File(destinationFilePath);
FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destinationFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
[Link]("File copied successfully!");
[Link]();
[Link]();
} catch (IOException e) {
[Link]("An error occurred while copying the file.");
[Link]();
7) Write a java program to accept' 'n' integers from the user & store them in an ArrayList Collection. Display the
elements of ArrayList collection in reverse order
import [Link];
import [Link];
import [Link];
public class ReverseArrayList {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
ArrayList<Integer> numbers = new ArrayList<>();
[Link]("Enter the number of integers: ");
int n = [Link]();
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int num = [Link]();
[Link](num);
[Link]("Elements in reverse order:");
[Link](numbers);
for (int num : numbers) {
[Link](num);
8) Write a Java program to display all the perfect numbers between 1 to n.
public class PerfectNumbers {
public static void main(String[] args) {
int n = 100;
[Link]("Perfect numbers between 1 and " + n + ":");
for (int i = 1; i <= n; i++) {
if (isPerfectNumber(i)) {
[Link](i);
public static boolean isPerfectNumber(int number) {
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
return sum == number;
}
9) Write a Java program to calculate area of circle, Triangle and Rectangle (Use Method over loading)
public class AreaCalculator {
public static void main(String[] args) {
double radius = 5.0;
double base = 8.0;
double height = 6.0;
double length = 10.0;
double width = 4.0;
double circleArea = calculateArea(radius);
double triangleArea = calculateArea(base, height);
double rectangleArea = calculateArea(length, width);
[Link]("Area of the circle: " + circleArea);
[Link]("Area of the triangle: " + triangleArea);
[Link]("Area of the rectangle: " + rectangleArea);
public static double calculateArea(double radius) {
return [Link] * radius * radius; // Area of a circle = π * r^2
public static double calculateArea(double base, double height) {
return 0.5 * base * height
public static double calculateArea(double length, double width) {
return length * width;
10) Write a Java program to count number of digits, spaces and characters from a file
import [Link];
import [Link];
import [Link];
import [Link];
public class FileCharacterCount {
public static void main(String[] args) {
String filePath = "path/to/[Link]"; // Replace with the actual file path
try {
File file = new File(filePath);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int character;
int digitCount = 0;
int spaceCount = 0;
int charCount = 0;
while ((character = [Link]()) != -1) {
char c = (char) character;
if ([Link](c)) {
digitCount++;
} else if ([Link](c)) {
spaceCount+
charCount++;
[Link]("Number of digits: " + digitCount);
[Link]("Number of spaces: " + spaceCount);
[Link]("Number of characters: " + charCount);
[Link]();
} catch (IOException e) {
[Link]("An error occurred while reading the file.");
[Link]();
}
11) Write a java program to count number of Lines, words and characters from given file
import [Link];
import [Link];
import [Link];
public class FileStatistics {
public static void main(String[] args) {
String filePath = "path/to/[Link]"; // Replace with the actual file path
int lineCount = 0;
int wordCount = 0;
int charCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = [Link]()) != null) {
lineCount++;
charCount += [Link]();
String[] words = [Link]("\\s+");
wordCount += [Link];
} catch (IOException e) {
[Link]("An error occurred while reading the file.");
[Link]();
[Link]("Number of lines: " + lineCount);
[Link]("Number of words: " + wordCount);
[Link]("Number of characters: " + charCount);
12) Write a Java program to design email registration form. (Use swing component)
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class EmailRegistrationForm extends JFrame {
private JTextField emailField;
private JPasswordField passwordField;
private JButton registerButton;
public EmailRegistrationForm() {
setTitle("Email Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2));
JLabel emailLabel = new JLabel("Email:");
emailField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
registerButton = new JButton("Register");
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String email = [Link]();
String password = [Link]([Link]())
[Link]([Link],
"Registration Successful\nEmail: " + email + "\nPassword: " + password,
"Registration Status", JOptionPane.INFORMATION_MESSAGE);
});
add(emailLabel);
add(emailField);
add(passwordLabel);
add(passwordField);
add(new JLabel()); // Empty label for spacing
add(registerButton);
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
EmailRegistrationForm form = new EmailRegistrationForm();
[Link](true);
});
13) Create a class Teacher (Tid, Tname, Designation, Salary, Subject). Write a java program to accept 'n'
teachers and display who teach Java subject (Use Array of object)
import [Link];
public class Teacher {
private int tid;
private String tname;
private String designation;
private double salary;
private String subject;
public Teacher(int tid, String tname, String designation, double salary, String subject) {
[Link] = tid;
[Link] = tname;
[Link] = designation;
[Link] = salary;
[Link] = subject;
public String getSubject() {
return subject;
}
public String toString() {
return "Teacher ID: " + tid + "\nTeacher Name: " + tname + "\nDesignation: " + designation + "\nSalary: $"
+ salary + "\nSubject: " + subject;
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of teachers: ");
int n = [Link]();
Teacher[] teachers = new Teacher[n];
for (int i = 0; i < n; i++) {
[Link]("\nEnter details for Teacher " + (i + 1) + ":");
[Link]("Teacher ID: ");
int tid = [Link]();
[Link](); // Consume the newline character
[Link]("Teacher Name: ");
String tname = [Link]();
[Link]("Designation: ");
String designation = [Link]();
[Link]("Salary: ");
double salary = [Link]();
[Link](); // Consume the newline character
[Link]("Subject: ");
String subject = [Link]();
teachers[i] = new Teacher(tid, tname, designation, salary, subject);
[Link]("\nTeachers who teach Java subject:");
for (Teacher teacher : teachers) {
if ([Link]().equalsIgnoreCase("Java")) {
[Link](teacher);