package edu; import java.util.List; import java.util.Optional; public class Sear
ID: 3749685 • Letter: P
Question
package edu;
import java.util.List;
import java.util.Optional;
public class Search {
/** Looks for the position of the named team in an array. */
public static Optional<Integer> findTeamPosition(final Team[] arr, final String key) {
// Gets the array size
final int size = arr.length;
// Runs through a for loop to check
for (int i = 0; i < size; i++) {
// Gets the current item at index and compare name to key
if (arr[i].getName().equals(key)) {
// Return the index of where the item with key is located
return Optional.of(i);
}
}
// If it does not exist in the array then return an index of -1
return Optional.empty();
}
/** Looks for the position of the named team in a list. */
public static Optional<Integer> findTeamPosition(final List<Team> list, final String key) {
// TODO complete this method
return Optional.empty();
}
/**
* Looks for the position of the poorest team that has at least
* the specified funding level.
* @pre arr is sorted
* @post arr[result].funding >= minFunding && for all 0 <= i < result : arr[i].funding < minFunding
*/
public static Optional<Integer> findTeamMinFunding(final Team[] arr, final int minFunding) {
// TODO complete this method
return Optional.empty();
}
/**
* Looks for the position of the poorest team that has at least
* the specified funding level.
* Uses binary search: Initially consider the entire index range,
* then repeatedly eliminate the wrong half of the array until
* zero or one items are left.
* @pre arr is sorted
* @post arr[result].funding >= minFunding && for all 0 <= i < result : arr[i].funding < minFunding
*/
public static Optional<Integer> findTeamMinFundingFast(final Team[] arr, final int minFunding) {
// TODO complete this method
// Gets the array size
final int size = arr.length;
// Initially consider the entire index range of the array
int low = 0;
int high = size - 1;
// Keep going as long as there is more than one item to be checked
// Eliminate the wrong half of the array
// Return current item only if it meets the condition!
if (low <= high && arr[low].getFunding() >= minFunding) {
return Optional.of(low);
} else {
return Optional.empty();
}
}
}
HOW do I do " // TODO complete this method" in the code?
Explanation / Answer
To give you the exact solution, please provide me the implementation of class Team. Without the implementation of Team class, I cannot provide you the exact implementation of findTeamPosition().
For time being, please find the modified code below.
CODE
=====================
package edu;
import java.util.List;
import java.util.Optional;
public class Search {
/** Looks for the position of the named team in an array. */
public static Optional<Integer> findTeamPosition(final Team[] arr, final String key) {
// Gets the array size
final int size = arr.length;
// Runs through a for loop to check
for (int i = 0; i < size; i++) {
// Gets the current item at index and compare name to key
if (arr[i].getName().equals(key)) {
// Return the index of where the item with key is located
return Optional.of(i);
}
}
// If it does not exist in the array then return an index of -1
return Optional.empty();
}
/** Looks for the position of the named team in a list. */
public static Optional<Integer> findTeamPosition(final List<Team> list, final String key) {
for(int i=0; i<list.size(); i++) {
if(key.equals(list.get(i).getName())) {
return Optional.of(i + 1);
}
}
return Optional.empty();
}
/**
* Looks for the position of the poorest team that has at least
* the specified funding level.
* @pre arr is sorted
* @post arr[result].funding >= minFunding && for all 0 <= i < result : arr[i].funding < minFunding
*/
public static Optional<Integer> findTeamMinFunding(final Team[] arr, final int minFunding) {
for(int i=1; i<arr.length; i++) {
if(arr[i-1].getFunding() < minFunding && arr[i].getFunding() >= minFunding) {
return Optional.of(i);
}
}
return Optional.empty();
}
/**
* Looks for the position of the poorest team that has at least
* the specified funding level.
* Uses binary search: Initially consider the entire index range,
* then repeatedly eliminate the wrong half of the array until
* zero or one items are left.
* @pre arr is sorted
* @post arr[result].funding >= minFunding && for all 0 <= i < result : arr[i].funding < minFunding
*/
public static Optional<Integer> findTeamMinFundingFast(final Team[] arr, final int minFunding) {
// TODO complete this method
// Gets the array size
final int size = arr.length;
// Initially consider the entire index range of the array
int low = 0;
int high = size - 1;
while(low <= high) {
int mid = (high + low) / 2;
if (arr[mid-1].getFunding() < minFunding && arr[mid].getFunding() >= minFunding) {
return Optional.of(mid);
} else if (arr[mid].getFunding() < minFunding) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return Optional.empty();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.