0% found this document useful (0 votes)
13 views4 pages

(Key) Csa U8l7 Searching and Sorting Extra Practice

The document provides exercises related to searching and sorting in Java, including code segments for checking budget constraints and merging lists of participants. It also includes a task to implement a method for updating the color of letters in a Wordle game based on specific rules. The exercises aim to enhance understanding of array manipulation and algorithm implementation.

Uploaded by

Zeyna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

(Key) Csa U8l7 Searching and Sorting Extra Practice

The document provides exercises related to searching and sorting in Java, including code segments for checking budget constraints and merging lists of participants. It also includes a task to implement a method for updating the color of letters in a Wordle game based on specific rules. The exercises aim to enhance understanding of array manipulation and algorithm implementation.

Uploaded by

Zeyna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSA Unit 8 Lesson 7

Name(s) __________________________________________________ Period _______ Date _____________________

[KEY] Extra Practice - Searching and Sorting

Check for Understanding

Consider the following code segment:

ArrayList<String> menuItems = new ArrayList<String>();


menuItems.add("Curry");
menuItems.add("Stir Fry");
menuItems.add("Noodles");
menuItems.add("Fried Rice");

ArrayList<Double> prices = new ArrayList<Double>();


prices.add(13.50);
prices.add(12.00);
prices.add(13.00);
prices.add(14.00);

public String[] withinBudget


(ArrayList<String> menu, ArrayList<double> prices, double budget) {
ArrayList<String> choices = new ArrayList<String>();
for (int i = 0; i < menu.size(); i++) {
if ( /* missing code 1 */ ) {
/* missing code 2 */
}
}

return choices;
}

1
Which two code segments can be inserted in for /* missing code 1 */ and /* missing code 2 */ in
order to return a list of menu items that are equal to or below the budget?

A. prices.get(i) == budget, ​ ​ choices.add(menu.get(i));

B. prices.get(i) <= budget, ​ ​ choices.add(menu.get(i));

C. prices.get(i) <= budget, ​ ​ menu.add(menu.get(i));

D. menu.get(i) <= budget,​ ​ ​ choices.add(prices.get(i));

E. menu.get(i) == budget,​ ​ ​ choices.add(prices.get(i));

AP Exam Prep

Your city is hosting a charity marathon that will occur on two different dates and stores the names of registered
participants in two ArrayLists of String objects called day1Runners and day2Runners. Interested
participants could register for one or both of the dates. When a participant registers, their name is added to
either the day1Runners list, the day2Runners list, or both.

The city wants a new list created containing the names of all unique participants that have registered for either
or both dates of the marathon. Write pseudocode or Java code to merge the two lists into one new list without
any duplicates.

ArrayList<String> merged = new ArrayList<String>();

for (String s : day1Runners) {


merged.add(s);
}

for (String s : day2Runners) {


boolean alreadyInList = false;

for (String name : day1Runners) {


if (s.equals(name)) {
alreadyInList = true;
}
}

if (!alreadyInList) {
merged.add(s);
}
}

2
Extra Practice

Do This: You have implemented standard algorithms that utilize array traversal to determine if multiple
elements have a particular property. Let's use this algorithm to check a word for the game of Wordle, and
update each character's color property.

Assume the class Letter has already been defined, and is described by this UML diagram:

Letter

-​ letter: String
-​ color: String

+​ getLetter()
+​ getColor()
+​ setColor(String color)

Implement updateColor(Letter[] guess, Letter[] goal) which accepts a guess word and a goal word,
then changes the letter property of the goal word based on the following rules:

●​ Green: The character is in the word and in the correct location.


●​ Yellow: The character is part of the word, but not in the correct location.
●​ Gray: The character is not part of the word.

Assume that guess and goal are the same length.

public void updateColor(Letter[] guess, Letter[] goal) {

for (int i = 0; i < guess.length; i++) {


if (guess[i].getLetter().equals(goal[i].getLetter())) {
guess[i].setColor("Green");
}
else {
boolean partOfWord = false;
for (int j = 0; j < goal.length; j++) {
if (guess[i].getLetter().equals(goal[j].getLetter())) {
3
partOfWord = true;
}
}

if (partOfWord) {
guess[i].setColor("Yellow");
}
else {
guess[i].setColor("Gray");
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy