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

Java Exp File_watermark

Uploaded by

2k22.csds.32825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Exp File_watermark

Uploaded by

2k22.csds.32825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PSIT-Pranveer Singh Institute of Technology

Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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:

public class EmployeeRecord {


public static void main(String[] args) {
double[] salaries = {23500.0, 25080.0, 28760.0, 22340.0, 19890.0, 23555.0, 25980.0,
29760.0, 23340.0, 29890.0};
// Calculate total salaryUday Vimal
and average 2201641540116
salary
double totalSalary = 0;
for (double salary : salaries) {
totalSalary += salary;
}
double averageSalary = totalSalary / salaries.length;
// 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++;
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

// 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);
}
}

Uday Vimal 2201641540116

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

private double cost;


// Default constructor
public Chocolate() {
this.barCode = 1001;
this.name = "Cadbury";
this.weight = 15;
this.cost = 50;
}
// Parameterized constructor
public Chocolate(int barCode, String name, double weight, double cost) {
this.barCode = barCode;
this.name = name;
this.weight = weight;
this.cost = cost;
}
// Getter methods Uday Vimal 2201641540116
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) {

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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();

// Display default values using getter methods


UdayValues:");
System.out.println("Default Vimal 2201641540116
System.out.println("Bar Code: " + chocolate1.getBarCode());
System.out.println("Name: " + chocolate1.getName());
System.out.println("Weight: " + chocolate1.getWeight() + "g");
System.out.println("Cost: $" + chocolate1.getCost());
// Modify values using setter methods
chocolate1.setBarCode(1002);
chocolate1.setName("Hershey's");
chocolate1.setWeight(30);
chocolate1.setCost(95);
// Display modified values using getter methods
System.out.println("\nModified Values:");
System.out.println("Bar Code: " + chocolate1.getBarCode());
System.out.println("Name: " + chocolate1.getName());
System.out.println("Weight: " + chocolate1.getWeight() + "g");
System.out.println("Cost: $" + chocolate1.getCost());
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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

A record of the employee name,


Udaysalary,
Vimal and2201641540116
the employee Id has to be maintained for each
employee. PermanentEmployee:
PermanentEmployee
basicPay : double
hra : double
experience : int
getBasicPay() : double
setBasicPay(salary:double) : void
getHra() : double
setHra(empId :double) : void
getExperience() : int
setExperience(name : int) : void
calculateSalary() : 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:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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 {

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

// 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;
}

public void setSalary(double salary) {


this.salary = salary;
}
}
class PermanentEmployee extends Employee {
// Additional member variables for permanent employees
private double basicPay;
private double HRA;
Uday Vimal 2201641540116
private int experience;
// 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;
}
// Method to calculate salary for permanent employees
public void calculateSalary() {
double variableComponent;
if (experience < 5)
variableComponent = 0;
else if (experience < 7)
variableComponent = 0.05 * basicPay;
else if (experience < 12)
variableComponent = 0.10 * basicPay;
else
variableComponent = 0.15 * basicPay;

double salary = variableComponent + basicPay + HRA;


setSalary(salary);
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

class ContractEmployee extends Employee {


// Additional member variables for contract employees
private double wages;
private int hours;

// 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());
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

Marks Secured 5500

Program:
> Employee.java
public class Employee { private String empName; private int empAge; private double empSalary;

public Employee(String empName, int empAge, double empSalary) {


this.empName = empName;
this.empAge = empAge; this.empSalary = empSalary;
}

// Getters and setters


public String getEmpName() {
return empName; Uday Vimal 2201641540116
}

public void setEmpName(String empName) {


this.empName = empName;
}

public int getEmpAge() {


return empAge;
}

public void setEmpAge(int empAge) {


this.empAge = empAge;
}

public double getEmpSalary() {


return empSalary;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

public void setEmpSalary(double empSalary) {


this.empSalary = empSalary;
}
}

>EmpSalaryException.java

public class EmpSalaryException extends Exception {


public EmpSalaryException(String message) {
super(message);
}
}

> 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());
}
}

public static void main(String[] args) {


Employee emp1 = new Employee("Alice", 30, 1500);
Employee emp2 = new Employee("Bob", 25, 900);
Employee emp3 = new Employee("Charlie", 28, 1200);
Employee emp4 = new Employee("David", 22, 800);
Employee emp5 = new Employee("Eve", 35, 1100);
Employee[] employees = { emp1, emp2, emp3, emp4, emp5 };
for (Employee emp : employees) {
try {
checkEmployeeSalary(emp);

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

}
catch (EmpSalaryException e) {
System.out.println(e.getMessage());
}
}
}
}

Uday Vimal 2201641540116

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

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:

import java.util.HashSet; import java.util.Objects;


class Student {
Uday
private String name; private String Vimal
course; 2201641540116
private int rollNumber;
public Student(String name, String course, int rollNumber) { this.name = name;
this.course = course; this.rollNumber = rollNumber;
}

@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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

public int hashCode() {


return Objects.hash(rollNumber);
}
}

public class Main {


public static void main(String[] args) {
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

// Adding students to the HashSet

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)

// Printing the HashSet


for (Student student : studentHashSet) {
System.out.println(student);
}
}
}

public class Employee { private String empName; private int empAge; private double empSalary;

public Employee(String empName, int empAge, double empSalary) {


this.empName = empName;
this.empAge = empAge; this.empSalary = empSalary;
}

// Getters and setters

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

public String getEmpName() {


return empName;
}

public void setEmpName(String empName) {


this.empName = empName;
}

public int getEmpAge() {


return empAge;
}

public void setEmpAge(int empAge) {


this.empAge = empAge;
}
Uday Vimal 2201641540116
public double getEmpSalary() { return empSalary;
}

public void setEmpSalary(double empSalary) { this.empSalary = empSalary;


}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452

You might also like