VNSGU BCA Sem 3: OOP & Data Structures (304) Practical Solutions - April 2025 Set A
Paper Information
| Attribute | Value |
|---|---|
| Subject | Object Oriented Programming and Data Structures |
| Subject Code | 304 |
| Set | A |
| Semester | 3 |
| Month/Year | April 2025 |
| Max Marks | 25 |
| Paper | View Paper | Download PDF |
Questions & Solutions
Q1: Employee Salary Calculation
Max Marks: 20
Write a program to accept employee number, employee name & basic salary. Calculate H.R.A., D.A., P.F., Gross Salary, net salary & display all.
- HRA = 10% of basic
- DA = 15% of basic
- PF = 15% of basic
- Gross Salary = Basic + HRA + DA
- Net Salary = Gross Salary - PF
1. Concept Explanation
Object-Oriented Design
To demonstrate correct object-oriented principles, we encapsulate employee details inside a dedicated Employee class:
- Attributes/Data Members: Private variables (
empNo,empName,basicSalary) representing the state of an employee. - Constructor: A constructor method to initialize newly created employee objects.
- Operations/Methods: Calculations for HRA, DA, PF, Gross, and Net salary, and a
displayDetails()function to present data cleanly. - Scanner Class: Used for reading primitive types and strings from standard console input.
2. Algorithm & Step-by-Step Logic
- Define class
Employee: Include fields for Employee Number, Name, and Basic Salary. - Define calculation logic:
HRA=0.10 * basicDA=0.15 * basicPF=0.15 * basicGross Salary=basic + HRA + DANet Salary=Gross Salary - PF
- Read Inputs: In the main driver program, instantiate a
Scannerobject to capture user inputs. - Instantiate Object: Create an
Employeeobject passing inputs to the constructor. - Output Results: Call the object's methods to compute values and print the complete salary slip.
3. Implementation Code & Output
View Solution & Output
// 💼 VNSGU BCA Sem 3 - OOP & Data Structures (Practical 304)
// 🚀 Action: Accept Employee inputs, calculate salary slip elements, display structured output.
import java.util.Scanner;
class Employee {
// Attributes
private int empNo;
private String empName;
private double basicSalary;
// Constructor
public Employee(int empNo, String empName, double basicSalary) {
this.empNo = empNo;
this.empName = empName;
this.basicSalary = basicSalary;
}
// Salary Component Calculation Methods
public double calculateHra() {
return 0.10 * basicSalary; // HRA = 10% basic
}
public double calculateDa() {
return 0.15 * basicSalary; // DA = 15% basic
}
public double calculatePf() {
return 0.15 * basicSalary; // PF = 15% basic
}
public double calculateGrossSalary() {
return basicSalary + calculateHra() + calculateDa();
}
public double calculateNetSalary() {
return calculateGrossSalary() - calculatePf();
}
// Display employee salary slip
public void displaySalarySlip() {
System.out.println("\n=============================================");
System.out.println(" VD COMPUTER TUITION SURAT ");
System.out.println(" EMPLOYEE SALARY SLIP ");
System.out.println("=============================================");
System.out.printf(" Employee ID : %d\n", empNo);
System.out.printf(" Employee Name : %s\n", empName);
System.out.println("---------------------------------------------");
System.out.printf(" Basic Salary : Rs. %.2f\n", basicSalary);
System.out.printf(" H.R.A (10%%) : Rs. %.2f\n", calculateHra());
System.out.printf(" D.A. (15%%) : Rs. %.2f\n", calculateDa());
System.out.printf(" P.F. (15%%) : Rs. %.2f\n", calculatePf());
System.out.println("---------------------------------------------");
System.out.printf(" Gross Salary : Rs. %.2f\n", calculateGrossSalary());
System.out.printf(" Net Salary : Rs. %.2f\n", calculateNetSalary());
System.out.println("=============================================");
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Employee Details:");
System.out.print("Enter Employee Number: ");
int empNo = scanner.nextInt();
// Consume left-over newline character
scanner.nextLine();
System.out.print("Enter Employee Name : ");
String empName = scanner.nextLine();
System.out.print("Enter Basic Salary : ");
double basicSalary = scanner.nextDouble();
// Create Employee object
Employee emp = new Employee(empNo, empName, basicSalary);
// Display results
emp.displaySalarySlip();
scanner.close();
}
}
Expected Console Output:
Enter Employee Details:
Enter Employee Number: 401
Enter Employee Name : Rahul Sharma
Enter Basic Salary : 35000
=============================================
VD COMPUTER TUITION SURAT
EMPLOYEE SALARY SLIP
=============================================
Employee ID : 401
Employee Name : Rahul Sharma
---------------------------------------------
Basic Salary : Rs. 35000.00
H.R.A (10%) : Rs. 3500.00
D.A. (15%) : Rs. 5250.00
P.F. (15%) : Rs. 5250.00
---------------------------------------------
Gross Salary : Rs. 43750.00
Net Salary : Rs. 38500.00
=============================================
💡 Common Examination Mistakes & Tips
Key Pitfalls to Avoid
- Scanner Newline Issue: Calling
scanner.nextInt()orscanner.nextDouble()does not consume the newline (\n) character. If you follow it immediately withscanner.nextLine(), it will read an empty string. Always add an extrascanner.nextLine()to flush the buffer. - Use correct datatypes: Do not use
floatorintfor salary parameters. Financial operations should usedouble(orBigDecimalin enterprise systems) to avoid precision errors. - Keep it Object-Oriented: Avoid doing all calculations directly inside the
main()method. Creating a dedicatedEmployeeclass highlights your command of Java OOP principles.