Experiment 1-6 FINALccc
Experiment 1-6 FINALccc
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:
// Count employees with salary greater than average and those with salary lesser than
average
int aboveAverageCount = 0; int
belowAverageCount = 0; for
(double salary : salaries)
{ if (salary >
averageSalary)
{
aboveAverageCount++;
}
else if (salary < averageSalary)
{
belowAverageCount++;
}
}
// 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);
}
}
Output:
Experiment No. 2
Attributes Values
barCode 1001
name Cadbury
weight 15
cost 50
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;
private double cost;
// Getter methods
public int getBarCode()
{
return barCode;
}
public String getName()
{ return
name;
}
public double getWeight()
{
return weight;
}
public double getCost()
{
return cost;
}
// Setter methods
public void setBarCode(int barCode)
{
this.barCode = barCode;
}
public void setName(String name)
{
this.name = name;
}
public void setWeight(double weight)
{
this.weight = weight;
}
Output:
Default Values:
Bar Code: 1001
Name: Cadbury
Weight: 15g
Cost: $50
Modified Values:
Bar Code: 1002
Name: Hershey's
Weight: 30g
Cost: $95
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
getSalary() : double
setSalary(salary:double) : void
getEmpId() : int setEmpId(empId
:int) : void getName() : String
setName(name : String) : void
A record of the employee name, salary, and the employee Id has to be maintained for each
employee. PermanentEmployee:
PermanentEmployee
basicPay : double hra : double
experience : int
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
>= 12 15
ContractEmployee:
ContractEmployee
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:
Name Abc
Employee Id 1001
HRA 3500
Experience 6
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;
}
// Constructor
public PermanentEmployee(String name, int employeeId, double basicPay, double HRA,
int experience)
{
super(name, employeeId);
this.basicPay = basicPay;
this.HRA = HRA;
this.experience = experience;
}
}
}
Output:
Permanent Employee:
Name:Abc
Employee ID:1001
Your Salary is: $16250.0
Contract Employee:
Name:Xyz
Employee ID:7001
Your Salary is: $11250.0
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
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.
Input :
(For Trainee):
Program:
public interface Provider {
int totalMaximumMarks = 8000;
void calcPercentage();
}
public class Intern implements Provider {
private int marksSecured;
private int graceMarks;
public Intern(int marksSecured, int graceMarks) {
this.marksSecured = marksSecured;
this.graceMarks = graceMarks;
}
@Override
public void calcPercentage() {
if (marksSecured < 0 || marksSecured > 7000 || graceMarks < 0 || graceMarks > 1000) {
System.out.println("Please enter the correct marks");
} else {
int totalMarks = marksSecured + graceMarks;
double percentage = (double) totalMarks / totalMaximumMarks * 100;
System.out.printf("The total aggregate percentage secured is %.2f%%\n", percentage);
}
}
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
4.2
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India
Attributes Values
Marks Secured 5500
Attributes Values
Marks Secured 8000
Grace Marks 500
public class Main {
public static void main(String[] args) {
// Test Intern
Intern intern = new Intern(4500, 400);
intern.calcPercentage();
// Test Trainee
Trainee trainee = new Trainee(5500);
trainee.calcPercentage();
// Test Intern with incorrect marks
Intern invalidIntern = new Intern(8000, 500);
invalidIntern.calcPercentage();
// Test Trainee with incorrect marks
Trainee invalidTrainee = new Trainee(9000);
invalidTrainee.calcPercentage();
}
}
Output:
Experiment No. 5
Objective:
1. Create a class "Employee" with data members “empName",“empAge",“empSalary".
2. Add appropriate getter/setter methods for the data members.
3. Create an Exception class “EmpSalaryException" with an appropriate constructor.
4. Create a class Service with the main() method. CO1
5. Write a method checkEmployeeSalary(Employee emp) in Service, which checks if
empSalary< 1000
Program:
public class Employee
{
private String empName;
private int empAge;
private double empSalary;
{
return empAge;
}
public void setEmpAge(int empAge)
{
this.empAge = empAge;
}
public double getEmpSalary()
{
return empSalary;
}
public void setEmpSalary(double empSalary)
{
this.empSalary = empSalary;
}
}
Output:
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 public
int hashCode()
{ return
Objects.hash(rollNumber);
}
}
{
HashSet<Student> studentHashSet = new HashSet<>();
Student student1 = new Student("Alice", "Math", 101); Student student2 = new Student("Bob",
"Science", 102);
Student student3 = new Student("Charlie", "Math", 101); // Duplicate rollNumber
Output: