0% found this document useful (0 votes)
16 views13 pages

Java Assignment-3

The document contains multiple Java class implementations for managing different entities such as Students, Employees, LibraryBooks, BankAccounts, and Medicines. Each class includes attributes, constructors, methods for calculations, and display functionalities. The classes demonstrate object-oriented programming principles with encapsulation and method overloading.

Uploaded by

Sushil Laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Java Assignment-3

The document contains multiple Java class implementations for managing different entities such as Students, Employees, LibraryBooks, BankAccounts, and Medicines. Each class includes attributes, constructors, methods for calculations, and display functionalities. The classes demonstrate object-oriented programming principles with encapsulation and method overloading.

Uploaded by

Sushil Laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

NAME – MAHAPRASAD ROUT

ROLL NO – 37

REG.NO – 2405432040

1.
class Student {

private String name;

private int rollNo;

private double totalMarks;

private double maxMarks;

private double percentage;

private char grade;

public Student(String name, int rollNo) {

this.name = name;

this.rollNo = rollNo;

public Student(String name, int rollNo, double[] marks) {

this.name = name;

this.rollNo = rollNo;

this.totalMarks = calculateTotal(marks);

this.maxMarks = marks.length * 100;

this.percentage = calculatePercentage(this.totalMarks, this.maxMarks);

this.grade = getGrade(this.percentage);

public Student(String name, int rollNo, double totalMarks, double maxMarks) {

this.name = name;

this.rollNo = rollNo;

this.totalMarks = totalMarks;

this.maxMarks = maxMarks;
this.percentage = calculatePercentage(totalMarks, maxMarks);

this.grade = getGrade(this.percentage);

public sta c double calculateTotal(int marks1, int marks2, int marks3) {

return marks1 + marks2 + marks3;

public sta c double calculateTotal(double[] marksArray) {

double total = 0;

for (double mark : marksArray) {

total += mark;

return total;

public sta c double calculatePercentage(double total, double maxMarks) {

return (total / maxMarks) * 100;

public sta c char getGrade(double percentage) {

if (percentage >= 90) return 'A';

else if (percentage >= 80) return 'B';

else if (percentage >= 70) return 'C';

else if (percentage >= 60) return 'D';

else return 'F';

public void displayDetails() {

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

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

System.out.println("Total Marks: " + totalMarks + "/" + maxMarks);

System.out.println("Percentage: " + percentage + "%");


System.out.println("Grade: " + grade);

public sta c void main(String[] args) {

System.out.println("Total Marks (int): " + calculateTotal(85, 90, 78));

double[] marks = {85, 90, 78};

System.out.println("Total Marks (array): " + calculateTotal(marks));

System.out.println("Percentage: " + calculatePercentage(253, 300) + "%");

System.out.println("Grade: " + getGrade(84.33));

Student student1 = new Student("Alice", 101, marks);

student1.displayDetails();

Student student2 = new Student("Bob", 102, 250, 300);

student2.displayDetails();

2.

class Employee {

private String name;

private int empID;

private double basicSalary;

private double taxRate;

private double bonus;

private double netSalary;

public Employee(String name, int empID) {

this.name = name;
this.empID = empID;

public Employee(String name, int empID, double basicSalary, double taxRate) {

this.name = name;

this.empID = empID;

this.basicSalary = basicSalary;

this.taxRate = taxRate;

this.netSalary = calculateSalary(basicSalary, taxRate);

public Employee(String name, int empID, double basicSalary, double taxRate, double bonus) {

this.name = name;

this.empID = empID;

this.basicSalary = basicSalary;

this.taxRate = taxRate;

this.bonus = bonus;

this.netSalary = calculateSalary(basicSalary, taxRate, bonus);

public sta c double calculateSalary(double basicSalary, double taxRate) {

return basicSalary - (basicSalary * taxRate / 100);

public sta c double calculateSalary(double basicSalary, double taxRate, double bonus) {

return (basicSalary - (basicSalary * taxRate / 100)) + bonus;

public sta c double calculateAnnualSalary(double monthlySalary) {

return monthlySalary * 12;

public void displayDetails() {


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

System.out.println("Employee ID: " + empID);

System.out.println("Basic Salary: " + basicSalary);

System.out.println("Tax Rate: " + taxRate + "%");

System.out.println("Bonus: " + bonus);

System.out.println("Net Salary: " + netSalary);

System.out.println("Annual Salary: " + calculateAnnualSalary(netSalary));

public sta c void main(String[] args) {

System.out.println("Net Salary (without bonus): " + calculateSalary(5000, 10));

System.out.println("Net Salary (with bonus): " + calculateSalary(5000, 10, 500));

System.out.println("Annual Salary: " + calculateAnnualSalary(4500));

Employee emp1 = new Employee("John Doe", 101, 5000, 10);

emp1.displayDetails();

Employee emp2 = new Employee("Jane Smith", 102, 6000, 12, 700);

emp2.displayDetails();

3.
class LibraryBook {

private String tle;

private String author;

private int totalCopies;

private int issuedCopies;

public LibraryBook(String tle, String author) {


this. tle = tle;

this.author = author;

this.totalCopies = 1;

this.issuedCopies = 0;

public LibraryBook(String tle, String author, int totalCopies) {

this. tle = tle;

this.author = author;

this.totalCopies = totalCopies;

this.issuedCopies = 0;

public LibraryBook(String tle, String author, int totalCopies, int issuedCopies) {

this. tle = tle;

this.author = author;

this.totalCopies = totalCopies;

this.issuedCopies = issuedCopies;

public void getBookDetails(String tle) {

System.out.println("Title: " + tle);

public void getBookDetails(String tle, String author) {

System.out.println("Title: " + tle + ", Author: " + author);

public void getBookDetails(String tle, String author, int copiesAvailable) {

System.out.println("Title: " + tle + ", Author: " + author + ", Copies Available: " + copiesAvailable);

public boolean checkAvailability(int requestedCopies) {


return (totalCopies - issuedCopies) >= requestedCopies;

public void issueBook(int copies) {

if (checkAvailability(copies)) {

issuedCopies += copies;

System.out.println(copies + " copy(ies) issued successfully.");

} else {

System.out.println("Not enough copies available.");

public void returnBook(int copies) {

if (issuedCopies >= copies) {

issuedCopies -= copies;

System.out.println(copies + " copy(ies) returned successfully.");

} else {

System.out.println("Invalid return a empt.");

public void displayBookStatus() {

System.out.println("Title: " + tle);

System.out.println("Author: " + author);

System.out.println("Total Copies: " + totalCopies);

System.out.println("Issued Copies: " + issuedCopies);

System.out.println("Available Copies: " + (totalCopies - issuedCopies));

public sta c void main(String[] args) {

LibraryBook book1 = new LibraryBook("Java Programming", "James Gosling", 5, 2);

book1.getBookDetails("Java Programming");

book1.getBookDetails("Java Programming", "James Gosling");

book1.getBookDetails("Java Programming", "James Gosling", book1.totalCopies - book1.issuedCopies);


System.out.println("Availability Check: " + book1.checkAvailability(2));

book1.issueBook(2);

book1.displayBookStatus();

book1.returnBook(1);

book1.displayBookStatus();

4.
class BankAccount {

private String accountHolder;

private int accountNumber;

private double balance;

private String accountType;

public BankAccount(String accountHolder, int accountNumber) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = 0.0;

public BankAccount(String accountHolder, int accountNumber, double balance) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = balance;

public BankAccount(String accountHolder, int accountNumber, double balance, String accountType) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = balance;
this.accountType = accountType;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

System.out.println("Deposited: " + amount + " | New Balance: " + balance);

} else {

System.out.println("Invalid deposit amount.");

public void deposit(double amount, String currencyType) {

double conversionRate = getConversionRate(currencyType);

double convertedAmount = amount * conversionRate;

deposit(convertedAmount);

System.out.println("Deposited: " + amount + " " + currencyType + " (Converted to " + convertedAmount + "
USD)");

public boolean withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

System.out.println("Withdrawn: " + amount + " | New Balance: " + balance);

return true;

} else {

System.out.println("Insufficient balance or invalid amount.");

return false;

public boolean transferFunds(double amount, BankAccount recipient) {

if (withdraw(amount)) {
recipient.deposit(amount);

System.out.println("Transferred: " + amount + " to Account: " + recipient.accountNumber);

return true;

} else {

System.out.println("Transfer failed.");

return false;

private double getConversionRate(String currencyType) {

switch (currencyType.toUpperCase()) {

case "EUR": return 1.1;

case "GBP": return 1.3;

case "INR": return 0.013;

default: return 1.0;

public void displayAccountDetails() {

System.out.println("Account Holder: " + accountHolder);

System.out.println("Account Number: " + accountNumber);

System.out.println("Balance: " + balance);

if (accountType != null) {

System.out.println("Account Type: " + accountType);

public sta c void main(String[] args) {

BankAccount acc1 = new BankAccount("Alice", 1001, 5000.0, "Savings");

BankAccount acc2 = new BankAccount("Bob", 1002, 3000.0);

acc1.deposit(1000);

acc1.deposit(500, "EUR");

acc1.withdraw(2000);
acc1.transferFunds(1500, acc2);

acc1.displayAccountDetails();

acc2.displayAccountDetails();

5.
import java. me.Year;

class Medicine {

private String name;

private String manufacturer;

private int expiryYear;

private int stockQuan ty;

private double price;

public Medicine(String name, String manufacturer) {

this.name = name;

this.manufacturer = manufacturer;

public Medicine(String name, String manufacturer, int expiryYear, int stockQuan ty) {

this.name = name;

this.manufacturer = manufacturer;

this.expiryYear = expiryYear;

this.stockQuan ty = stockQuan ty;

public Medicine(String name, String manufacturer, int expiryYear, int stockQuan ty, double price) {

this.name = name;

this.manufacturer = manufacturer;
this.expiryYear = expiryYear;

this.stockQuan ty = stockQuan ty;

this.price = price;

public boolean isExpired(int expiryYear) {

int currentYear = Year.now().getValue();

return expiryYear < currentYear;

public boolean isExpired(int expiryYear, int manufactureYear) {

int shelfLife = expiryYear - manufactureYear;

return isExpired(expiryYear) || shelfLife <= 0;

public boolean checkStock(int requiredQuan ty) {

return stockQuan ty >= requiredQuan ty;

public void updateStock(int quan tyAdded, boolean isRestock) {

if (isRestock) {

stockQuan ty += quan tyAdded;

} else {

stockQuan ty = Math.max(0, stockQuan ty - quan tyAdded);

System.out.println("Updated Stock: " + stockQuan ty);

public void displayMedicineDetails() {

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

System.out.println("Manufacturer: " + manufacturer);

System.out.println("Expiry Year: " + expiryYear);

System.out.println("Stock Quan ty: " + stockQuan ty);


System.out.println("Price: " + price);

public sta c void main(String[] args) {

Medicine med1 = new Medicine("Paracetamol", "XYZ Pharma", 2025, 100, 5.0);

med1.displayMedicineDetails();

System.out.println("Is Expired: " + med1.isExpired(2025));

System.out.println("Is Expired (with manufacture year): " + med1.isExpired(2025, 2020));

System.out.println("Stock Available: " + med1.checkStock(50));

med1.updateStock(20, true);

med1.updateStock(30, false);

You might also like