PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
ADDITIONAL PROGRAMS
EX 1 CALCULATE AVERAGE AND ATTENDANCE
PERCENTAGE OF STUDENTS
PROGRAM :
import [Link];
class Student {
private String studentId;
private String name;
private String branch;
private int year;
private String location;
private String college;
private int[] marks; // Array to store marks of 6 subjects
private int totalClasses; // Total classes held
private int classesAttended; // Total classes attended
// Constructor to initialize student details
public Student(String studentId, String name, String branch, int year, String location, String college)
{
[Link] = studentId;
[Link] = name;
[Link] = branch;
[Link] = year;
[Link] = location;
[Link] = college;
[Link] = new int[6]; // Initializing array for 6 subjects
}
// Method to set marks for the subjects
public void setMarks(int[] marks) {
if ([Link] == 6)
{ [Link] = marks;
} else {
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
[Link]("Please provide marks for exactly 6 subjects.");
}
}
// Method to calculate average of marks
public double calculateAverage() {
int sum = 0;
for (int mark : marks)
{ sum += mark;
}
return (double) sum / [Link];
}
// Method to set attendance details
public void setAttendance(int totalClasses, int classesAttended)
{ [Link] = totalClasses;
[Link] = classesAttended;
}
// Method to calculate attendance percentage
public double calculateAttendancePercentage() {
if (totalClasses == 0) return 0.0; // Avoid division by zero
return ((double) classesAttended / totalClasses) * 100;
}
// Method to display student details
public void displayDetails() {
[Link]("Student ID: " + studentId);
[Link]("Name: " + name);
[Link]("Branch: " + branch);
[Link]("Year: " + year);
[Link]("Location: " + location);
[Link]("College: " + college);
[Link]("Average Marks: " + calculateAverage());
[Link]("Attendance Percentage: " + calculateAttendancePercentage() + "%");
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
public class StudentDetails {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
// Create a Student object with initial values
Student student = new Student("S123", "John Doe", "Computer Science", 2, "New York", "XYZ
University");
// Input marks for 6 subjects
[Link]("Enter marks for 6 subjects:");
int[] marks = new int[6];
for (int i = 0; i < 6; i++)
{ [Link]("Subject " + (i + 1) + ":
"); marks[i] = [Link]();
}
[Link](marks);
// Input attendance details
[Link]("Enter total classes held: ");
int totalClasses = [Link]();
[Link]("Enter total classes attended: ");
int classesAttended = [Link]();
[Link](totalClasses, classesAttended);
// Display student details
[Link]();
[Link]();
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
EX 2 SIMPLE TEST APPLICATION DEMONSTRATING
DYNAMIC POLYMORPHISM
PROGRAM:
// Base class
abstract class Shape
{ abstract double area();
abstract void draw();
}
// Rectangle class
class Rectangle extends Shape
{ private double width;
private double height;
public Rectangle(double width, double height)
{ [Link] = width;
[Link] = height;
}
@Override
double area() {
return width * height;
}
@Override
void draw() {
[Link]("Drawing a Rectangle");
}
}
// Square class
class Square extends Rectangle
{ public Square(double side) {
super(side, side);
}
@Override
void draw() {
[Link]("Drawing a Square");
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
// Circle class
class Circle extends Shape {
private double radius;
public Circle(double radius)
{ [Link] = radius;
}
@Override
double area() {
return [Link] * radius * radius;
}
@Override
void draw() {
[Link]("Drawing a Circle");
}
}
// Ellipse class
class Ellipse extends Shape
{ private double
semiMajorAxis; private double
semiMinorAxis;
public Ellipse(double semiMajorAxis, double semiMinorAxis)
{ [Link] = semiMajorAxis;
[Link] = semiMinorAxis;
}
@Override
double area() {
return [Link] * semiMajorAxis * semiMinorAxis;
}
@Override
void draw() {
[Link]("Drawing an Ellipse");
}
}
// Triangle class
class Triangle extends Shape
{ private double base;
private double height;
public Triangle(double base, double height)
{ [Link] = base;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
[Link] = height;
}
@Override
double area() {
return 0.5 * base * height;
}
@Override
void draw() {
[Link]("Drawing a Triangle");
}
}
// Polygon class
class Polygon extends Shape
{ private double[] sides;
public Polygon(double[] sides)
{ [Link] = sides;
}
@Override
double area() {
return 0.0; // Area calculation can be complex for general polygons
}
@Override
void draw() {
[Link]("Drawing a Polygon");
}
}
// Test application
public class ShapeTest
{
public static void main(String[] args)
{ Shape[] shapes = new Shape[6];
shapes[0] = new Rectangle(5, 10);
shapes[1] = new Square(4);
shapes[2] = new Circle(3);
shapes[3] = new Ellipse(5, 3);
shapes[4] = new Triangle(6, 8);
shapes[5] = new Polygon(new double[]{3, 4, 5, 6});
// Demonstrating dynamic polymorphism
for (Shape shape : shapes) {
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
[Link]("Area: " + [Link]());
[Link]();
}
}
}
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
EX 3 CREATING THREADS
PROGRAM:
class MessageThread extends Thread {
private String message;
private int interval;
public MessageThread(String message, int interval) {
[Link] = message;
[Link] = interval;
}
@Override
public void run() {
while (true) {
[Link](message);
try {
[Link](interval);
} catch (InterruptedException e) {
[Link]().interrupt();
break;
}
}
}
}
public class MultiThreadExample {
public static void main(String[] args) {
Thread thread1 = new MessageThread("Good Morning", 1000); // 1 second
Thread thread2 = new MessageThread("Hello", 2000); // 2 seconds
Thread thread3 = new MessageThread("Welcome", 3000); // 3 seconds
// Start the threads
[Link]();
[Link]();
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
[Link]();
// Optional: Join threads to main thread (if you want to wait for them to finish)
// In this case, they run indefinitely, so this is commented out.
// try {
// [Link]();
// [Link]();
// [Link]();
// } catch (InterruptedException e) {
// [Link]().interrupt();
// }
}
}
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
EX 4 FINDING FACTORIAL OF A NUMBER
PROGRAM:
import [Link].*;
import [Link];
import [Link];
public class FactorialCalculator extends JFrame
{ private JTextField inputField;
private JTextField resultField;
private JButton calculateButton;
public FactorialCalculator() {
// Set up the frame
setTitle("Factorial Calculator");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// Input field
JLabel inputLabel = new JLabel("Enter a number:");
[Link](10, 10, 120, 25);
add(inputLabel);
inputField = new JTextField();
[Link](140, 10, 100, 25);
add(inputField);
// Calculate button
calculateButton = new JButton("Calculate Factorial");
[Link](10, 40, 230, 25);
add(calculateButton);
// Result field
JLabel resultLabel = new JLabel("Factorial:");
[Link](10, 70, 120, 25);
add(resultLabel);
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
resultField = new JTextField();
[Link](140, 70, 100, 25);
[Link](false); // Make result field non-editable
add(resultField);
// Button action listener
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{ String inputText = [Link]();
try {
int number = [Link](inputText);
long factorial = calculateFactorial(number);
[Link]([Link](factorial));
} catch (NumberFormatException ex)
{ [Link]("Invalid input!");
}
}
});
}
// Method to calculate factorial
private long calculateFactorial(int number)
{ long factorial = 1;
for (int i = 1; i <= number; i++)
{ factorial *= i;
}
return factorial;
}
public static void main(String[] args)
{ [Link](() -> {
FactorialCalculator calculator = new FactorialCalculator();
[Link](true);
});
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE [Link]
OUTPUT:
RESULT: