/*
Name : NUR DIYANA BINTI AB AZIZ
Sid# : 2020844414
Course: CSC508
Group#: CS2304C
Assign#: #1
Due Date: 23 April 2021 (Friday before 12 AM)
Program Description: Lab Assignment #1 ( Arraylist)
*/
EMPLOYEE CLASS
public class Employee{
public String empName;
public String id;
public double salary;
public Employee(){}
public Employee(String name, String id, double salary)
[Link]= name;
[Link] = id;
[Link] = salary;
//accessor
public String getName()
return empName;
}
public String getID()
return id;
public double getSalary()
return salary;
//mutator
public void setEmpName(String name)
[Link] = name;
public void setID(String id)
[Link] = id;
public void setSalary(double salary)
[Link] = salary;
public String toString()
return "Name: "+ empName + " ID: "+id + " Salary(RM): " +salary;
}
ARRAYLIST CLASS
public class MyArrayList
// default initial capacity
private static final int INITIAL_CAPACITY = 50;
private Object[] theData; // the array to hold the list elements
private int size = 0; // the current size
private int capacity = 0; // the current capacity
//Default constructor
public MyArrayList()
theData = new Object[INITIAL_CAPACITY];
capacity = INITIAL_CAPACITY;
public boolean isEmpty() {
return size == 0;
//exercise 3
//method isFull()
public boolean isFull()
if(size >= capacity)
return true;
else return false;
//Return the number of elements in this list
public int size() {
return size;
//insert front
public void insertAtFront(Object theValue)
if(size >= capacity) //the list is full
[Link]("Cannot insert in a full list.");
else
for (int i = size; i > 0; i--)
theData[i] = theData[i-1]; // move elements down
theData[0] = theValue; //insert the item at front
size++; //increment the size
} //end add
//insert at back
public void insertAtBack(Object theValue)
if(size >= capacity) //the list is full
[Link]("Cannot insert in a full list.");
else
theData[size] = theValue; //insert the item at front
size++; //increment the size
}
//GET OBJECT DATA
public Object get(int index)
if(index < 0 || index >= size)
throw new ArrayIndexOutOfBoundsException(index);
else
return theData[index];
} //end get
// display the elements of the list
public void display()
for ( int i = 0; i < size; i++)
[Link](theData[i]);
[Link]();
}
MAIN CLASS
import [Link];
public class mainEmployee{
public static void main(String[] args)
MyArrayList empList = new MyArrayList();
MyArrayList empHigh = new MyArrayList();
double avg=0,total=0;
Employee [] emp = new Employee[5];
//input data
for(int i =0; i<[Link];i++){
Scanner sc = new Scanner([Link]);
[Link]("Enter Your Name:");
String name =[Link]();
[Link]("Enter Your ID:");
String id =[Link]();
[Link]("Enter Your Salary:");
double salary =[Link]([Link]());
emp[i] = new Employee(name,id,salary);
[Link](emp[i]);
//Employee that has salary more than 5000 will store in empHigh list
for(int j =0; j<[Link]();j++)
if(emp[j].getSalary()>5000)
[Link]([Link](j));
}
for(int t =0; t<[Link]();t++)
//total salary
total += emp[t].getSalary();
double max=emp[0].getSalary(),min=emp[0].getSalary();
for(int m=1;m<[Link]();m++)
if(emp[m].getSalary()>max)
max = emp[m].getSalary();
if(emp[m].getSalary()<min)
min = emp[m].getSalary();
//[Link] average salary
avg = total/[Link]();
//display all employee detail
[Link]("Detail in empList:");
[Link]();
//display total salary
[Link]("Detail in empHigh:");
[Link]();
[Link]("Total salary(RM):"+total);
[Link]("Average salary of "+[Link]()+" workers (RM):"+avg);
[Link]("Minimum salary of (RM):"+min);
[Link]("Maximum salary of (RM):"+max);
}
OUTPUT