LAB 3
LINKEDLIST
AHMAD ZUNNURAIN BIN ALIAS (2023466072)
1. Write a program that will do the following:
a. Use LinkedList class from Collection Framework to store 5 integer values.
b. Calculate the total of all value in the list.
c. Find the largest number and display it.
d. Display all odd number in the list and add it into a new linkedList object named
oddList.
2. Given the following ADTs,
Write a program that will do the following:
a) Store 30 car objects into a linked list named carList.
b) Calculate and display total price of Proton cars.
c) All cars that have been purchased in year 2010 are given 15% discount. Calculate the
new price for each car and update the original price with the new price.
LAB 3
LINKEDLIST
Assuming that all information about contract workers has been stored in a linked list cwList,
write program that will do the followings:
a) Count and display the number of female and male contract worker.
public class Main {
public static void main(String[] args) {
LinkedList<ContractWorker> workerList = new LinkedList<>();
// Add contract workers to the list
workerList.insertAtFront(new ContractWorker("yana", 6, 'F', 2500));
workerList.insertAtFront(new ContractWorker("badrul", 12, 'M', 3000));
workerList.insertAtFront(new ContractWorker("kimiy", 6, 'M', 2800));
workerList.insertAtFront(new ContractWorker("kirana", 9, 'F', 2700));
// Count and display the number of female and male contract workers
int femaleCount = 0;
int maleCount = 0;
LinkedList.ListNode<ContractWorker> current = workerList.firstNode;
while (current != null) {
ContractWorker currentWorker = current.data;
if (currentWorker.getGender() == 'F') {
femaleCount++;
} else if (currentWorker.getGender() == 'M') {
maleCount++;
}
current = current.nextNode;
}
System.out.println("Number of female contract workers: " + femaleCount);
System.out.println("Number of male contract workers: " + maleCount);
}
b) Display the information of the contract worker who has the highest salary.
// Find the contract worker with the highest salary
ContractWorker highestPaidWorker = findHighestPaidWorker(workerList);
// Display information of the contract worker with the highest salary
if (highestPaidWorker != null) {
System.out.println("Contract worker with the highest salary:");
System.out.println(highestPaidWorker);
} else {
System.out.println("No contract workers found.");
}
c) Calculate and display the total salary of the contract worker who has signed contract
for not more than 120 months.
// Calculate and display total salary of contract workers with contract duration <= 120
months
double totalSalary = calculateTotalSalary(workerList);
System.out.println("Total salary of contract workers with contract duration not
more than 120 months: " + totalSalary);
}
public static double calculateTotalSalary(LinkedList<ContractWorker> workerList) {
double totalSalary = 0;
LinkedList.Node<ContractWorker> current = workerList.firstNode;
while (current != null) {
ContractWorker worker = current.data;
if (worker.getMonthOfContract() <= 120) {
totalSalary += worker.getSalary();
}
current = current.nextNode;
}
return totalSalary;
}
public static class ContractWorker {
private String name;
private int monthOfContract;
private char gender;
private double salary;
public ContractWorker(String name, int monthOfContract, char gender, double
salary) {
this.name = name;
this.monthOfContract = monthOfContract;
this.gender = gender;
this.salary = salary;
}