Midterm1ModelSolutions

# Midterm1ModelSolutions - boolean match = true int k = 0...

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

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() { return (r == 255 && b == 0 && g == 0) || (r == 0 && b == 255 && g == 0) || (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++) { if( c.getSuit() != myCards[i].getSuit() && c.getValue() != myCards[i].getValue() ) { temp[newNumCards] = myCards[i]; newNumCards++; } } myCards = temp; iMyNumCards = newNumCards; }

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

View Full Document
4. public String findMatches(char[] dna, int m)
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 < matches.lengthj; i++) matches[i] = false; for(int i = 0; i < dna.length - m; i++) for(int j = i + 1; j <= dna.length - m; j++) { match = true; k = 0; while(match && k < m) { match = dna[i + k] == dna[j + k]; k++ } if(match) { matches[i] = true; matches[j] = true; } } String result = ""; for(int i = 0; i < dna.length; i++) if(matches[i]) result += i + " "; return result; } 5. public void Sieve(int limit) { boolean primes = new boolean[limit + 1]; for(int i = 0; i < primes.length; i++) primes[i] = true; for(int num = 2; num <= Math.sqrt(limit); num++) { if(primes[num]) for(int mult = num * 2; mult < primes.length; mult += num) primes[mult] = false; } for(int i = 2; i < primes.length; i++) if(primes[i]) System.out.println(i); }...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern