All homeworks are due before class on the due date. All homework assignments must reflect your own understanding of the topic and be communicated in your own words. You may use one of the collaborative options. You may also request help and advice from your classmates on Piazza. Any help not allowed by that policy will be an honor violation.
Purpose: To encourage students to start working on the homework assignments early and recognize students who are actively engaged in the content
You will receiveNote: The final submission timestamp shown on Gradescope will be used.
Consider the following Java method
/**
* Description: Leet Code 771
* You're given strings jewels (representing the types of stones that are jewels)
* and stones (representing the stones you have).
* Each character in stones is a type of stone you have.
* You want to know how many of the stones you have are also jewels.
* Letters are case sensitive, so "a" is a different type of stone from "A".
*
* numJewelsInStones("aAb", "dbaaAbbcd") returns 6
* since there are 2 "a" and 1 "A" and 3 "b" in the string.
*/
public static int numJewelsInStones(String jewels, String stones)
{
var jewelSet = new HashSet<Character>();
for (int i = 0; i < jewels.length(); i++)
{
jewelSet.add(jewels.charAt(i));
}
var count = 0;
for (int i = 0; i < stones.length(); i++)
{
if (jewelSet.contains(stones.charAt(i)))
count += 1;
}
return count;
}
(2 points) Draw a control flow graph (or graphs) representing the software under test. Annotate all line of code/code fragments. (no need for data-flow annotations).
Consider the following Java method and the given graph (hw4.java)
/**
* This function parses a string s and returns a map containing the number
* of times each character occurred within the string.
*
* For example, if s = "testing"
* the frequencies method returns {s=1, t=2, e=1, g=1, i=1, n=1}
*/
public static Map<Character, Integer> frequencies(String s)
{
var map = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++)
{
Character c = s.charAt(i);
if (map.containsKey(c))
{
var currVal = map.get(c);
map.put(c, currVal + 1);
}
else
{
map.put(c, 1);
}
}
return map;
}
(2 points) Annotate the graph from Part 2 with definitions and uses (i.e., data-flow annotations).
Consider the following Java method and the given graph (hw4.java)
/**
* @param array an array in ascending order
* @param value the value to search for
* @return the index of 'value' in 'array' else -1
*/
public static int binarySearch(int[] array, int value)
{
int left = 0;
int right = array.length - 1;
while (left <= right)
{
int midIdx = (left + right) / 2;
if (array[midIdx] > value)
right = midIdx - 1;
else if (array[midIdx] < value)
left = midIdx + 1;
else
return midIdx;
}
return -1;
}
Do NOT modify the program under test.
Note that the Graph Coverage Web App and Data Flow Coverage Web App on the book web site will let you check (most of) your work. However, you should be able to apply the concept on a given software artifact without the tool.
[Total: 20 points] Complete the tasks. Provide correct and clear solutions. Properly document your solutions.
( -5 points) for 24 hours late (submitted after 27-October 11am EST, by 28-October 11am EST)
(-10 points) for 48 hours late (submitted after 28-October 11am EST, by 29-October 11am EST)
(-2 points) for submitting a Word document or handwriting, or a write-up that is not a typed PDF file.
Save your write-up as a PDF file (only typed PDF is acceptable) — No handwriting. No Word document. Only the graphs may be hand-drawn neatly.
Submit your JUnit test file(s), .java (not a .class).
Each team submits only one copy.
Upload your report as a PDF and your JUnit test file(s) (.java) to Assignment 4 on Gradescope. Make sure you connect your partner to your group on Gradescope so that everyone receives credit.
Making your submission available to instructor and course staff is your responsibility; if we cannot access or open your file, you will not get credit. Be sure to test access to your file before the due date.
CC-BY-NC-SA 4.0 license.