Name : Kumavath Vishal
Enroll : 25SE02ML572
ASSIGNMENT -2
CLASSESS AND OBJECTS
[Link] a class 'Student' with variables name, rollNo, and marks. Write a method displayInfo() to print all
details. Create an object and call the method
PROGRAM :-
class Student {
String name;
int rollNo;
double marks;
// Method to display student information
void displayInfo() {
[Link]("Name: " + name);
[Link]("Roll No: " + rollNo);
[Link]("Marks: " + marks);
public class Main {
public static void main(String[] args) {
// Creating a Student object
Student s1 = new Student();
// Assigning values
[Link] = "John";
[Link] = 101;
[Link] = 85.5;
// Calling the method
[Link]();
OUTPUT:-
[Link] a class 'Rectangle' with variables length and breadth. Write methods calculateArea() and
calculatePerimeter(). Create two objects with different dimensions
PROGRAM:-
class Rectangle {
double length;
double breadth;
// Method to calculate area
double calculateArea() {
return length * breadth;
}
// Method to calculate perimeter
double calculatePerimeter() {
return 2 * (length + breadth);
}
}
public class Main {
public static void main(String[] args) {
// First rectangle
Rectangle r1 = new Rectangle();
[Link] = 10;
[Link] = 5;
// Second rectangle
Rectangle r2 = new Rectangle();
[Link] = 8;
[Link] = 4;
// Display results for first rectangle
[Link]("Rectangle 1:");
[Link]("Area = " + [Link]());
[Link]("Perimeter = " + [Link]());
// Display results for second rectangle
[Link]("\nRectangle 2:");
[Link]("Area = " + [Link]());
[Link]("Perimeter = " + [Link]());
}
}
OUTPUT:-
[Link] a class 'BankAccount' with variables accountNumber, holderName, and balance. Write methods
deposit(double amount) and withdraw(double amount) and displayBalance(). Create an object and test it.
PROGRAM:-
class BankAccount {
String accountNumber;
String holderName;
double balance;
// Method to deposit money
void deposit(double amount) {
balance += amount;
[Link](amount + " deposited successfully.");
}
// Method to withdraw money
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link](amount + " withdrawn successfully.");
} else {
[Link]("Insufficient balance!");
}
}
// Method to display balance
void displayBalance() {
[Link]("Account Number: " + accountNumber);
[Link]("Holder Name: " + holderName);
[Link]("Current Balance: " + balance);
}
}
public class Main {
public static void main(String[] args) {
// Creating a BankAccount object
BankAccount account = new BankAccount();
// Assigning values
[Link] = "ACC12345";
[Link] = "John Doe";
[Link] = 5000.0;
// Display initial balance
[Link]();
// Deposit money
[Link](2000);
// Withdraw money
[Link](1500);
// Display updated balance
[Link]();
}
}
OUTPUT:-
[Link] a class 'Circle' with a radius variable. Write methods to calculate area and circumference. Create
three objects with radii 5, 10, and 15.
PROGRAM:-
class Circle {
double radius;
// Method to calculate area
double calculateArea() {
return [Link] * radius * radius;
}
// Method to calculate circumference
double calculateCircumference() {
return 2 * [Link] * radius;
}
}
public class Main {
public static void main(String[] args) {
// Creating three Circle objects
Circle c1 = new Circle();
[Link] = 5;
Circle c2 = new Circle();
[Link] = 10;
Circle c3 = new Circle();
[Link] = 15;
// Display details
[Link]("Circle 1 (Radius = 5)");
[Link]("Area = " + [Link]());
[Link]("Circumference = " + [Link]());
[Link]("\nCircle 2 (Radius = 10)");
[Link]("Area = " + [Link]());
[Link]("Circumference = " + [Link]());
[Link]("\nCircle 3 (Radius = 15)");
[Link]("Area = " + [Link]());
[Link]("Circumference = " + [Link]());
}
}
OUTPUT:-
[Link] a class 'Employee' with variables empId, empName, and salary. Write a method to calculate 10%
bonus and display the updated salary. Create two employee objects
PROGRAM:-
class Employee {
int empId;
String empName;
double salary;
// Method to calculate 10% bonus and update salary
void calculateBonus() {
double bonus = salary * 0.10;
salary += bonus;
}
// Method to display employee details
void displayDetails() {
[Link]("Employee ID: " + empId);
[Link]("Employee Name: " + empName);
[Link]("Updated Salary: " + salary);
}
}
public class Main {
public static void main(String[] args) {
// First Employee
Employee e1 = new Employee();
[Link] = 101;
[Link] = "John";
[Link] = 50000;
// Second Employee
Employee e2 = new Employee();
[Link] = 102;
[Link] = "Alice";
[Link] = 60000;
// Calculate bonus
[Link]();
[Link]();
// Display details
[Link]("Employee 1 Details:");
[Link]();
[Link]("\nEmployee 2 Details:");
[Link]();
}
}
OUTPUT:-