You are on page 1of 5

Lab 13

Driver: Kevin Aquino


Scribe: kaizhao & Bryan C

A
1. It will return the first repeated character
2. String index out of bounds exception output o a
3. if (ch == word.charAt(i + 1))
4. String Index out of bounds
5. The character is going over the length of the string
Output a o 0 0
/**
* A class that analyzes words.
*/
public class WordAnalyzer
{
/**
* Constructs an analyzer for a given word.
* @param aWord the word to be analyzed
*/
public WordAnalyzer(String aWord)
{
word = aWord;
}

/**
* Gets the first repeated character. A character is <i>repeated</i>
* if it occurs at least twice in adjacent positions. For example,
* 'l' is repeated in "hollow", but 'o' is not.
* @return the first repeated character, or 0 if none found
*/
public char firstRepeatedCharacter()
{
for (int i = 0; i < word.length() - 1; i++)
{
char ch = word.charAt(i);
if (ch == word.charAt(i + 1))
return ch;
}
return 0;
}

/**
* Gets the first multiply occuring character. A character is <i>multiple</i>
* if it occurs at least twice in the word, not necessarily in adjacent positions.
* For example, both 'o' and 'l' are multiple in "hollow", but 'h' is not.
* @return the first repeated character, or 0 if none found
*/
public char firstMultipleCharacter()
{
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
if (find(ch, i) >= 0)
return ch;
}
return 0;
}

private int find(char c, int pos)


{
for (int i = pos; i < word.length(); i++)
{
if (word.charAt(i) == c)
{
return i;
}
}
return -1;
}

/**
* Counts the groups of repeated characters. For example, "mississippi!!!" has
* four such groups: ss, ss, pp and !!!.
* @return the number of repeated character groups
*/
public int countRepeatedCharacters()
{
int c = 0;
for (int i = 0; i < word.length() - 1; i++)
{
if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
{
if (i == 0 || word.charAt(i - 1) != word.charAt(i)) // it't the start
c++;
}
}
return c;
}

private String word;


}
public class WordAnalyzerTester3
{
public static void main(String[] args)
{
test("mississippiii", 4); // expect: 4 (ss, ss, pp, iii)
test("test", 0); // expect: 0 (no repeated letters)
test("aabbcdaaaabb", 4); // expect: 4 (aa, bb, aaaa, bb)
}

public static void test(String s, int expected)


{
WordAnalyzer wa = new WordAnalyzer(s);
int result = wa.countRepeatedCharacters();
System.out.println(result + " repeated characters.");
System.out.println("Expected: " + expected);
}
}

B.
1. You get 4 0 3
2. You get 4 0 and not the last one because there is a breakpoint
3. It goes to the next line
4. What happens is that it goes to the next line
5. You would do step into to see the variables
6. Done
7. Done
8. Done
9. Done
10. Done

C
1. Done
2. Done
3. Done
4. The bug happened because
5. yes
/**
* A Java class that provides methods for finding vowels.
*
* @author Qi Yang
* @version 2022-04-22
*/
public class VowelFinder
{
String word;

/**
* Constructs a VowelFinder object.
*
* @param newWord the word to be analyzed
*/
public VowelFinder(String newWord)
{
word = newWord;
}

/**
* Cumulates all vowels of a word together into a string.
*
* @return string with all the vowels of a word
*/
public String findVowels()
{
String vowels = "aeiouyAEIOUY";
String result = "";
int i = 0;
while (i < word.length())
{
String letter = word.substring(i, i + 1);
if (vowels.contains(letter))
{
result = result + letter;
}
i++;
}
return result;
}
}
/**
* A Java tester program for class VowelFinder.
*
* @author Qi Yang
* @version 2022-04-22
*/
public class VowelFinderTester
{
public static void main(String[] args)
{
test("karen", "ae"); // Expected: ae
test("alice", "aie"); // Expected: aie
test("britain", "iai"); // Expected: iai
test("papaya", "aaya"); // Expected: aaya
test("",""); //Expected the empty string
}

/**
* A static method to test VowelFinder.
*
* @param s the string used to create a VowelFinder object
* @param expected the expected output string from method findVowels
*/
public static void test(String s, String expected)
{
VowelFinder vf = new VowelFinder(s);
String result = vf.findVowels();
System.out.println("Result: " + result + "\nExpected: " + expected + "\n");
}
}

You might also like