0% found this document useful (0 votes)
27 views25 pages

Experiment 1-6 FINALccc

Uploaded by

dhruvdixit8141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views25 pages

Experiment 1-6 FINALccc

Uploaded by

dhruvdixit8141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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 salary and average salary


double totalSalary = 0; for (double
salary : salaries)
{ totalSalary +=
salary;
}
double averageSalary = totalSalary / salaries.length;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

Output:

The average salary of the employees is: 25299.5


The number of employees having salary greater than the average is: 4
The number of employees having salary lesser than the average is: 6

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


1.3
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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

Program:
public class Chocolate
{
// Member variables
private int barCode;
private String name;
private double weight;
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
public int getBarCode()

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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
System.out.println("Default 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());
// Modify values using setter method
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


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

Output:

Default Values:
Bar Code: 1001
Name: Cadbury
Weight: 15g
Cost: $50

Modified Values:
Bar Code: 1002
Name: Hershey's
Weight: 30g
Cost: $95

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


2.5
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, salary, and 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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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

class ContractEmployee extends Employee


{
// Additional member variables for contract employee
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;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

// 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)
{

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


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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


3.7
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

+ 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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

Method Description: calcPercentage(): calculates the overall percentage of the marks of the trainee.

Output: The total aggregate percentage secured is XX.XX

Input :
(For Trainee):

Attribute Values Attributes Values

Marks Secure 4500


Marks Secured 5500

Grace Marks 400

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:

The total aggregate percentage secured is 61.25%


The total aggregate percentage secured is 68.75%
Please enter the correct marks
Please enter the correct marks

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

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;

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;
}
public void setEmpName(String empName)
{
this.empName = empName;
}
public int getEmpAge()
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
5.1
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-19), Bhauti, Kanpur-209305 (U.P.), India

{
return empAge;
}
public void setEmpAge(int empAge)
{
this.empAge = empAge;
}
public double getEmpSalary()
{
return empSalary;
}
public void setEmpSalary(double empSalary)
{
this.empSalary = empSalary;
}
}

public class EmpSalaryException extends Exception


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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

public class Service


{
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);
}
catch (EmpSalaryException e)
{
System.out.println(e.getMessage());
}
}
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

Output:

Employee with salary < 1000: John

Employee with salary < 1000: Alex

Employee with salary < 1000: Daniel

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


5.4
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
{
private String name;
private String course;
private int rollNumber;

public Student(String name, String course, int rollNumber)


{
this.name = name;
this.course = course;
this.rollNumber = rollNumber;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

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

public class Main


{
public static void main(String[] args)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


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

{
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: " + studentHashSet.add(student1)); // 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);
}
}
}

Output:

Adding student1: true


Adding student2: true
Adding student3: true
Adding student4: true
Adding student5 (duplicate): false
Students in the HashSet:
Student{name='John Doe', course='Computer Science', rollNumber=101}
Student{name='Jane Smith', course='Mathematics', rollNumber=102}
Student{name='Emily Davis', course='Physics', rollNumber=103}
Student{name='Michael Brown', course='Chemistry', rollNumber=104

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


6.3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy