35a-section-6-solutions

35a-section-6-solutions - Mehran Sahami CS 106A Handout...

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

Mehran Sahami Handout #35A CS 106A November 9, 2011 Solutions to Section #6 1. How Prime public class SieveOfEratosthenes extends ConsoleProgram { private static final int UPPER_LIMIT = 1000; public void run() { boolean[] resolved = new boolean[UPPER_LIMIT + 1]; for (int i = 2; i <= UPPER_LIMIT; i++) { resolved[i] = false; } for (int n = 2; n <= UPPER_LIMIT; n++) { if (!resolved[n]) { println(n); for (int k = n; k <= UPPER_LIMIT; k += n) { resolved[k] = true; } } } } } 2. Image processing private GImage flipHorizontal(GImage image) { int[][] array = image.getPixelArray(); int width = array[0].length; int height = array.length; for (int row = 0; row < height; row++) { for (int p1 = 0; p1 < width / 2; p1++) { int p2 = width - p1 - 1; int temp = array[row][p1]; array[row][p1] = array[row][p2]; array[row][p2] = temp; } } return new GImage(array); }

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

View Full Document
– 2 – 3. Word Frequency import java.io.*; import java.util.*; import acm.program.*; public class WordFrequency extends ConsoleProgram{ private static final int MIN_COUNT = 50;
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/09/2012 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

35a-section-6-solutions - Mehran Sahami CS 106A Handout...

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

View Full Document
Ask a homework question - tutors are online