0% found this document useful (0 votes)
312 views9 pages

Assignment 1 Array List

This document contains code for an Employee class that stores employee name, ID, and salary. It also contains a MyArrayList class that implements basic array list functionality like insertion, size checking, and retrieval. The main class prompts the user for employee data, stores employees in lists based on salary, calculates total salary, average salary, minimum salary, and maximum salary, and displays the results.

Uploaded by

SushyYaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
312 views9 pages

Assignment 1 Array List

This document contains code for an Employee class that stores employee name, ID, and salary. It also contains a MyArrayList class that implements basic array list functionality like insertion, size checking, and retrieval. The main class prompts the user for employee data, stores employees in lists based on salary, calculates total salary, average salary, minimum salary, and maximum salary, and displays the results.

Uploaded by

SushyYaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

/*

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)

this.empName= name;

this.id = id;

this.salary = salary;

//accessor

public String getName()

return empName;

}
public String getID()

return id;

public double getSalary()

return salary;

//mutator

public void setEmpName(String name)

this.empName = name;

public void setID(String id)

this.id = id;

public void setSalary(double salary)

this.salary = 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

System.err.println("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

System.err.println("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++)

System.out.println(theData[i]);

System.out.println();

}
MAIN CLASS

import java.util.Scanner;

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<emp.length;i++){

Scanner sc = new Scanner(System.in);

System.out.print("Enter Your Name:");

String name =sc.nextLine();

System.out.print("Enter Your ID:");

String id =sc.nextLine();

System.out.print("Enter Your Salary:");

double salary =Double.parseDouble(sc.nextLine());

emp[i] = new Employee(name,id,salary);

empList.insertAtBack(emp[i]);

//Employee that has salary more than 5000 will store in empHigh list

for(int j =0; j<empList.size();j++)

if(emp[j].getSalary()>5000)

empHigh.insertAtBack(empList.get(j));

}
for(int t =0; t<empList.size();t++)

//total salary

total += emp[t].getSalary();

double max=emp[0].getSalary(),min=emp[0].getSalary();

for(int m=1;m<empList.size();m++)

if(emp[m].getSalary()>max)

max = emp[m].getSalary();

if(emp[m].getSalary()<min)

min = emp[m].getSalary();

//d.calculate average salary

avg = total/empList.size();

//display all employee detail

System.out.println("Detail in empList:");

empList.display();

//display total salary

System.out.println("Detail in empHigh:");

empHigh.display();

System.out.println("Total salary(RM):"+total);

System.out.println("Average salary of "+empList.size()+" workers (RM):"+avg);

System.out.println("Minimum salary of (RM):"+min);

System.out.println("Maximum salary of (RM):"+max);

}
OUTPUT

You might also like