import java.util.ArrayList; import java.util.Arrays; public class PS07A { /** * Write the method named mesh. * * Start with two ArrayLists of String, A and B, each with * its elements in alphabetical order and without any duplicates. * Return a new list containing the first N elements from the two * lists. The result list should be in alphabetical order and without * duplicates. A and B will both have a size which is N or more. * Your solution should make a single pass over A and B, taking * advantage of the fact that they are in alphabetical order, * copying elements directly to the new list. * * Remember, to see if one String is "greater than" or "less than" * another, you need to use the compareTo() method, not the < or > * operators. * * Examples: * mesh(["a","c","z"], ["b","f","z"], 3) returns ["a","b","c"] * mesh(["a","c","z"], ["c","f","z"], 3) returns ["a","c","f"] * mesh(["f","g","z"], ["c","f","g"], 3) returns ["c","f","g"] * * @param a an ArrayList of String in alphabetical order. * @param b an ArrayList of String in alphabetical order. * @param n the size of the resulting list to return. * @return a list of n elements in alphabetical order. */ // TODO - Write the mesh method here. /** * Write the method named examScore(). * * The "key" list contains the correct answers * to an exam, like ["a", "a", "b", "b"]. The "answers" * list contains a student's answers, with "?" representing * a question left blank. The two lists are not empty and * are the same length. Return the score for this list * of answers, giving +4 for each correct answer, -1 for * each incorrect answer, and +0 for each blank answer. * * Examples: * examScore(["a", "a", "b", "b"], ["a", "c", "b", "c"]) returns 6 * examScore(["a", "a", "b", "b"], ["a", "a", "b", "c"]) returns 11 * examScore(["a", "a", "b", "b"], ["a", "a", "b", "b"]) returns 16 * * @param key a list of correct answers to an exam. * @param answers the student's answers to the exam. * @return the score as described above. */ // TODO - Write the examScore method here. /** * Write the method filterBySize(). * * Given an ArrayList of String, return a new list where only * strings of the given length are retained. * * Examples: * filterBySize(["a", "bb", "b", "ccc"], 1) returns ["a", "b"] * filterBySize(["a", "bb", "b", "ccc"], 3) returns ["ccc"] * filterBySize(["a", "bb", "b", "ccc"], 4) returns [] * * @param list the list of Strings to process. * @param n the size of words to retain. * @return a new list of words with the indicated size retained. */ // TODO - Write the filterBySize method here. /** * Write the method named nOdds(). * * Given an ArrayList of positive Integers, return a new list * of length "count" containing the first odd numbers from * the original list. The original will contain at least * "count" odd numbers. * * Examples: * nOdds([3, 2, 4, 5, 8], 2) returns [3, 5] * nOdds([3, 2, 4, 5, 8, 9], 3) returns [3, 5, 9] * nOdds([3, 1, 2, 4, 5, 8], 3) returns [3, 1, 5] * * @param list the ArrayList of Integers to search. * @param count the number of odd numbers to copy. * @return the first "count" odd numbers from the list. */ // TODO - Write the nOdds method here. // A utility method for testing @SuppressWarnings("unchecked") public static ArrayList al(T...o) { ArrayList result = new ArrayList(); for (T s : o) result.add(s); return result; }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

import java.util.ArrayList;
import java.util.Arrays;

public class PS07A
{
/**
* Write the method named mesh.
*
* Start with two ArrayLists of String, A and B, each with
* its elements in alphabetical order and without any duplicates.
* Return a new list containing the first N elements from the two
* lists. The result list should be in alphabetical order and without
* duplicates. A and B will both have a size which is N or more.
* Your solution should make a single pass over A and B, taking
* advantage of the fact that they are in alphabetical order,
* copying elements directly to the new list.
*
* Remember, to see if one String is "greater than" or "less than"
* another, you need to use the compareTo() method, not the < or >
* operators.
*
* Examples:
* mesh(["a","c","z"], ["b","f","z"], 3) returns ["a","b","c"]
* mesh(["a","c","z"], ["c","f","z"], 3) returns ["a","c","f"]
* mesh(["f","g","z"], ["c","f","g"], 3) returns ["c","f","g"]
*
* @param a an ArrayList of String in alphabetical order.
* @param b an ArrayList of String in alphabetical order.
* @param n the size of the resulting list to return.
* @return a list of n elements in alphabetical order.
*/
// TODO - Write the mesh method here.

/**
* Write the method named examScore().
*
* The "key" list contains the correct answers
* to an exam, like ["a", "a", "b", "b"]. The "answers"
* list contains a student's answers, with "?" representing
* a question left blank. The two lists are not empty and
* are the same length. Return the score for this list
* of answers, giving +4 for each correct answer, -1 for
* each incorrect answer, and +0 for each blank answer.
*
* Examples:
* examScore(["a", "a", "b", "b"], ["a", "c", "b", "c"]) returns 6
* examScore(["a", "a", "b", "b"], ["a", "a", "b", "c"]) returns 11
* examScore(["a", "a", "b", "b"], ["a", "a", "b", "b"]) returns 16
*
* @param key a list of correct answers to an exam.
* @param answers the student's answers to the exam.
* @return the score as described above.
*/
// TODO - Write the examScore method here.

/**
* Write the method filterBySize().
*
* Given an ArrayList of String, return a new list where only
* strings of the given length are retained.
*
* Examples:
* filterBySize(["a", "bb", "b", "ccc"], 1) returns ["a", "b"]
* filterBySize(["a", "bb", "b", "ccc"], 3) returns ["ccc"]
* filterBySize(["a", "bb", "b", "ccc"], 4) returns []
*
* @param list the list of Strings to process.
* @param n the size of words to retain.
* @return a new list of words with the indicated size retained.
*/
// TODO - Write the filterBySize method here.


/**
* Write the method named nOdds().
*
* Given an ArrayList of positive Integers, return a new list
* of length "count" containing the first odd numbers from
* the original list. The original will contain at least
* "count" odd numbers.
*
* Examples:
* nOdds([3, 2, 4, 5, 8], 2) returns [3, 5]
* nOdds([3, 2, 4, 5, 8, 9], 3) returns [3, 5, 9]
* nOdds([3, 1, 2, 4, 5, 8], 3) returns [3, 1, 5]
*
* @param list the ArrayList of Integers to search.
* @param count the number of odd numbers to copy.
* @return the first "count" odd numbers from the list.
*/
// TODO - Write the nOdds method here.


// A utility method for testing
@SuppressWarnings("unchecked")
public static <T> ArrayList<T> al(T...o)
{
ArrayList<T> result = new ArrayList<T>();
for (T s : o) result.add(s);
return result;
}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning