Java Exp File_watermark
Java Exp File_watermark
Experiment No. 1
Objective:. Write a program to track of 10 employees in the project and their salaries, and also
find out the average of their salaries. They also want to find the number of employees who get
a salary greater than the average salary and those who get lesser. Consider that the salaries are
stored in an array of double variables as given below: double salary[] = {23500.0, 25080.0,
28760.0, 22340.0, 19890.0, 23555.0, 25980.0, 29760.0, 23340.0, 29890.0} Create a class
EmployeeRecord and write a program to implement the above requirement. Refer to the
output given below: The average salary of the employee is : XXXXXX The number of
employees having salary greater than the average is : Y The number of employees having salary
lesser than the average is : Z.
Program:
// Output results
System.out.println("The average salary of the employees is: " + averageSalary);
System.out.println("The number of employees having salary greater than the average is:
" + aboveAverageCount);
System.out.println("The number of employees having salary lesser than the average is: "
+ belowAverageCount);
}
}
Experiment No. 2
Objective:. Create a class Chocolate, with a parameterized constructor and a default
constructor. Also, use the "this" keyword while initializing member variables within the
parameterized constructor. Every chocolate which is manufactured will be with a default
weight and cost. The cost and weight might be modified later based on business needs.
Constructor Description: Chocolate( int barCode, String name, double weight, double cost): In
the constructor initialize the member variables, barCode, name, weight, and cost, according to
the table given below:
Attributes Values
barCode 1001
name Cadbury
weight 15
cost 50
Uday Vimal 2201641540116
Use setter methods to modify the values as given below:
Attributes Values
barCode 1002
name Hersheys
weight 30
cost 95
Use the below skeleton code for the Tester class main method and do the required
implementation. public class ChocolateTester{ public static void main (String[] args){ //Create
an object of chocolate //Use getter methods to display the values //Use setter methods to modify
the values //Use getter methods to display the modified values } }
Program:
public class Chocolate {
// Member variables
private int barCode;
private String name;
private double weight;
this.name = name;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setCost(double cost) {
this.cost = cost;
}
}
public class ChocolateTester {
public static void main(String[] args) {
// Create an object of chocolate using default constructor
Chocolate chocolate1 = new Chocolate();
Experiment No. 3
Objective:. A company wants to keep a record of the employees working in it. There are
permanent employees as well as contract employees. Contract employees work on an hourly
basis whereas permanent employees are paid monthly salary. The class diagrams are as given
below: Employee:
Employee
empId : int
name : String
salary : double
getSalary() : double
setSalary(salary:double) : void
getEmpId() : int
setEmpId(empId :int) : void
getName() : String
setName(name : String) : void
Method Description:
calculateSalary():This method calculates the salary using the formula as given below:
Salary = variable component + Basic pay +HRA
The condition for calculating variable component is given below:
Experience (in years) % of Basic pay
<5 0
>= 5 and < 7 5
>= 7 and < 12 10
>= 12 15
ContractEmployee:
ContractEmployee
wages : double
hours : int
getWages(): double
setWages(wages: double): void
getHours(): int
setHours(hours: int): void
calculateSalary(): void
Method Description:
calculateSalary():This method calculates the salary using the formula as given below: Salary =
total hours * wages
Implementation Details:
Create a class EmployeeRecords and implement the main method: public class
EmployeeRecords {
Public static void main(String str[]){
{
}
}
Provide the following inputs:
Uday Vimal 2201641540116
Input (For PermanentEmployee):
Attributes Values
Name Abc
Employee Id 1001
Basic Pay 12500
HRA 3500
Experience 6
Output: Permanent Employee: Your Salary is : XXXX
Input (For ContractEmployee):
Attributes Values
Name Xyz
Employee Id 7001
Wages 750
Hours 15
Output: Contract Employee: Your Salary is : XXXX
Note:- You call setter and getter method of salary of Employee class using an instance of both
Permanent as well as Contract Employee. The getter and setter method of instance variable in
parent class can be reused by child classes as it is inherited from parent and hence need not be
created again.
Program:
class Employee {
// Member variables
private String name;
private int employeeId;
private double salary;
// Constructor
public Employee(String name, int employeeId) {
this.name = name;
this.employeeId = employeeId;
}
// Getter and setter methods
public double getSalary() {
return salary;
}
// Constructor
public ContractEmployee(String name, int employeeId, double wages, int hours) {
super(name, employeeId);
this.wages = wages;
this.hours = hours;
}
// Method to calculate salary for contract employees
public void calculateSalary() {
double salary = wages * hours;
setSalary(salary);
}
}
public class EmployeeRecords {
public static void main(String[] args) {
Uday Vimal 2201641540116
// Creating a PermanentEmployee object and calculating salary
PermanentEmployee permanentEmployee = new PermanentEmployee("Abc", 1001,
12500, 3500, 6);
permanentEmployee.calculateSalary();
System.out.println("Permanent Employee: Your Salary is: $" +
permanentEmployee.getSalary());
// Creating a ContractEmployee object and calculating salary
ContractEmployee contractEmployee = new ContractEmployee("Xyz", 7001, 750,
15);
contractEmployee.calculateSalary();
System.out.println("Contract Employee: Your Salary is: $" +
contractEmployee.getSalary());
}
}
Experiment No. 4
Objective:. The Provider as interface, provides an efficient way for students to calculate their overall
percentage of all the courses. The total Maximum Marks for all courses is 8000. Intern studied in
Institute A, where each semester comprises of 1000 marks, in which 900 marks are for courses and 100
marks are kept for co-curricular activities (grace Marks). Whereas Trainee studied in Institute B where
each semester comprises of 1000 marks for courses.
Provider (interface)
+ totalMaximumMarks: int
+ calcPercentage(): void
Intern: This class for interns who completed their course in Institute A.
Intern (class)
- marksSecured: int
- graceMarks: int
Uday Vimal 2201641540116
+ Intern(marksSecured:int, graceMarks: int)
+ calcPercentage(): void
Method Description: calcPercentage(): This method will calculate the total marks of the intern which
is the sum of grace marks and the marks secured by the intern, and hence calculate the
percentage on the totalMaximumMarks.
Trainee: This class is for trainees who completed their course in Institute B.
Trainee (class)
- marksSecured: int
+ Trainee(marksSecured: int)
+calcPercentage(): void
Method Description: calcPercentage(): calculates the overall percentage of the marks of the trainee.
Attribute Values
Output: The total aggregate Marks Secure 4500 percentage secured is XX.XX Input
(For Trainee): Grace Marks 400
Attributes Values
Program:
> Employee.java
public class Employee { private String empName; private int empAge; private double empSalary;
>EmpSalaryException.java
> Service.java
public class Service { Uday Vimal 2201641540116
public static void checkEmployeeSalary(Employee emp) throws EmpSalaryException {
if (emp.getEmpSalary() < 1000) {
throw new EmpSalaryException("Salary is less than 1000 for employee: " +
emp.getEmpName());
}
}
}
catch (EmpSalaryException e) {
System.out.println(e.getMessage());
}
}
}
}
Experiment No. 6
Objective:. Create a Java program that adds Student objects into a HashSet. The Student class has
name, course and rollNumber as its attributes and a toString() method. If we add two Student objects
having the same rollNumber to the HashSet, it should be considered as duplicate and should not get
added.
Student (class)
- name: String
- course : String
- rollNumber : int
+ Student(name: String, course: String, rollNumber:int)
+ toString() : String
+ add() : boolean
Program:
@Override
public String toString() { return "Student{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", rollNumber=" + rollNumber + '}';
}
@Override
public boolean equals(Object o) { if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o;
return rollNumber == student.rollNumber;
}
@Override
Student student1 = new Student("Alice", "Math", 101); Student student2 = new Student("Bob",
"Science", 102);
Student student3 = new Student("Charlie", "Math", 101); // Duplicate rollNumber
System.out.println("Adding student1:
Uday "Vimal
+ studentHashSet.add(student1));
2201641540116 // Should print true
System.out.println("Adding student2: " + studentHashSet.add(student2)); // Should print true
System.out.println("Adding student3: " + studentHashSet.add(student3)); // Should print false
(duplicate rollNumber)
public class Employee { private String empName; private int empAge; private double empSalary;