exp_1
exp_1
Experiment 1
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 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;
}
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
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().