package p5_SortedArrayAccess; import java.util.*; public class sortedArrayAccess
ID: 3745756 • Letter: P
Question
package p5_SortedArrayAccess;
import java.util.*;
public class sortedArrayAccess { public static double[] arr; private int arraySize;
public sortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public final double get(int i) { return arr[i]; }
public final int BinarySearch(double Key) { int k = 0; int lower = 0; int upper = arraySize - 1; while (lower < upper) { k = (lower + upper + 1) / 2; if (Key == arr[k]) { break; } if (Key < arr[k]) { upper = k - 1; } else { lower = k + 1; } } if (lower == upper) { k = lower; } if (Key == arr[k]) { return k; } else { System.out.println("The item cannot be found!"); return -1; } }
public final void insertion(double Key, double Item) { if (arraySize == 0) { arr[0] = Item; } /* find the position for interting the given item */ int position = 0; while (Key > arr[position] && position < arraySize) { position++; } for (int i = arraySize; i > position; i--) { arr[i] = arr[i - 1]; } arr[position] = Item; arraySize = arraySize + 1;
}
public final void deletion(double Key) { /* find the given item */ int position = BinarySearch(Key); if (position != -1) { for (int i = position; i < arraySize - 1; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public final void display() { if (arraySize != 0) { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("The number of items is " + arraySize); } } package p5_SortedArrayAccess;
import java.util.*;
public class sortedArrayAccess { public static double[] arr; private int arraySize;
public sortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public final double get(int i) { return arr[i]; }
public final int BinarySearch(double Key) { int k = 0; int lower = 0; int upper = arraySize - 1; while (lower < upper) { k = (lower + upper + 1) / 2; if (Key == arr[k]) { break; } if (Key < arr[k]) { upper = k - 1; } else { lower = k + 1; } } if (lower == upper) { k = lower; } if (Key == arr[k]) { return k; } else { System.out.println("The item cannot be found!"); return -1; } }
public final void insertion(double Key, double Item) { if (arraySize == 0) { arr[0] = Item; } /* find the position for interting the given item */ int position = 0; while (Key > arr[position] && position < arraySize) { position++; } for (int i = arraySize; i > position; i--) { arr[i] = arr[i - 1]; } arr[position] = Item; arraySize = arraySize + 1;
}
public final void deletion(double Key) { /* find the given item */ int position = BinarySearch(Key); if (position != -1) { for (int i = position; i < arraySize - 1; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public final void display() { if (arraySize != 0) { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("The number of items is " + arraySize); } } 4.3 Modify program P1-2 so that the array is of Employee type. The sorting key is the employee's id. The class Employee is given blow: Leport Java.util.Scanner public clas&eeployee public int id public String nane public double salary ublsc vold Input) Systes.out.printin( Enter nae:) ane-now Scannr(Systow.in).nextlne) Systes.out.printin Enter 10 ld Integer.parseint (now ScannertSystes.in) netLne)) Systee.out.printin Enter salary: alary-Double-parseboubleau Scanner(Syston.n) nextlinet)) blsc vold Output Systes.out.pr1nt4("turw: sass. ID: sai. Grade: *. nase, 4, salary); ublic String toString rsturn string-foreatC[Nane: co)D). Gr: (2),,1e, salary)i Add a testing class that can test all the operations defined in the sortedArrayAccess class.
Explanation / Answer
Hi,
PFB Code modified to include array of Employees :
SortedArrayAccess.java
package p5_SortedArrayAccess;
/**
* The Class SortedArrayAccess.
*/
public class SortedArrayAccess {
/** The employee array. */
public static Employee[] employeeArray;
/** The array size. */
private int arraySize;
/**
* Instantiates a new sorted array access.
*
* @param scale
* the scale
*/
public SortedArrayAccess(int scale) {
employeeArray = new Employee[scale];
arraySize = 0;
}
/**
* Gets the.
*
* @param i
* the i
* @return the employee
*/
public final Employee get(int i) {
return employeeArray[i];
}
/**
* Binary search.
*
* @param id
* the id
* @return the int
*/
public final int BinarySearch(int id) {
int k = 0;
int lower = 0;
int upper = arraySize - 1;
while (lower < upper) {
k = (lower + upper + 1) / 2;
if (id == employeeArray[k].id) {
break;
}
if (id < employeeArray[k].id) {
upper = k - 1;
} else {
lower = k + 1;
}
}
if (lower == upper) {
k = lower;
}
if (id == employeeArray[k].id) {
return k;
} else {
System.out.println("The item cannot be found!");
return -1;
}
}
/**
* Insertion.
*
* @param id
* the id
* @param employee
* the employee
*/
public final void insertion(double id, Employee employee) {
if (arraySize == 0) {
employeeArray[0] = employee;
}
/* find the position for inserting the given item */
int position = 0;
while (position < arraySize && id > employeeArray[position].id) {
position++;
}
for (int i = arraySize; i > position; i--) {
employeeArray[i] = employeeArray[i - 1];
}
employeeArray[position] = employee;
arraySize = arraySize + 1;
}
/**
* Deletion.
*
* @param id
* the id
*/
public final void deletion(int id) {
/* find the given item */
int position = BinarySearch(id);
if (position != -1) {
for (int i = position; i < arraySize - 1; i++) {
employeeArray[i] = employeeArray[i + 1];
}
arraySize = arraySize - 1;
}
;
}
/**
* Display.
*/
public final void display() {
if (arraySize != 0) {
for (int i = 0; i < arraySize; i++) {
System.out.println(employeeArray[i]);
}
}
;
System.out.println("The number of items is " + arraySize);
}
/**
* Find highest salary emp.
*
* @return the employee
*/
public final Employee findHighestSalaryEmp() {
Employee highestSalary = employeeArray[0];
for (Employee emp : employeeArray) {
if (emp != null) {
if (emp.salary > highestSalary.salary) {
highestSalary = emp;
}
}
}
return highestSalary;
}
/**
* Find avg salary.
*
* @return the double
*/
public final double findAvgSalary() {
double averageSalary = 0;
for (Employee emp : employeeArray) {
if (emp != null) {
averageSalary += averageSalary + emp.salary;
}
}
return averageSalary;
}
}
EmployeeTest.java
package p5_SortedArrayAccess;
import org.junit.Before;
import org.junit.Test;
/**
* The Class EmployeeTest.
*/
public class EmployeeTest {
/** The sorted array access. */
private SortedArrayAccess sortedArrayAccess = new SortedArrayAccess(4);
/** The emp 1. */
private Employee emp1 = new Employee(2, "A", 5000);
/** The emp 2. */
private Employee emp2 = new Employee(3, "B", 3000);
/** The emp 3. */
private Employee emp3 = new Employee(1, "C", 2500);
/** The emp 4. */
private Employee emp4 = new Employee(4, "D", 9500);
/**
* Before.
*/
@Before
public void before() {
sortedArrayAccess.insertion(1, emp1);
sortedArrayAccess.insertion(2, emp2);
sortedArrayAccess.insertion(3, emp3);
}
/**
* Test insertion.
*/
@Test
public void testInsertion() {
sortedArrayAccess.insertion(4, emp4);
System.out.println("TEST ::: After Insertion of new Employee : ");
sortedArrayAccess.display();
}
/**
* Test binary search.
*/
@Test
public void testBinarySearch() {
System.out.println("TEST ::: After Binary Search of Employee with Id 3: ");
int index = sortedArrayAccess.BinarySearch(3);
System.out.println("Employee with Id 3 found at Index " + index);
}
/**
* Test deletion.
*/
@Test
public void testDeletion() {
System.out.println("TEST ::: After Deletion of Emplpyee with Id 2: ");
sortedArrayAccess.deletion(1);
sortedArrayAccess.display();
}
/**
* Test find highest salary.
*/
@Test
public void testFindHighestSalary() {
System.out.println("TEST ::: Employee with Highest Salary : ");
Employee employee = sortedArrayAccess.findHighestSalaryEmp();
System.out.println(employee.toString());
}
/**
* Test find average salary.
*/
@Test
public void testFindAverageSalary() {
System.out.println("TEST ::: Average Salary of Employees : ");
double avgSalary = sortedArrayAccess.findAvgSalary();
System.out.println(avgSalary);
}
}
TEST CASE RESULT :
TEST ::: Average Salary of Employees :
21000.0
TEST ::: Employee with Highest Salary :
[Name: {A}, Id: {2}, Salary: {5000.0}]
TEST ::: After Insertion of new Employee :
[Name: {C}, Id: {1}, Salary: {2500.0}]
[Name: {B}, Id: {3}, Salary: {3000.0}]
[Name: {A}, Id: {2}, Salary: {5000.0}]
[Name: {D}, Id: {4}, Salary: {9500.0}]
The number of items is 4
TEST ::: After Binary Search of Employee with Id 3:
Employee with Id 3 found at Index 1
TEST ::: After Deletion of Emplpyee with Id 2:
[Name: {B}, Id: {3}, Salary: {3000.0}]
[Name: {A}, Id: {2}, Salary: {5000.0}]
The number of items is 2
Note that for Test cases, I have used JUNIT4. Please add the library of Junit4 in your project.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.