1) Write a java program to display alternate character from a given
string.
public class AlternateCharacters {
public static void main(String[] args) {
String input = “ExampleString”;
For (int I = 0; I < input.length(); I += 2) {
System.out.print(input.charAt(i));
2) Write a Java program to calculate area of Circle, Triangle &
Rectangle.(Use Method Overloading).
public class AreaCalculator {
public double area(double radius) {
return Math.PI * radius * radius;
public double area(double base, double height) {
return 0.5 * base * height;
public double area(double length, double width) {
return length * width;
public static void main(String[] args) {
AreaCalculator calc = new AreaCalculator();
System.out.println("Circle Area: " + calc.area(5));
System.out.println("Triangle Area: " + calc.area(4, 6));
System.out.println("Rectangle Area: " + calc.area(7, 3));
}
3) Write a java program to search given name into the array, if it is
found then display its index otherwise display appropriate message.
public class NameSearch {
public static void main(String[] args) {
String[] names = {"Alice", "Bob", "Charlie"};
String target = "Bob";
int index = -1;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(target)) {
index = i;
break;
if (index != -1) {
System.out.println("Index: " + index);
} else {
System.out.println("Name not found");
4) Write a java program to display ASCII values of the characters
from a file.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class AsciiValuesFromFile {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new
FileReader("input.txt"))) {
int c;
while ((c = reader.read()) != -1) {
System.out.println((char) c + " : " + (int) c);
} catch (IOException e) {
e.printStackTrace();
5) Write a java program to display multiplication table of a given
number into the List box by clicking on button.
Import javax.swing.*;
Import java.awt.event.ActionEvent;
Import java.awt.event.ActionListener;
Public class MultiplicationTableGUI {
Public static void main(String[] args) {
JFrame frame = new JFrame(“Multiplication Table”);
JButton button = new JButton(“Generate Table”);
JList<String> list = new JList<>();
Button.addActionListener(new ActionListener() {
Public void actionPerformed(ActionEvent e) {
Int number = 5; // Example number
DefaultListModel<String> model = new DefaultListModel<>();
For (int I = 1; I <= 10; i++) {
Model.addElement(number + “ x “ + I + “ = “ + (number * i));
List.setModel(model);
});
Frame.setLayout(new java.awt.BorderLayout());
Frame.add(button, java.awt.BorderLayout.NORTH);
Frame.add(new JScrollPane(list), java.awt.BorderLayout.CENTER);
Frame.setSize(300, 200);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
6) Create a package named Series having three different classes to
print
series:
i) Fibonacci series
ii) Cube of numbers
iii) Square of numbers
Write a java program to generate ‘n’ terms of the above series
Fibonacci.java
Package Series;
Public class Fibonacci {
Public static void generate FibonacciSeries(int n) {
Int a = 0, b = 1;
System.out.print(“Fibonacci Series (“ + n + “ terms): “);
For (int I = 1; I <= n; i++) {
System.out.print(a + “ “);
Int sum a + b;
A = b;
B = sum;
CubeOfNumbers.java
Package Series;
Public class CubeOfNumbers {
Public static void generateCubeSeries(int n) {
System.out.print(“Cube of Numbers (“ + n + “ terms): “);
For (int I = 1; I <= n; i++) {
Int cube = I * I * I;
System.out.print(cube + “ “);
SquareOfNumbers.java
Package Series;
Public class SquareOfNumbers {
Public static void generateSquareSeries(int n) {
System.out.print(“Square of Numbers (“ + n +
“ terms): “);
For (int I = 1; I <= n; i++) {
Int square = I * I;
System.out.print(square + “ “);
SeriesMain.java
Import Series.*;
Public class Series Main {
Public static void main(String[] args) {
Int n = 10;
Fibonacci.generateFibonacciSeries(n); System.out.println();
CubeOfNumbers.generateCubeSeries(n);
System.out.println();
SquareOfNumbers.generateSquareSeries(n);
System.out.println();
8) Write a ‘java’ program to check whether given number is
Armstrong or not. (Use static keyword)
Public class ArmstrongNumber {
Public static boolean isArmstrong(int number) {
Int original = number, sum = 0, digits =
String.valueOf(number).length();
While (number > 0) {
Int digit = number % 10;
Sum += Math.pow(digit, digits);
Number /= 10;
}
Return sum == original;
Public static void main(String[] args) {
Int num = 153; // Example number
If (isArmstrong(num)) {
System.out.println(num + “ is an Armstrong number.”);
} else {
System.out.println(num + “ is not an Armstrong number.”);
9) Write a ‘java’ program to copy only non-numeric data from one
file to another file.
Import java.io.BufferedReader;
Import java.io.BufferedWriter;
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
Public class NonNumericFileCopy {
Public static void main(String[] args) {
String sourceFile = “source.txt”;
String destinationFile = “destination.txt”;
Try (BufferedReader reader = new BufferedReader(new
FileReader(sourceFile));
BufferedWriter writer = new BufferedWriter(new
FileWriter(destinationFile))) {
String line;
While ((line = reader.readLine()) != null) {
If (!containsNumeric(line)) {
Writer.write(line);
Writer.newLine();
} catch (IOException e) {
e.printStackTrace();
Private static boolean containsNumeric(String line) {
For (char c : line.toCharArray()) {
If (Character.isDigit©) {
Return true;
Return false;
10)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 StudentDetailsAWT extends Frame implements ActionListener {
Label l1, l2, l3;
TextField tf1, tf2, tf3;
Button b1;
String sid, sname, saddr;
Public StudentDetailsAWT() {
setLayout(new FlowLayout());
l1 = new Label(“Student ID:”);
l2 = new Label(“Student Name:”);
l3 = new Label(“Student Address:”);
tf1 = new TextField(20);
tf2 = new TextField(20);
tf3 = new TextField(20);
b1 = new Button(“Submit”);
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(b1);
b1.addActionListener(this);
setTitle(“Enter Student Details”);
setSize(300, 200);
setVisible(true);
Public void actionPerformed(ActionEvent e) {
Sid = tf1.getText();
Sname = tf2.getText();
Saddr = tf3.getText();
This.setVisible(false);
Frame displayFrame = new Frame(“Student Details”);
displayFrame.setLayout(new FlowLayout());
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.setSize(300, 200);
displayFrame.setVisible(true);
displayFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
});
Public static void main(String[] args) {
New StudentDetailsAWT();
11)Write a package MCA which has one class student. Accept
student Details through parameterized constructor. Write display()
method to display details. Create a main class which will use
package and calculate total marks and percentage.
Package MCA;
Import MCA Student
Public class Student {
String sid;
String sname;
Int marks1, marks2, marks3;
Public Student(String sid, String sname, int marks1, int marks2, int
marks3) {
This.sid = sid;
This.sname = sname;
This.marks1 = marks1;
This.marks2 = marks2;
This.marks3 = marks3;
Public void display() {
System.out.println(“Student ID: “ + sid);
System.out.println(“Student Name: “ + sname);
System.out.println(“Marks 1: “ + marks1);
System.out.println(“Marks 2: “ + marks2);
System.out.println(“Marks 3: “ + marks3);
Public class MainClass {
Public static void main(String[] args) {
Student student = new Student(“S101”, “John Doe”, 85, 90, 88);
Student.display();
Int totalMarks = student.marks1 + student.marks2 + student.marks3;
Float percentage = (totalMarks / 3.0f);
System.out.println(“Total Marks: “ + totalMarks);
System.out.println(“Percentage: “ + percentage + “%”);
}
12)Write Java program which accepts string from user, if its length
is less than five, then throw user defined exception “Invalid String”
otherwise display string in uppercase.
import java.util.Scanner;
class InvalidStringException extends Exception {
public InvalidStringException(String message) {
super(message);
public class StringValidation {
public static void validateString(String str) throws
InvalidStringException {
if (str.length() < 5) {
throw new InvalidStringException("Invalid String: The length of the
string is less than five.");
} else {
System.out.println("Valid String in Uppercase: " +
str.toUpperCase());
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
try {
validateString(input);
} catch (InvalidStringException e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
13)Write a Java Program using Applet to create login form.
Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
Public class LoginFormApplet extends Applet implements
ActionListener {
Label userLabel, passLabel, messageLabel;
TextField userField, passField;
Button loginButton;
Public void init() {
setLayout(new GridLayout(4, 2));
userLabel = new Label(“Username:”);
passLabel = new Label(“Password:”);
userField = new TextField(20);
passField = new TextField(20);
passField.setEchoChar(‘*’);
loginButton = new Button(“Login”);
messageLabel = new Label();
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(new Label());
add(loginButton);
add(messageLabel); loginButton.addActionListener(this);
}
Public void actionPerformed(ActionEvent ae) {
String username = userField.getText();
String password = passField.getText();
If (username.equals(“admin”) && password.equals(“password123”)) {
messageLabel.setText(“Login Successful”);
} else {
messageLabel.setText(“Invalid Username or Password”);
}
}
}
14)What is recursion is Java? Write a Java Program to final factorial
of a Given number using recursion.
Import java.util.Scanner;
Public class Factorial {
Public static int factorial(int n) {
If (n == 0) { // Base case: factorial of 0 is 1
Return 1;
} else {
Return n * factorial(n – 1); // Recursive case
}
}
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
Int number = scanner.nextInt();
Scanner.close();
If (number < 0) {
System.out.println(“Factorial is not defined for negative
numbers.”);
} else {
Int result = factorial(number);
System.out.println(“Factorial of “ + number + “ is “ + result);
}
}
}
15)Write a applet application in java for designing smiley.
Import java.applet.Applet;
Import java.awt.Color;
Import java.awt.Graphics;
Public class SmileyApplet extends Applet {
Public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(50, 50, 200, 200); // Draw face
g.setColor(Color.BLACK);
g.fillOval(100, 100, 30, 30); // Left eye
g.fillOval(170, 100, 30, 30); // Right eye
g.setColor(Color.RED);
g.drawArc(100, 150, 100, 50, 0, -180);
<!DOCTYPE html>
<html>
<head>
<title>Smiley Applet</title>
</head>
<body>
<applet code=”SmileyApplet.class” width=”300” height=”300”>
Your browser does not support Java Applets.
</applet>
</body>
</html>
16)Write a java program to copy the dates from one file into another
file.
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
Public class FileCopy {
Public static void main(String[] args) {
If (args.length != 2) {
System.out.println(“Usage: java FileCopy <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;
While ((ch = fr.read()) != -1) {
Fw.write(ch);
System.out.println(“File copied successfully!”);
} catch (IOException e) {
System.out.println(“An error occurred during file operations.”);
e.printStackTrace();
17)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 you want to input: “);
Int n = scanner.nextInt();
System.out.println(“Enter “ + n + “ integers:”);
For (int I = 0; I < n; i++) {
Int number = scanner.nextInt();
Numbers.add(number);
Collections.reverse(numbers);
System.out.println(“Elements in reverse order:”);
For (int num : numbers) {
System.out.println(num);
Scanner.close();