Question

IN JAVA: Write recursive method to return true if a given array has element equal to...

IN JAVA: 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)

{

}

Homework Answers

Answer #1

Program Code [JAVA]

// 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");
}
}

Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
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...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, 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. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
In c++ Implement the following recursive function, that returns true if all elements in the given...
In c++ Implement the following recursive function, that returns true if all elements in the given array are smaller than the given value, otherwise false. Return false for the empty array and NULL array cases. bool all_smaller_r(int *a1, int size, int value) { }
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an argument and it will return an array of chars. Each index of the String will be stored in each index of the array. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for * @return...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...