Add method findFirstOfPlayer with String searchString parameter to search the ca
ID: 3594495 • Letter: A
Question
Add method findFirstOfPlayer with String searchString parameter to search the cards ArrayList and return the index of ONLY the FIRST card found containing the given searchString parameter:
Use a local variable named index initialized to 0
Use a local variable named searching initialized to true
Loop while still searching AND index < .size (search player field of each card in cards until a match is found or all cards searched):
May (or may not) use a local variable to .get each card and then .getPlayer String for that card
if the player String for that card .contains the searchString, set searching to false (indicating card found at location index)
else the player has not been found yet, so increment index by 1 (thus, continuing with another loop iteration onto the next card)
(Only after ENTIRE search loop is completed - thus, outside of loop) if still searching (is true) then no card matching searchString was found, so print “NO player card for: <searchString>”
else there was a card found, so print the formatted String: “Player card for: <searchString> at index: <index>”
HINT: If found, then looping should end with the looping variable index holding the value of the index location in cards of where the actual card matching the searchString was found
Make sure that you return the index of the first card found, or an out-of-bounds index value (i.e. –1) if there is no card found at all
Explanation / Answer
Hi,
This program written in Java Language.
Java version used: Java 1.8
File name : CardDemo.java
-----------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
/**
* @author sachinargade
*
*/
public class CardDemo {
/**
* Find first player matching from card list
*
* @param searchString
* The serach string
* @param cardList
* The input card list
* @return The intger index
*/
private static int findFirstOfPlayer(String searchString, List<Card> cardList) {
int cardListSize = cardList.size();
boolean searching = true;
int index = 0;
// check list is null or empty
if (cardList != null && !cardList.isEmpty()) {
while (searching && index < cardListSize) {
Card card = cardList.get(index);
String player = card.getPlayer();
if (player.contains(searchString)) {
searching = false;
} else {
index++;
}
}
if (searching) {
System.out.println("NO player card for:" + searchString);
index = -1;
} else {
System.out.println("Player card for: " + searchString + " at index: " + index);
}
} else {
System.out.println("Card list may be null or emtpy");
}
return index;
}
/**
* @param args
*/
public static void main(String[] args) {
// Declare and initialize list
List<Card> cardList = new ArrayList<Card>();
cardList.add(new Card("Vijay"));
cardList.add(new Card("Anand"));
cardList.add(new Card("Sachin"));
cardList.add(new Card("Kanir"));
cardList.add(new Card("Sachin"));
// Calling search method to find First Of Player
int indexfirstOfPlayer = findFirstOfPlayer("Sachin", cardList);
System.out.println("IndexOf first player is :" + indexfirstOfPlayer);
}
}
class Card {
public Card(String player) {
this.player = player;
}
private String player;
/**
* @return the player
*/
public String getPlayer() {
return player;
}
/**
* @param player
* the player to set
*/
public void setPlayer(String player) {
this.player = player;
}
}
................................................................
Output of this program:
-------------------------------------------------------------------
Player card for: Sachin at index: 2
Index of First Player is :2
---------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.