DOC PREVIEW
Stanford CS 106A - Answers to Practice Final Examination

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #64ACS 106A March 10, 2010Answers to Practice Final Examination #2Problem 1—Short answer (15 points)1a)0 1 2 3 4 5 6 7 8 9 10 11yarra1212 22 30 36 40 42 42 40 36 30 22 120The values for this problem (which was taken from an autumn quarter final and thuscame at the right season) indicate the total number of gifts in each category if you takethe words to “The Twelve Days of Christmas” literally. At the end of true love’s gift-giving spree, the total haul contains:12 Partridges in pear trees 42 Swans-a-swimming22 Turtle doves 40 Maids-a-milking30 French hens 36 Ladies dancing36 Calling birds 30 Lords-a-leaping40 Gold rings 22 Pipers piping42 Geese-a-laying 12 Drummers drummingCharles M. Shultz offered a more interesting rendition of this problem in 1963:1b) 1. Missing declaration and initialization of result2. Illegal call to ch.isLetter() instead of Character.isLetter(ch)3. Failed to set inWord to true when a letter was found– 2 –Problem 2—Using the acm.graphics library (15 points)public class GraphicNim extends GraphicsProgram { public void init() { setupCoins(); addMouseListeners(); }/** Called when the mouse is clicked */ public void mouseClicked(MouseEvent e) { GObject obj = getElementAt(e.getX(), e.getY()); if (obj != null) { int index = coinList.indexOf(obj); if (index >= Math.max(0, coinList.size() - 3)) { for (int i = coinList.size() - 1; i >= index; i--) { remove((GObject) coinList.get(i)); coinList.remove(i); } } } }/* Sets up the game */ private void setupCoins() { double widthNeeded = COIN_COUNT * COIN_SIZE + (COIN_COUNT - 1) * COIN_SEP; double x = (getWidth() - widthNeeded) / 2; double y = (getHeight() - COIN_SIZE) / 2; coinList = new ArrayList(); for (int i = 0; i < COIN_COUNT; i++) { GOval coin = new GOval(x, y, COIN_SIZE, COIN_SIZE); coin.setFilled(true); coin.setFillColor(Color.GRAY); add(coin); coinList.add(coin); x += COIN_SIZE + COIN_SEP; } }/* Private instance variables */ private ArrayList coinList;/* Private constants */ private static final int COIN_COUNT = 11; private static final int COIN_SIZE = 20; private static final int COIN_SEP = 10;}– 3 –Problem 3—Strings (15 points)/** Returns true if s1 and s2 are anagrams of each other */ private boolean isAnagram(String s1, String s2) { int[] table1 = createFrequencyTable(s1); int[] table2 = createFrequencyTable(s2); for (int i = 0; i < table1.length; i++) { if (table1[i] != table2[i]) return false; } return true; }/** Creates a letter-frequency table for the string */ private int[] createFrequencyTable(String str) { int[] letterCounts = new int[26]; for (char ch = 'A'; ch <= 'Z'; ch++) { letterCounts[ch - 'A'] = 0; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isLetter(ch)) { letterCounts[Character.toUpperCase(ch) - 'A']++; } } return letterCounts; }Problem 4—Arrays (10 points)/** * Returns an image that is twice the size of the original in each * dimension. Each pixel in the original is replicated so that * it appears as a square of four pixels in the new image. */ private GImage doubleImage(GImage image) { int[][] oldPixels = image.getPixelArray(); int height = oldPixels.length; int width = oldPixels[0].length; int[][] newPixels = new int[2 * height][2 * width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int pixel = oldPixels[i][j]; newPixels[2 * i][2 * j] = pixel; newPixels[2 * i][2 * j + 1] = pixel; newPixels[2 * i + 1][2 * j] = pixel; newPixels[2 * i + 1][2 * j + 1] = pixel; } } return new GImage(newPixels); }– 4 –Problem 5—Building graphical user interfaces (15 points)/* * File: SignMaker.java * -------------------- * This program allows the user to compose an advertising * sign consisting of multiple lines of centered text. The * user can set the font of each line independently. */import acm.graphics.*;import acm.program.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class SignMaker extends GraphicsProgram { public void init() { lineInputField = new JTextField(CHARS_IN_LINE_FIELD); lineInputField.addActionListener(this); fontField = new JTextField(CHARS_IN_FONT_FIELD); fontField.setText("Times-Bold-24"); currentBaseline = 0; add(new JLabel("Line: "), SOUTH); add(lineInputField, SOUTH); add(new JLabel(" Font: "), SOUTH); add(fontField, SOUTH); }/** * Called when an action event occurs. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == lineInputField) { GLabel label = new GLabel(lineInputField.getText()); label.setFont(fontField.getText()); currentBaseline += label.getHeight(); double x = (getWidth() - label.getWidth()) / 2; add(label, x, currentBaseline); lineInputField.setText(""); } }/* Instance variables */ private int currentBaseline; private JTextField lineInputField; private JTextField fontField;/* Constants */ private static final int CHARS_IN_LINE_FIELD = 30; private static final int CHARS_IN_FONT_FIELD = 15;}– 5 –Problem 6—Using Java collections (15 points)import acm.util.*;import java.io.*;import java.util.*;/** * This class localizes strings for an internationalized application. */public class Localizer {/** * Creates a new Localizer from the data in the specified file. */ public Localizer(String filename) { map = new HashMap<String,String>(); try { BufferedReader rd = new BufferedReader(new FileReader(filename)); String word = null; while (true) { String line = rd.readLine(); if (line == null) break; int equalSign = line.indexOf('='); if (equalSign == -1) { word = line; } else { String language = line.substring(0, equalSign); String translation = line.substring(equalSign + 1); map.put(word + "+" + language, translation); } } rd.close() } catch (Exception ex) { throw new


View Full Document

Stanford CS 106A - Answers to Practice Final Examination

Download Answers to Practice Final Examination
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Answers to Practice Final Examination and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Answers to Practice Final Examination 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?