Skip to main content

← Back to Past Papers

VNSGU BCA Sem 3: OOP & Data Structures (304) Practical Solutions - April 2025 Set A

Paper Information

AttributeValue
SubjectObject Oriented Programming and Data Structures
Subject Code304
SetA
Semester3
Month/YearApril 2025
Max Marks25
PaperView 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:

  1. Attributes/Data Members: Private variables (empNo, empName, basicSalary) representing the state of an employee.
  2. Constructor: A constructor method to initialize newly created employee objects.
  3. Operations/Methods: Calculations for HRA, DA, PF, Gross, and Net salary, and a displayDetails() function to present data cleanly.
  4. Scanner Class: Used for reading primitive types and strings from standard console input.

2. Algorithm & Step-by-Step Logic

  1. Define class Employee: Include fields for Employee Number, Name, and Basic Salary.
  2. Define calculation logic:
    • HRA = 0.10 * basic
    • DA = 0.15 * basic
    • PF = 0.15 * basic
    • Gross Salary = basic + HRA + DA
    • Net Salary = Gross Salary - PF
  3. Read Inputs: In the main driver program, instantiate a Scanner object to capture user inputs.
  4. Instantiate Object: Create an Employee object passing inputs to the constructor.
  5. 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
  1. Scanner Newline Issue: Calling scanner.nextInt() or scanner.nextDouble() does not consume the newline (\n) character. If you follow it immediately with scanner.nextLine(), it will read an empty string. Always add an extra scanner.nextLine() to flush the buffer.
  2. Use correct datatypes: Do not use float or int for salary parameters. Financial operations should use double (or BigDecimal in enterprise systems) to avoid precision errors.
  3. Keep it Object-Oriented: Avoid doing all calculations directly inside the main() method. Creating a dedicated Employee class highlights your command of Java OOP principles.

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir