Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

1) Write a java program which accepts student details (Sid, Sname, Saddr) from user and display it on next

frame. (Use AWT).

import java.awt.*;

import java.awt.event.*;

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");

submitButton.addActionListener(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) {

System.exit(0);

});
}

public void actionPerformed(ActionEvent ae)


if (ae.getSource() == submitButton) {

String sid = sidField.getText();

String sname = snameField.getText();

String saddr = saddrField.getText();

Frame displayFrame = new Frame();

displayFrame.setTitle("Student Details");

displayFrame.setSize(300, 200);

displayFrame.setLayout(new FlowLayout());

displayFrame.setResizable(false);

Label sidLabel = new Label("Student ID: " + sid);

Label snameLabel = new Label("Student Name: " + sname);

Label saddrLabel = new Label("Student Address: " + saddr);

displayFrame.add(sidLabel);

displayFrame.add(snameLabel);

displayFrame.add(saddrLabel);

displayFrame.setVisible(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) {

this.name = name;

this.rollNumber = rollNumber;

this.marks1 = marks1;

this.marks2 = marks2;

this.marks3 = marks3;

public void display() {

System.out.println("Student Name: " + name);

System.out.println("Roll Number: " + rollNumber);

System.out.println("Marks 1: " + marks1);

System.out.println("Marks 2: " + marks2);

System.out.println("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 java.util.Scanner;

class InvalidStringException extends Exception {

public InvalidStringException(String message) {

super(message);

}}

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");

String input = scanner.nextLine();

try {

if (input.length() < 5) {
throw new InvalidStringException("Invalid String");

} else {

String uppercaseString = input.toUpperCase();

System.out.println("Uppercase string: " + uppercaseString);

} catch (InvalidStringException e) {

System.out.println("Exception: " + e.getMessage()); }}}

4) Write a Java Program using Applet to create login form

import java.applet.Applet;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

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);

passwordField.setEchoChar('*');

loginButton = new Button("Login");

loginButton.addActionListener(this);

add(usernameLabel);

add(usernameField);

add(passwordLabel);

add(passwordField);

add(loginButton);

public void actionPerformed(ActionEvent e) {

if (e.getSource() == loginButton) {
String username = usernameField.getText();

String password = passwordField.getText();

if (username.equals("admin") && password.equals("password")) {

showStatus("Login successful!");

} else {

showStatus("Invalid username or password");

usernameField.setText("");

passwordField.setText(""); } } }

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);

System.out.println("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 java.io.*;

public class FileCopy {

public static void main(String[] args) {

String sourceFilePath = "path/to/source/file.txt";

String destinationFilePath = "path/to/destination/file.txt";

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 = fis.read(buffer)) != -1) {

fos.write(buffer, 0, bytesRead);

System.out.println("File copied successfully!");

fis.close();

fos.close();

} catch (IOException e) {

System.out.println("An error occurred while copying the file.");

e.printStackTrace();

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 java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class ReverseArrayList {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

ArrayList<Integer> numbers = new ArrayList<>();

System.out.print("Enter the number of integers: ");

int n = scanner.nextInt();

System.out.println("Enter " + n + " integers:");


for (int i = 0; i < n; i++) {

int num = scanner.nextInt();

numbers.add(num);

System.out.println("Elements in reverse order:");

Collections.reverse(numbers);

for (int num : numbers) {

System.out.println(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;

System.out.println("Perfect numbers between 1 and " + n + ":");

for (int i = 1; i <= n; i++) {

if (isPerfectNumber(i)) {

System.out.println(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);

System.out.println("Area of the circle: " + circleArea);

System.out.println("Area of the triangle: " + triangleArea);

System.out.println("Area of the rectangle: " + rectangleArea);

public static double calculateArea(double radius) {

return Math.PI * 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 java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;
import java.io.IOException;

public class FileCharacterCount {

public static void main(String[] args) {

String filePath = "path/to/file.txt"; // 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 = bufferedReader.read()) != -1) {

char c = (char) character;

if (Character.isDigit(c)) {

digitCount++;

} else if (Character.isWhitespace(c)) {

spaceCount+

charCount++;

System.out.println("Number of digits: " + digitCount);

System.out.println("Number of spaces: " + spaceCount);

System.out.println("Number of characters: " + charCount);

bufferedReader.close();

} catch (IOException e) {

System.out.println("An error occurred while reading the file.");

e.printStackTrace();

}
11) Write a java program to count number of Lines, words and characters from given file

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class FileStatistics {

public static void main(String[] args) {

String filePath = "path/to/file.txt"; // 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 = br.readLine()) != null) {

lineCount++;

charCount += line.length();

String[] words = line.split("\\s+");

wordCount += words.length;

} catch (IOException e) {

System.out.println("An error occurred while reading the file.");

e.printStackTrace();

System.out.println("Number of lines: " + lineCount);

System.out.println("Number of words: " + wordCount);

System.out.println("Number of characters: " + charCount);

12) Write a Java program to design email registration form. (Use swing component)

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

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");

registerButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String email = emailField.getText();

String password = String.valueOf(passwordField.getPassword())

JOptionPane.showMessageDialog(EmailRegistrationForm.this,

"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) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

EmailRegistrationForm form = new EmailRegistrationForm();

form.setVisible(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 java.util.Scanner;

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) {

this.tid = tid;

this.tname = tname;

this.designation = designation;

this.salary = salary;

this.subject = 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(System.in);

System.out.print("Enter the number of teachers: ");

int n = scanner.nextInt();

Teacher[] teachers = new Teacher[n];

for (int i = 0; i < n; i++) {

System.out.println("\nEnter details for Teacher " + (i + 1) + ":");

System.out.print("Teacher ID: ");

int tid = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

System.out.print("Teacher Name: ");

String tname = scanner.nextLine();

System.out.print("Designation: ");

String designation = scanner.nextLine();

System.out.print("Salary: ");

double salary = scanner.nextDouble();

scanner.nextLine(); // Consume the newline character

System.out.print("Subject: ");

String subject = scanner.nextLine();

teachers[i] = new Teacher(tid, tname, designation, salary, subject);

System.out.println("\nTeachers who teach Java subject:");

for (Teacher teacher : teachers) {

if (teacher.getSubject().equalsIgnoreCase("Java")) {

System.out.println(teacher);

You might also like