You are on page 1of 6

(Quiz – 1 Algorithim $ data structer)

STUDY PROGRAM D-IV


TEKNIK INFORMATIKA POLITEKNIK NEGERI MALANG
Jl. Soekarno Hatta No. 9 Malang 65144

Student name : Hammam abdullah Saeed B.G


Nim : ( 2341720203)
N.a : 12
Class : TI_I1

Code explanation text


1.
The “employee” class
public class employee {
String name;
int age;
int[] salary = new int[3];
int livingAllowance;
int transportationAllowance;

public class employee : declares a class named "employee", which will encapsulate the
attributes and behaviors.
String name: Declares a String variable named "name" to store the name of the employee.
int age: Declares an integer variable named "age" to store the age of the employee
int[] salarynew = int[3]; -Creates an integer array named "salary" with a length of 3.
int livingAllowance; - Declares an integer variable named "livingAllowance" to store the living
allowance provided to the employee.
int transportationAllowance; Declares an integer variable named "transportationAllowance" to
store the transportation allowance provided to the employee.
public employee(String name, int age, int[] salary, int livingAllowance, int
transportationAllowance) {
this.name = name;
this.age = age;
this.salary = salary;
this.livingAllowance = livingAllowance;
this.transportationAllowance = transportationAllowance;
it defines a constructor for the Employee class. It takes in parameters such as name (String), age
(int), etc…..
- Inside the parameter all the lines allows for the creation of Employee objects by
initializing their attributes with the values provided when the object is instantiated. It
sets the name, age, salary array, living allowance, and transportation allowance for each
Employee object.

- public double averageSalary() {


- double sum = 0;
- for (int i = 0; i < salary.length; i++) {
- sum += salary[i];
- }
- return sum / salary.length;
public double declares a method “averageSalary” with a return type of double.
double sum = 0 declares a variable “sum” with a data type of double
for (int i = 0; i < salary.length; i++) : it starts a for loop that iterates over the elements of the
salary array. It initializes the loop variable i to 0 and continues the loop as long as i is less than
the length of the salary array.
sum += salary[i];: This line adds the current element of the salary array to the sum variable
return sum / salary.length: returns the average salary by dividing the sum variable by the length
of the salary array
public double totalSalary() {
return averageSalary() + livingAllowance + transportationAllowance;
}
public void printData() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary[0] + ", " + salary[1] + ", "
+
public double totalSalary: declares a method `totalSalary` with a return type of double.
return averageSalary() + livingAllowance + transportationAllowance:
returns the total salary by adding the average salary (calculated by the `averageSalary` method),
the living allowance, and the transportation allowance.
public void printData() : declares a method named `printData` with a return type of void
System.out.println("Name: " + name),("Age: " + age), ("Salary: " + salary + ", " + salary + ", " +
salary)
It’s all to print the values usinh System.out.println metheod .

8. `System.out.println("Living Allowance: " + livingAllowance);`: This line prints the living


allowance of the employee using the `System.out.println` method.

salary[2]);
System.out.println("Living Allowance: " + livingAllowance);
System.out.println("Transportation Allowance: " +
transportationAllowance);
}
It prints the living , Transportation allowance of the employee using
the System.out.println method.

2. the “main class”

public static void main(String[] args) :declares the main method with a return type of void

int n = 5; :declares an integer variable named n and initializes it to 5.

employee[] employees = new employee[n];: declares an array of employee objects


named employees with a length of n.

After that I created a new employee objects with (name , age , salary array , living and transportation
allowences , and It assigns these objectes to the element of the employees array.

So I have choose random names and ages with salaries and allowance values (it’kinda not enough for a
human to live but its only examples :)

Then..
double totalSalarySum = 0;
int maxAllowance = 0;
employee maxAllowanceEmployee = null;
for (employee emp : employees) {
if (emp.age > 35) {
totalSalarySum += emp.totalSalary();
}
if (emp.age >= 35 && emp.age <= 50) {
int totalAllowance = emp.livingAllowance +
emp.transportationAllowance;
if (totalAllowance > maxAllowance) {
maxAllowance = totalAllowance;
maxAllowanceEmployee = emp;
}
}
}
double averageTotalSalary = totalSalarySum / n;
System.out.println("Average total salary of employees above 35: " +
averageTotalSalary);

if (maxAllowanceEmployee != null) {
System.out.println("Employee with the biggest allowance (35-50 years
old): ");
maxAllowanceEmployee.printData();
}
}
}

This code must calculates the average total salary of employees above 35 years old as well as the
employee with the biggest allowance (between 35 and 50 years old).

If the employee's age is greater than 35, their total salary is added to totalSalarySum.

If the employee's age is between 35 and 50, their living and transportation allowances are added
together to get their total allowance.

If this total allowance is greater than the current maximum allowance, the maximum allowance is
updated, and the current employee is stored in
“maxAllowanceEmployee”

( I will show it in the output )

After the loop completes the average total salary is calculated by dividing totalSalarySum by the total
number of employees.
So.. if maxAllowanceEmployee is not 0 , the employee's data is printed .

That’s it !

So in summary..

The first code “ employee “ defines a class named employee that represents an employee with
properties such as name, age, salary, living allowance, and transportation allowance.

It also has methods to calculate the average salary and total salary of the employee.

The second “employee main” code is a main class named employeemain that creates an array
of employee objects, initializes their properties, and performs some calculations.

It calculates the total salary of employees above 35 years old and finds the employee with the biggest
allowance between 35 and 50 years old. The code then prints the average total salary and the data of
the employee with the biggest allowance.

The output:

- it shows the average total of salary above 35 ( 769.0 )


- the biggest allowance of 35-50 years old employee. ( Hammam )
- his age (40)
- his salary , living and transportation allowance.

Thank you !
1.

https://github.com/hemoabdullah/semester2/blob/main/semester2-1/jobsheet3/employee.java

2.
https://github.com/hemoabdullah/semester2/blob/main/semester2-1/jobsheet3/employeemain.java

You might also like