Question: Update: these are the 3 classes: import java.util.Optional; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(final String[] args) {

Update: these are the 3 classes:

import java.util.Optional; import java.util.Arrays; import java.util.List; import java.util.Scanner;

public class Main {

public static void main(final String[] args) { final Team t1 = new Team("USA", "Sarachan", 500); final Team t2 = new Team("Chile", "Rueda", 600); final Team t3 = new Team("Germany", "Lw", 700); final Team[] array = {t1, t2, t3}; final List list = Arrays.asList(array);

final Scanner keyboard = new Scanner(System.in);

// Get team name System.out.print("Enter name to search: "); final String key = keyboard.nextLine(); System.out.println("Looking for team " + key);

// Runs the linear search on the array final Optional index1 = Search.findTeamPosition(array, key); checkTeamArrayIndex(array, index1); // Runs the linear search on the list final Optional index2 = Search.findTeamPosition(list, key); checkTeamListIndex(list, index2);

// Get funding level System.out.print("Enter min funding to search: "); final String fundingString = keyboard.nextLine(); final int funding = Integer.parseInt(fundingString); System.out.println("Looking for min funding " + funding); // Runs the linear search on the array final Optional index3 = Search.findTeamMinFunding(array, funding); checkTeamArrayIndex(array, index3);

// Runs the binary search on the array final Optional index4 = Search.findTeamMinFundingFast(array, funding); checkTeamArrayIndex(array, index4); } static void checkTeamArrayIndex(final Team[] array, final Optional index) { // Checks the index if (index.isPresent()) { System.out.println("Found!"); final int pos = index.get(); final Team team = array[pos]; checkTeamListIndex(Arrays.asList(array),index); } else { System.out.println("NotFound!"); } } static void checkTeamListIndex(final List list, final Optional index) { // Checks the index if (index.isPresent()){ System.out.println("Found"); final int pos =index.get(); final Team team =list.get(pos); // TODO DRY - eliminate this code duplication System.out.println("Name: " + team.getName()); System.out.println("Head coach: " + team.getHeadcoach()); System.out.println("Funding: " + team.getFunding()); System.out.println("Array index: " + pos); System.out.println("Ranking: " + (pos + 1)); } else { System.out.println("Not Found!"); } } }

------------------------------------------------------------------------------------------------------------------------------.

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 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); // means sucsess } } // If it does not exist in the array then return an index of -1 return Optional.empty();// means its a fialure, cuz it couldn't find the match }

/** Looks for the position of the named team in a list. */ // Optional genraic meaning there is a value attached to it " Generic in the element type" // Why not bolean, bucause bolean doesnt allow us to find an interger // why not int, "we could get away with it if its >=0 && <0 is a failure"> findTeamPosition(final List list, final String key) { // TODO complete this method for (int i=0; i< list.size(); i++){ if (list.get (i).equals(key)) { return Optional.of(i); } } 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 findTeamMinFunding(final Team[] arr, final int minFunding) { // TODO complete this method // for loop for each team "liner search" for (int i =1; i= 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 findTeamMinFundingFast(final Team[] arr, final int minFunding) { // TODO complete this method //** the thing on the white bored Binary search // 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){ return Optional.of(mid); } else if(arr[mid].getFunding()

-----------------------------------------------------------------------------------------------

/** A sports team. */ public class Team { /** The team's name. */ private String name;

/** The team's head coach. */ private String headcoach;

/** The team's monetary funding level in thousands of US$. */ private int funding;

/** Constructs a new team. */ public Team(final String name, final String headcoach, final int funding) { if (name == null) { throw new IllegalArgumentException("name is null"); } // TODO validity checking for headcoach // TODO validity checking for funding this.name = name; // TODO complete this constructor }

/** Returns the team's name. */ public String getName() { return this.name; }

/** Returns the team's head coach. */ public String getHeadcoach() { // TODO complete this method return null; }

/** Returns the team's funding level. */ public int getFunding() { // TODO complete this method return -1; } }

---------------------------------------------------------------.

package edu;

import org.junit.Test; import static org.junit.Assert.*;

public class TestSearch { Team[] makeArrayFixture(final int size) { final Team[] array = new Team[size]; for (int i = 0; i < size; i++) { final String s = Integer.toString(i); array[i] = new Team("Team " + s, "Coach " + s, i * 100 + 50); } return array; }

// TODO makeListFixture

@Test public void testFindPositionArray0() { final Team[] arr = makeArrayFixture(0); assertFalse(Search.findTeamPosition(arr, "Team 5").isPresent()); }

@Test public void testFindPositionArray10s() { final Team[] arr = makeArrayFixture(10); assertTrue(Search.findTeamPosition(arr, "Team 5").isPresent()); }

@Test public void testFindPositionArray10f() { final Team[] arr = makeArrayFixture(10); assertFalse(Search.findTeamPosition(arr, "Team 11").isPresent()); } // TODO: testFindPositionList0, 10s, 10f // TODO: testFindMinFundingArray for several sizes and scenarios

// TODO: testFindMinFundingArrayFast for several sizes and scenarios }

How DO I DO the //TODO CODE?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!