Question

In: Computer Science

Write recursive method to return true if a given array has element equal to employee emp,...

Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not.

//PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary,

// and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns

// false otherwise. An empty array returns false. Method employeeInRec must be recursive and returns true if array csEmployees has employee emp, and returns false otherwise.

public boolean employeeInRec (Employee[] csEmployees, int n, Employee emp)

{

}

Solutions

Expert Solution

Programming language:- JAVA

SOURCE CODE:-

// Since Employee class is not given we are making our own

class Employee {

private String firstName;
private String lastName;
private int numHours;
private double hourlyPay;

// Constructor

public Employee(String firstName, String lastName, int numHours, double hourlyPay) {
this.firstName = firstName;
this.lastName = lastName;
this.numHours = numHours;
this.hourlyPay = hourlyPay;
}

public boolean equals(Employee emp) {

return ((this.firstName.equalsIgnoreCase(emp.firstName)) && (this.lastName.equalsIgnoreCase(emp.lastName)) && (this.numHours == emp.numHours) && (this.hourlyPay == emp.hourlyPay));
}

public double getSalary() {
return numHours * hourlyPay;
}
}

class Main {

// employeeInRec method

public static boolean employeeInRec(Employee[] csEmployees, int n, Employee emp) {

if (n == 0)
return false;

if(csEmployees[n-1].equals(emp))
return true;
else
return employeeInRec(csEmployees, n-1, emp);

}

public static void main(String[] args) {

// Testing employeeInRec method

Employee[] csEmployees = new Employee[3];
csEmployees[0] = new Employee("John", "Clint", 5, 20);
csEmployees[1] = new Employee("Sarah", "Khan", 7, 30);
csEmployees[2] = new Employee("Roxy", "Rush", 8, 25);

Employee emp = new Employee("Sarah", "Khan", 7, 30);

if(employeeInRec(csEmployees, 3, emp))
System.out.println("True: emp is in csEmployees array");
else
System.out.println("False: emp is not in csEmployees array");
}
}

OUTPUT:-

Please upvote if you found this answer useful or drop-in the comment's section your queries. I will try to assist you with the best possible solution.


Related Solutions

Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
php please // 5) isArraySorted // Given an array , return true if the element are...
php please // 5) isArraySorted // Given an array , return true if the element are in ascending order. // Note: 'abe' < 'ben' and 5 > 3 // Precondition $arr has all the same type of elements function isArraySorted($arr) {    } echo PHP_EOL . 'isArraySorted(array(1, 2, 2, 99) 1: ' . isArraySorted ( array ( 1, 2, 2, 99 ) ) . "\n"; assert ( isArraySorted ( array ( 1, 2, 2, 99 ) ) ); echo 'isArraySorted(array(2,...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT