Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
1. Write an applet program to display a traffic light.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class TrafficLightApplet extends Applet {
public void paint(Graphics g) {
// Draw traffic light outline
g.setColor(Color.BLACK);
g.fillRect(50, 50, 100, 300);
// Draw the red light
g.setColor(Color.RED);
g.fillOval(60, 60, 80, 80);
// Draw the yellow light
g.setColor(Color.YELLOW);
g.fillOval(60, 140, 80, 80);
// Draw the green light
g.setColor(Color.GREEN);
g.fillOval(60, 220, 80, 80);
}
}
OR
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class TrafficLightApplet extends Applet implements Runnable {
private Color currentColor = Color.RED; // Initial traffic light color
private Thread thread; // Thread to control the color change
public void init() {
// Start the thread for color cycling
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
// Draw the traffic light box
g.setColor(Color.BLACK);
g.fillRect(50, 50, 100, 300);
// Draw the red light
g.setColor(currentColor == Color.RED ? Color.RED : Color.GRAY);
g.fillOval(60, 60, 80, 80);
// Draw the yellow light
g.setColor(currentColor == Color.YELLOW ? Color.YELLOW : Color.GRAY);
g.fillOval(60, 140, 80, 80);
// Draw the green light
g.setColor(currentColor == Color.GREEN ? Color.GREEN : Color.GRAY);
g.fillOval(60, 220, 80, 80);
}
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
public void run() {
while (true) {
try {
// Change the traffic light color
if (currentColor == Color.RED) {
currentColor = Color.GREEN; // Switch to green
repaint();
Thread.sleep(5000); // Green for 5 seconds
} else if (currentColor == Color.GREEN) {
currentColor = Color.YELLOW; // Switch to yellow
repaint();
Thread.sleep(2000); // Yellow for 2 seconds
} else if (currentColor == Color.YELLOW) {
currentColor = Color.RED; // Switch to red
repaint();
Thread.sleep(5000); // Red for 5 seconds
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop() {
thread.interrupt(); // Stop the thread when the applet is stopped
}
}
2. Create a class called Matrix which contains a 2d integer array. Include the following
member functions
a. To read the matrix,
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
b. To display the matrix ,
c. check whether the given matrix is symmetric or not.
import java.io.DataInputStream;
import java.io.IOException;
class Matrix {
private int[][] matrix;
private int rows;
private int cols;
// Constructor to initialize the matrix size
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
matrix = new int[rows][cols];
}
// Method to read the matrix
public void readMatrix() throws IOException {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter the elements of the matrix (" + rows + "x" + cols + "):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = Integer.parseInt(dis.readLine());
}
}
}
// Method to display the matrix
public void displayMatrix() {
System.out.println("The matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
// Method to check if the matrix is symmetric
public boolean isSymmetric() {
if (rows != cols) {
// A non-square matrix cannot be symmetric
return false;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
}
}
}
return true;
}
// Main method to test the functionality
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(System.in);
// Read the size of the matrix
System.out.print("Enter the number of rows: ");
int rows = Integer.parseInt(dis.readLine());
System.out.print("Enter the number of columns: ");
int cols = Integer.parseInt(dis.readLine());
// Create a Matrix object
Matrix matrix = new Matrix(rows, cols);
// Read, display, and check for symmetry
matrix.readMatrix();
matrix.displayMatrix();
if (matrix.isSymmetric()) {
System.out.println("The matrix is symmetric.");
} else {
System.out.println("The matrix is not symmetric.");
}
}
}
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
3.Write a swing program to accept a value in a textbox then find the area of a circle
and display the result in the second textbox?
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class CircleAreaApplet extends Applet {
TextField radiusTextField, resultTextField;
Button calculateButton;
public void init() {
// Set layout
setLayout(new FlowLayout());
// Initialize components
radiusTextField = new TextField(10);
resultTextField = new TextField(10);
resultTextField.setEditable(false); // Read-only result field
calculateButton = new Button("Calculate Area");
// Add components to the applet
add(new Label("Enter Radius:"));
add(radiusTextField);
add(calculateButton);
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
add(new Label("Area:"));
add(resultTextField);
// Button click event handling
calculateButton.addActionListener(e -> {
try {
double radius = Double.parseDouble(radiusTextField.getText());
double area = Math.PI * radius * radius;
resultTextField.setText(String.format("%.2f", area));
} catch (NumberFormatException ex) {
resultTextField.setText("Invalid Input");
});
4. Write multithreaded program to print lowercase letters and uppercase letters
from two different threads with suitable delay.
class LowercaseThread extends Thread {
public void run() {
// Print lowercase letters from a to z
for (char ch = 'a'; ch <= 'z'; ch++) {
System.out.print(ch + " ");
try {
Thread.sleep(500); // Delay of 500ms
} catch (InterruptedException e) {
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
System.out.println(e);
}
}
}
}
class UppercaseThread extends Thread {
public void run() {
// Print uppercase letters from A to Z
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.print(ch + " ");
try {
Thread.sleep(500); // Delay of 500ms
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultithreadedLetters {
public static void main(String[] args) {
// Create objects for both threads
LowercaseThread lowercase = new LowercaseThread();
UppercaseThread uppercase = new UppercaseThread();
// Start both threads
lowercase.start();
uppercase.start();
}
}
5.Write an applet program to show how to pass a parameter from an applet code?
import java.applet.Applet;
import java.awt.Graphics;
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
public class ParameterApplet extends Applet {
String message;
public void init() {
// Get the parameter "message" from the HTML code
message = getParameter("message");
public void paint(Graphics g) {
// Display the message passed from the HTML parameter
g.drawString("Message: " + message, 20, 20);
<html>
<body>
<applet code="ParameterApplet.class" width="300" height="100">
<!-- Pass a parameter named "message" -->
<param name="message" value="Hello, Applet!">
</applet>
</body>
</html>
6.Write a Java Program to calculate the Result. Result should consist of name,
seatno, date, center number and marks of semester three exam. Create a User
Defined Exception class MarksOutOfBoundsException, If Entered marks of any
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
subject is greater than 100 or less than 0, and then program should create a user
defined Exception of type.
import java.io.DataInputStream;
import java.io.IOException;
// User-defined exception
class MarksOutOfBoundsException extends Exception {
public MarksOutOfBoundsException(String message) {
super(message);
public class SemesterResult {
public static void main(String[] args) {
DataInputStream input = new DataInputStream(System.in);
try {
// Accept basic student details
System.out.print("Enter Name: ");
String name = input.readLine();
System.out.print("Enter Seat Number: ");
String seatNo = input.readLine();
System.out.print("Enter Date (dd/mm/yyyy): ");
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
String date = input.readLine();
System.out.print("Enter Center Number: ");
String centerNo = input.readLine();
// Accept marks for 3 subjects
System.out.print("Enter Marks for Subject 1: ");
int subject1 = Integer.parseInt(input.readLine());
System.out.print("Enter Marks for Subject 2: ");
int subject2 = Integer.parseInt(input.readLine());
System.out.print("Enter Marks for Subject 3: ");
int subject3 = Integer.parseInt(input.readLine());
// Check marks validity
checkMarks(subject1);
checkMarks(subject2);
checkMarks(subject3);
// Calculate and display result
int totalMarks = subject1 + subject2 + subject3;
double percentage = (totalMarks / 3.0);
System.out.println("\n--- Result ---");
System.out.println("Name: " + name);
System.out.println("Seat No: " + seatNo);
System.out.println("Date: " + date);
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
System.out.println("Center No: " + centerNo);
System.out.println("Total Marks: " + totalMarks);
System.out.println("Percentage: " + percentage + "%");
} catch (MarksOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
// Method to check if marks are within valid range (0-100)
public static void checkMarks(int marks) throws MarksOutOfBoundsException {
if (marks < 0 || marks > 100) {
throw new MarksOutOfBoundsException("Marks must be between 0 and
100.");
7.Write an applet program to draw a house
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
public class HouseApplet extends Applet {
public void paint(Graphics g) {
// Draw the house (rectangle for the base)
g.setColor(Color.BLUE);
g.fillRect(50, 100, 200, 150);
// Draw the roof (triangle)
g.setColor(Color.RED);
int[] xPoints = {50, 150, 250};
int[] yPoints = {100, 40, 100};
g.fillPolygon(xPoints, yPoints, 3);
// Draw the door (rectangle)
g.setColor(Color.YELLOW);
g.fillRect(130, 180, 40, 70);
// Draw the window (rectangle)
g.setColor(Color.WHITE);
g.fillRect(75, 130, 40, 40);
g.fillRect(175, 130, 40, 40);
// Draw window lines
g.setColor(Color.BLACK);
g.drawLine(75, 150, 115, 150);
g.drawLine(95, 130, 95, 170);
g.drawLine(175, 150, 215, 150);
g.drawLine(195, 130, 195, 170);
}
}
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
8. Create a class called Matrix which contains a 2d integer array, m & n (order of
matrix) as data members. Include the following member functions
a. To read the matrix,
b. To display the matrix ,
c. Overload a method product () to find the product of two matrices and to
multiply each element of a matrix with a constant value
import java.io.DataInputStream;
import java.io.IOException;
class Matrix {
int m, n;
int[][] matrix;
// Method to read the matrix
public void readMatrix() throws IOException {
DataInputStream input = new DataInputStream(System.in);
System.out.print("Enter the number of rows (m): ");
m = Integer.parseInt(input.readLine());
System.out.print("Enter the number of columns (n): ");
n = Integer.parseInt(input.readLine());
matrix = new int[m][n];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < m; i++) {
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
for (int j = 0; j < n; j++) {
System.out.print("Enter element [" + (i+1) + "][" + (j+1) + "]: ");
matrix[i][j] = Integer.parseInt(input.readLine());
// Method to display the matrix
public void displayMatrix() {
System.out.println("Matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
System.out.println();
// Overloaded product method to multiply two matrices
public Matrix product(Matrix mat2) {
if (this.n != mat2.m) {
System.out.println("Matrix multiplication not possible. Column of first matrix must
be equal to row of second matrix.");
return null;
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
}
Matrix result = new Matrix();
result.m = this.m;
result.n = mat2.n;
result.matrix = new int[result.m][result.n];
// Multiply the matrices
for (int i = 0; i < result.m; i++) {
for (int j = 0; j < result.n; j++) {
result.matrix[i][j] = 0;
for (int k = 0; k < this.n; k++) {
result.matrix[i][j] += this.matrix[i][k] * mat2.matrix[k][j];
return result;
// Overloaded product method to multiply each element with a constant
public void product(int constant) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
matrix[i][j] *= constant;
public class MatrixExample {
public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream(System.in);
// Create first matrix and read its elements
Matrix matrix1 = new Matrix();
matrix1.readMatrix();
matrix1.displayMatrix();
// Create second matrix and read its elements
Matrix matrix2 = new Matrix();
matrix2.readMatrix();
matrix2.displayMatrix();
// Find product of the two matrices
Matrix result = matrix1.product(matrix2);
if (result != null) {
System.out.println("\nProduct of the two matrices:");
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
result.displayMatrix();
// Multiply each element of matrix1 with a constant
System.out.print("\nEnter a constant to multiply each element of the first matrix: ");
int constant = Integer.parseInt(input.readLine());
matrix1.product(constant);
System.out.println("\nMatrix after multiplication with constant:");
matrix1.displayMatrix();
9. Write a swing program to accept an integer in a textbox then reverse that number
and display the result in the second textbox?
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ReverseNumberApplet extends Applet {
TextField inputField, resultField;
Button reverseButton;
public void init() {
// Initialize components
inputField = new TextField(20);
resultField = new TextField(20);
reverseButton = new Button("Reverse");
// Add components to applet
add(inputField);
add(reverseButton);
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
add(resultField);
// Set layout and action for button
setLayout(new FlowLayout());
reverseButton.addActionListener(e -> {
try {
int number = Integer.parseInt(inputField.getText());
resultField.setText(String.valueOf(reverseNumber(number)));
} catch (NumberFormatException ex) {
resultField.setText("Invalid input!");
}
});
}
// Method to reverse the number
private int reverseNumber(int number) {
int reversed = 0;
while (number != 0) {
reversed = reversed * 10 + (number % 10);
number /= 10;
}
return reversed;
}
}
10. Write a Java program which creates a class named 'Employee' having the
following members: Name, Age, Phone number, Address, Salary. It also has a
method named 'printSalary( )' which prints the salary of the Employee. Two
classes 'Officer' and 'Manager' inherits the 'Employee' class. The 'Officer' and
'Manager' classes have data members 'specialization' and 'department'
respectively. Now, assign name, age, phone number, address and salary to an
officer and a manager by making an object of both of these classes and print the
same.
import java.io.*;
class Employee {
String name, phone, address;
int age;
double salary;
// Method to print salary
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
void printSalary() {
System.out.println("Salary: " + salary);
}
}
class Officer extends Employee {
String specialization;
}
class Manager extends Employee {
String department;
}
public class EmployeeDetails {
public static void main(String[] args) {
// Create a DataInputStream for reading input
DataInputStream dis = new DataInputStream(System.in);
try {
// Create Officer object
Officer officer = new Officer();
System.out.println("Enter Officer details:");
System.out.print("Name: ");
officer.name = dis.readLine();
System.out.print("Age: ");
officer.age = Integer.parseInt(dis.readLine());
System.out.print("Phone: ");
officer.phone = dis.readLine();
System.out.print("Address: ");
officer.address = dis.readLine();
System.out.print("Salary: ");
officer.salary = Double.parseDouble(dis.readLine());
System.out.print("Specialization: ");
officer.specialization = dis.readLine();
// Create Manager object
Manager manager = new Manager();
System.out.println("Enter Manager details:");
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
System.out.print("Name: ");
manager.name = dis.readLine();
System.out.print("Age: ");
manager.age = Integer.parseInt(dis.readLine());
System.out.print("Phone: ");
manager.phone = dis.readLine();
System.out.print("Address: ");
manager.address = dis.readLine();
System.out.print("Salary: ");
manager.salary = Double.parseDouble(dis.readLine());
System.out.print("Department: ");
manager.department = dis.readLine();
// Print Officer details
System.out.println("\nOfficer Details:");
System.out.println("Name: " + officer.name);
System.out.println("Age: " + officer.age);
System.out.println("Phone: " + officer.phone);
System.out.println("Address: " + officer.address);
officer.printSalary();
System.out.println("Specialization: " + officer.specialization);
// Print Manager details
System.out.println("\nManager Details:");
System.out.println("Name: " + manager.name);
System.out.println("Age: " + manager.age);
System.out.println("Phone: " + manager.phone);
System.out.println("Address: " + manager.address);
manager.printSalary();
System.out.println("Department: " + manager.department);
} catch (IOException e) {
System.out.println("Error reading input");
}
}
}
11. APPLET PROGRAM FOR HUMAN FACE
Time: 3 Hours
(Q1: 25 marks, Q2: 35 marks, Record: 10 marks, Viva: 10 Marks)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class HumanFaceApplet extends Applet {
public void paint(Graphics g) {
// Set color for the face
g.setColor(Color.YELLOW);
g.fillOval(50, 50, 200, 200); // Draw the face (oval)
// Set color for the eyes
g.setColor(Color.WHITE);
g.fillOval(100, 100, 40, 40); // Left eye
g.fillOval(160, 100, 40, 40); // Right eye
// Set color for the eye pupils
g.setColor(Color.BLACK);
g.fillOval(115, 115, 10, 10); // Left pupil
g.fillOval(175, 115, 10, 10); // Right pupil
// Set color for the mouth
g.setColor(Color.RED);
g.fillArc(100, 150, 100, 50, 0, -180); // Draw the mouth (arc)
// Set color for the nose
g.setColor(Color.BLACK);
g.fillPolygon(new int[]{150, 145, 155}, new int[]{130, 150, 150}, 3); // Draw the nose
(triangle)
}
}