0% found this document useful (0 votes)
5 views

exp_1

The document outlines an experiment conducted by a student in the Computer Science & Engineering department, focusing on creating a Java application to manage employee information using arrays. It includes the aim, objectives, implementation code, and learning outcomes related to the use of the Scanner class, arrays, and basic arithmetic calculations in Java. Key functionalities of the application include adding, finding, and displaying employee details along with their total salary.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

exp_1

The document outlines an experiment conducted by a student in the Computer Science & Engineering department, focusing on creating a Java application to manage employee information using arrays. It includes the aim, objectives, implementation code, and learning outcomes related to the use of the Scanner class, arrays, and basic arithmetic calculations in Java. Key functionalities of the application include adding, finding, and displaying employee details along with their total salary.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1

Student Name: Aakanksha Sharma UID: 22BCS16783


Branch: BE-CSE Section/Group: IOT_632_A
Semester: 6th Date of Performance: 10-01-25
Subject Name: PBLJ Lab Subject Code: 22CSH-359

1. Aim: Create an application to save the employee information using arrays.

2. Objective:
• To learn the implementation of Scanner Class.
• To learn about arrays in Java.
• To learn about basic arithmetic calculation in Java.
3. Implementation/Code:
import java.util.Scanner;

public class Employee {


private int empNo;
private String empName;
private String dept;
private String designation;
private double hra;
private double it;
private double ta;

public Employee(int empNo, String empName, String dept, String designation, double hra,
double it, double ta) {
this.empNo = empNo;
this.empName = empName;
this.dept = dept;
this.designation = designation;
this.hra = hra;
this.it = it;
this.ta = ta;
}

public void displayInfo() {


System.out.printf("%-10d %-20s %-15s %-15s %-10.2f %-10.2f %-10.2f%n", empNo,
empName, dept, designation, hra, it, ta);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}

public double calculateTotalSalary() {


return hra + it + ta;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
Employee[] employees = new Employee[3]; // Array to hold 3 employees
int count = 0;

while (true) {
System.out.println("Menu:");
System.out.println("1. Add Employee");
System.out.println("2. Find Employee");
System.out.println("3. Show All Employees");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1: // Add Employee
if (count < employees.length) {
System.out.println("Enter details for Employee " + (count + 1) + ":");
System.out.print("Employee No: ");
int empNo = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Employee Name: ");
String empName = scanner.nextLine();
System.out.print("Department: ");
String dept = scanner.nextLine();
System.out.print("Designation: ");
String designation = scanner.nextLine();
System.out.print("HRA: ");
double hra = scanner.nextDouble();
System.out.print("IT: ");
double it = scanner.nextDouble();
System.out.print("TA: ");
double ta = scanner.nextDouble();
scanner.nextLine(); // Consume newline
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

employees[count] = new Employee(empNo, empName, dept, designation, hra,


it, ta);
count++;
} else {
System.out.println("Employee array is full.");
}
break;

case 2: // Find Employee


System.out.print("Enter Employee No to find: ");
int searchEmpNo = scanner.nextInt();
boolean found = false;
for (Employee emp : employees) {
if (emp != null && emp.empNo == searchEmpNo) {
emp.displayInfo();
System.out.printf("Total Salary: %.2f%n", emp.calculateTotalSalary());
found = true;
break;
}
}
if (!found) {
System.out.println("Employee not found.");
}
break;

case 3: // Show All Employees


System.out.println("\nEmployee Information:");
System.out.printf("%-10s %-20s %-15s %-15s %-10s %-10s %-10s%n", "Emp
No", "Emp Name", "Department", "Designation", "HRA", "IT", "TA");
System.out.println("-------------------------------------------------------------------------
-------");
for (Employee emp : employees) {
if (emp != null) {
emp.displayInfo();
}
}
break;

case 4: // Exit
scanner.close();
return;

default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output:

5. Learning Outcomes:
a) Understand how to use methods to perform specific tasks like displaying employee
details and calculating total salaries.
b) Recognize the importance of checking boundary conditions, such as ensuring the array
does not exceed its size when adding new employees.
c) Learnt how to handle different types of inputs such as integers, strings, and doubles using
methods like nextInt(), nextLine(), and nextDou ble().

You might also like