Class 14 W 2/27 Comp 110 Practice for first test Loops Boolean expressions Branches Note - solution to problems from class 13 have been added to the class 13 notes 1) Count duplicates. Write a code segment that is given a String (named str) and prints the number of duplicate characters that occur in the string. For example, "abcade" has 1 duplicate (the letter 'a'). "abcbcad" has 3 duplicates (one for each of 'a','b','c') "abaacdb" has 3 duplicates (2 duplicates of the letter 'a' and one of 'b'). NOTE - you will not have to write code for Test 1, but this example still is useful preparation for understanding nested loops. Scanner keyboard = new Scanner(System.in); String str = keyboard.nextLine(); // your code goes here (A full executable skeleton is at the end of this page.) ***************** Sample solution ************************ We grow this program in 2 steps. First, the simple inchworm approach. This works on the first 2 examples above but it will double count when there is more than 2 occurrences of any letter. Solving this simpler problem (counting duplicates but with double counting) is a good first step. When we get this working, we will modify it to avoid double counting. public static void countDuplicatesV0() { String str = "ababcdaaaa"; int count = 0; int outer = 0; while( outer < str.length()-2) { int inner = outer + 1; while( inner <+ str.length()-1) { if(str.charAt(inner) == str.charAt(outer)) count++; inner++; } outer++; } System.out.println(count + " duplicates found"); } Second, how can we prevent double counting? The simplest way is for the inner loop to stop counting duplicates when the first duplicate of a given letter is found. After making this change, we notice that the outer loop must go one farther down the string to handle the case where the last letter in the string is a duplicate. Note that the inner loop now has 2 loop control variables corresponding to the 2 different conditions under which the loop must stop 1. reached end of string 2. found a duplicate public static void countDuplicatesV1() { String str = "ababcdaaaa"; int count = 0; int outer = 0; while( outer < str.length()-1) { int inner = outer + 1; boolean duplicateFound = false; while( ! duplicateFound && inner <= str.length()-1) { if(str.charAt(inner) == str.charAt(outer)) { count++; duplicateFound = true; } inner++; } outer++; } System.out.println(count + " duplicates found"); } Lastly, now that the code is working, we rewrite it in exemplary style. Only one change, replace the outer while loop with a for loop. public static void countDuplicatesV2() { String str = "ababcdaaaa"; int count = 0; for(int outer = 0; outer < str.length()-1; outer++) { int inner = outer + 1; boolean duplicateFound = false; while( ! duplicateFound && inner <= str.length()-1) { if(str.charAt(inner) == str.charAt(outer)) { count++; duplicateFound = true; } inner++; } } System.out.println(count + " duplicates found"); } 2) Trace nested loop. What is the output from the following code segment? int sum = 0; for( int i = 0; i < 3; i++ ) { sum = sum + i; for( int j = i; j < 4; j++ ) sum = sum + j; System.out.println("sum = " + sum); } ******* correct answer sum = 6 sum = 13 sum = 20 3) Boolean expression equivalence. For each pair of logical expressions, do they test for exactly the same condition? Answer Same/Different. If they are different, modify the second condition to be equivalent to the first. In the following problems a, b, c are boolean variables x, y, and z are int i) !(a || b && c) !a && (!b && !c) **** In all these problems we use step by step substitution in an attempt to convert one boolean expression into the second. !(a || b && c) = using deMorgan's law !a && !(b && c) = using deMorgan's law !a && (!b || !c) Different ii) x >= y && y < z !(x < y || z < y) ******** solution - the second expression is more complicated than the first so we start by simplifying the second. de Morgan's law again. !(x < y || z < y) = !(x < y) && !(z < y) = bring the not into the comparison operator x >= y && z >= y Different. 2nd expression needs to be !(x < y || z <= y) iii) x == y && y == z !(x != y || z != y) *** solution !(x != y || z != y) = !(x != y) && !(z != y) = x == y && z == y = if we want to be completely thorough, apply communitivity to the equality operator x == y && y == z A full skeleton for count duplicates import java.util.Scanner; public class ThursdayPractice { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String str = keyboard.nextLine(); // your code goes here } }