Midterm1ModelSolutions

Midterm1ModelSolutions - { boolean match = true; int k = 0;...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Midterm 1 Model Solutions 1.A 25.5 1.B true 1.C 60 1.D 0 3 4 7 16 11 36 15 64 19 100 false 1.E 10 15 1.F 20 5 1.G 1 8 1.H O(N) 1.I O(N^2) 1.J O(log N) 25 10 15 5 20 1 12 5 20 19 13 1 12 2. public class Color { private int r, b, g; public Color() { r = 0; b = 0; g = 0; // or this(0,0,0); } public Color(int rr, int bb, int gg) { r = rr; b = bb; g = gg; } public boolean equals(Color c) { return (r == c.r) & (b == c.b) && (g == c.g); } public String toString() { return "red: " + r + " green: " + g + " blue: " + b; } public boolean isPure() { || (r == 0 & b == 0 && g == 255); } } 3. public void removeMatchingCards(Card c) { Card temp[] = new Card[myCards.length]; int newNumCards = 0; for(int i = 0; i < iMyNumCards; i++) { c.getValue() != myCards[i].getValue() ) { temp[newNumCards] = myCards[i]; newNumCards++; } } myCards = temp; iMyNumCards = newNumCards; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4. public String findMatches(char[] dna, int m)
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { boolean match = true; int k = 0; boolean matches = new boolean[dna.length]; for(int i = 0; i &lt; matches.lengthj; i++) matches[i] = false; for(int i = 0; i &lt; dna.length - m; i++) for(int j = i + 1; j &lt;= dna.length - m; j++) { match = true; k = 0; while(match &amp;&amp; k &lt; m) { match = dna[i + k] == dna[j + k]; k++ } if(match) { matches[i] = true; matches[j] = true; } } String result = &quot;&quot;; for(int i = 0; i &lt; dna.length; i++) if(matches[i]) result += i + &quot; &quot;; return result; } 5. public void Sieve(int limit) { boolean primes = new boolean[limit + 1]; for(int i = 0; i &lt; primes.length; i++) primes[i] = true; for(int num = 2; num &lt;= Math.sqrt(limit); num++) { if(primes[num]) for(int mult = num * 2; mult &lt; primes.length; mult += num) primes[mult] = false; } for(int i = 2; i &lt; primes.length; i++) if(primes[i]) System.out.println(i); }...
View Full Document

Page1 / 2

Midterm1ModelSolutions - { boolean match = true; int k = 0;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online