Practice Final Examination #2

Similar documents
Practice Final Examination #1

Practice Final Examination #2

Practice Midterm Examination #1

Practice Final Examination #1

Assignment #3 Breakout!

Programming Project 1

Practice Final Examination #1

Practice Final Examination #1

Practice Midterm Examination

Practice Midterm Examination #2

SSEA Newbies Handout 09 Summer 2012 August 17 th, 2012 Assignment 4: Bouncing Ball and Bizz Bazz Buzz

Assignment A7 BREAKOUT CS1110 Spring 2011 Due Fri 6 May 1

Assignment 2: Welcome to Java!

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Practice Midterm Examination

Assignment #2: Simple Java Programs Due: 1:15pm on Friday, April 19th

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, April 23 rd

Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

Spring 2018 June 20 th, 2018 Extra Practice Problems. Short Problems

Practice Final Examination

Eric Roberts Handout #27 CSCI 121 October 3, Project #2 Snowman. This assignment is designed to build your skills in the following areas:

Assignment #1 Simple C++

Touring the Mac S e s s i o n 4 : S A V E, P R I N T, C L O S E & Q U I T

CMPSCI 187 / Spring 2015 Hangman

Coach s Office Playbook Tutorial Playbook i

You have three hours to complete this exam. There are 50 total points.

CS106A Handout 18 Winter February 3, 2014 Practice Midterm Exam

Practice Midterm #2. Midterm Time: Monday, July 18 th, 7pm 9pm Midterm Location: Hewlett 200

Practice Midterm Exam #1

CS 106A, Lecture 11 Graphics

CS2 Practical 1 CS2A 22/09/2004

[ the academy_of_code] Senior Beginners

Answers to Practice Final #1

CS161: Introduction to Computer Science Homework Assignment 10 Due: Monday 11/28 by 11:59pm

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, Jan 29 th

Creating Breakout - Part 2

Practice Midterm Examination

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

First Visual Basic Lab Paycheck-V1.0

Assignment #7 FacePamphlet Due: 1:30pm on Friday, December 7th Note: No late days (free or otherwise) may be used on Assignment #7

Part II: Creating Visio Drawings

Making ecards Can Be Fun!

Assignment #4 Hangman Due: 11am on Monday, May 14th This assignment may be done in pairs (which is optional, not required)

The first time you open Word

You Can Make a Difference! Due April 11/12 (Implementation plans due in class on 4/9)

Graphing on Excel. Open Excel (2013). The first screen you will see looks like this (it varies slightly, depending on the version):

Objects and Graphics

Drawing shapes and lines

Spring 2018 June 20 th, 2018 CS106A Practice Final #1

CS 134 Programming Exercise 9:

the NXT-G programming environment

CSE 142 Su 02 Homework 4

Beginning a presentation

Expressions, Statements, and Control Structures

CS 134 Programming Exercise 2:

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

SETTING UP A. chapter

CS 142 Style Guide Grading and Details

PART 7. Getting Started with Excel

YEAH Hours. January , 7-8 PM Jared Wolens

CS106A Handout 28 Winter February 28, 2014 Second Practice Second CS106A Midterm

1 Getting started with Processing

CSSE 490 Model-Based Software Engineering

Variables, Types, and Expressions

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Section Handout #7: Interactors and Classes

CS106A Review Session

DOING MORE WITH WORD: MICROSOFT OFFICE 2013

CS 106A, Lecture 27 Final Exam Review 1

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

Solutions to Quiz 1 (March 14, 2016)

DOING MORE WITH WORD: MICROSOFT OFFICE 2007

Practice Midterm Exam #2

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

MAKING TABLES WITH WORD BASIC INSTRUCTIONS. Setting the Page Orientation. Inserting the Basic Table. Daily Schedule

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

CHAPTER 3. Entering Text and Moving Around

First Name: AITI 2004: Exam 2 July 19, 2004

Visual C# Program: Simple Game 3

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 051 Homework Laboratory #2

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Assignment 3 Functions, Graphics, and Decomposition

Solving Minesweeper Using CSP

DOING MORE WITH WORD: MICROSOFT OFFICE 2010

IGCSE ICT Section 16 Presentation Authoring

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Variables One More (but not the last) Time with feeling

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Quiz 1 (March 14, 2016)

Programming via Java Defining classes

Name CIS 201 Midterm II: Chapters 1-8

CS 2110 Summer 2011: Assignment 2 Boggle

Creating a Template in WordPerfect

Microsoft Office Word. Part1

Making use of other Applications

To practice overall problem-solving skills, as well as general design of a program

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

CS Problem Solving and Object-Oriented Programming

Practice Final Examination #2

Transcription:

Eric Roberts Handout #61 CS 106A May 30, 2012 Practice Final Examination #2 Review session: Wednesday, June 6, 7:30 9:30 P.M. (Hewlett 200) Scheduled finals: Friday, June 8, 8:30 11:30 A.M. (Dinkelspiel Auditorium) Monday, June 11, 8:30 11:30 A.M. (Hewlett 200) The answers to this practice final will be posted to the web site on Friday. Problem 1 Short answer (10 points) 1a) Trace through the evaluation of the following program and indicate the output it produces. import acm.program.*; public class Mystery extends ConsoleProgram { public void run() { int[] array = new int[13]; for (int i = 1; i <= 12; i++) { for (int j = i; j > 0; j--) { array[j] += j; Work through the method carefully and indicate your answer by filling in the boxes below to show the final contents of array, just before the run method returns: array 0 1 2 3 4 5 6 7 8 9 10 11 12

2 1b) Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code. One of the methods you have been asked to rewrite which doesn t even compile looks like this: /** * Returns the acronym of the specified string, which is * defined to be a new word formed from the first letter in * each separate word contained in the string. A word is a * consecutive string of letters. For example, calling * * acronym("self-contained underwater breathing apparatus") * * should return the string "scuba". */ private String acronym(string str) { boolean inword = false; for (int i = 0; i < str.length(); i++) { char ch = str.charat(i); if (ch.isletter()) { if (!inword) result += ch; else { inword = false; return result; Unfortunately, the code short as it is contains several bugs. Circle the bugs in the implementation and write a short sentence explaining the precise nature of each problem you identify. Problem 2 Using the acm.graphics library (15 points) Back before we had a graphics library, one of the early assignments in CS 106A was to write a program that played a simple game called Nim. In the simplest version of the game, two players start with a pile of 11 coins on the table between them. The players then take turns removing 1, 2, or 3 coins from the pile. The player who is forced to take the last coin loses. In the old days, the point of this assignment was to figure out a strategy for winning the game. With our modern attachment to fancier user interfaces, however, it is just as easy to focus on the problem of representing the game graphically on the screen. Your job in this problem is to complete the implementation of the program that appears on the following page, which is responsible for displaying the coins and handling the user interaction. You should think about the task as consisting of two parts. The first part of this problem is to create a graphical display in which you have a line of coins arranged horizontally on the screen, like this:

3 In constructing your display, you should make use of the named constants N_COINS, COIN_SIZE, and COIN_SEP that appear at the top of the program framework. You should also make sure that each coin is filled in GRAY and outlined in BLACK. The line of coins should be centered both horizontally and vertically in the window. The second part of the problem consists of making it possible to take coins away. Add the necessary code so that if the user clicks the mouse in one of the last three coins in the row, those coins disappear. For example, if the user clicked on the third coin from the right end, the program should respond by removing the last three coins from the display, like this: If the user then clicked on the rightmost coin, only that coin should go away: If the mouse click does not occur inside a coin or if the coin is not one of the last three in the row, that click should simply be ignored. In order to solve this problem, you will need to store the GOval objects in some kind of data structure that allows you to keep track of their order. Given the fact that you need to take coins away, an ArrayList seems perfect for the job. When a mouse click occurs, your program needs to find the object at that mouse location (if any) and see whether it is one of the last three elements in the ArrayList. If so, your program should remove that element and any following elements from both the ArrayList and the window. You may assume the definition of the following constants: public static final int N_COINS = 11; /* Number of coins */ public static final int COIN_SIZE = 25; /* Diameter of a coin */ public static final int COIN_SEP = 10; /* Space between coins */

4 Problem 3 Strings (15 points) In Dan Brown s best-selling novel The Da Vinci Code, the first clue in a long chain of puzzles is a cryptic message left by the dying curator of the Louvre. Two of the lines of that message are O, Draconian devil! Oh, lame saint! Professor Robert Langdon (the hero of the book, who is played by Tom Hanks in the movie version) soon recognizes that these lines are anagrams pairs of strings that contain exactly the same letters even if those letters are rearranged for Leonardo da Vinci The Mona Lisa Your job in this problem is to write a predicate method public boolean isanagram(string s1, String s2) that takes two strings and returns true if they contain exactly the same alphabetic characters, even though those characters may appear in any order. Thus, your method should return true for each of the following calls: isanagram("o, Draconian devil!", "Leonardo da Vinci") isanagram("oh, lame saint!", "The Mona Lisa") isanagram("algorithmically", "logarithmically") isanagram("doctor Who", "Torchwood") These examples illustrate two important requirements for the isanagram method: The implementation should look only at letters (i.e., characters for which the Character.isLetter method returns true), ignoring any extraneous spaces and punctuation marks thrown in along the way. The implementation should ignore the case of the letters in both strings. There are many different algorithmic strategies you could use to decide whether two strings contain the same alphabetic characters. If you re having trouble coming up with a strategy, you should note that two strings are anagrams if and only if they would generate the same letter-frequency table, as discussed in the lecture on arrays.

5 Problem 4 Arrays (10 points) Write a method private GImage doubleimage(gimage oldimage) that takes an existing GImage and returns a new GImage that is twice as large in each dimension as the original. Each pixel in the old image should be mapped into the new image as a 2 x 2 square in the new image where each of the pixels in that square matches the original one. As an example, suppose that you have a GImage from the file TinyFrenchFlag.gif that looks like this, where the diagram has been expanded so that you can see the individual pixels, each of which appears as a small outlined square: This 6 x 4 rectangle has two columns of blue pixels, two columns of white pixels, and two columns of red pixels. Calling GImage biggerflag = doubleimage(new GImage("TinyFrenchFlag.gif")); should create a new image with the following 12 x 8 pixel array: The blue pixel in the upper left corner of the original has become a square of four blue pixels, the pixel to its right has become the next 2 x 2 square of blue pixels, and so on. Keep in mind that your goal is to write an implementation of doubleimage that works with any GImage and not just the flag image used in this example.

6 Problem 5 Building graphical user interfaces (20 points) In this problem, your task is to implement a simple graphical user interface for creating simple signs, each of which consists of lines of centered text displayed in different fonts. When you start the SignMaker program, the user interface looks like this, where the widths of the JTextField interactors are given by the constants CHARS_IN_LINE_FIELD and CHARS_IN_FONT_FIELD, respectively: You can then add a line to the display by entering text in the field marked Line, as follows: Hitting ENTER should add a new GLabel to the graphics display containing the text in the field. That GLabel should be centered in the window in both dimensions, adjusting the vertical position of the text by half the font ascent as you did for Assignment #2. The first line therefore is set in Times-Bold-36 as follows:

7 Note that hitting ENTER also clears the text field in the control strip making it easier for the user to enter the next line of the sign. Once you have placed a label in the window, the user should be able to drag the label to another point on the screen. For example, the user might drag the label to the top, so the window looks like this: The user can change the font of each line by typing a new font name in the Font field. For example, if the user wanted to add a second line to the message in a smaller, italic font, that user could do so by changing the contents of the Font field to Times-Italic-24 and then typing in the new message, like this: Hitting ENTER at this point would add a new GLabel at the center of the window, like this: You can then reposition either of these labels by clicking on them and dragging them to the desired location.

8 Problem 6 Data structures (20 points) In recent years, the globalization of the world economy has put increasing pressure on software developers to make their programs operate in a wide variety of languages. That process used to be called internationalization, but is now more often referred to (perhaps somewhat paradoxically) as localization. In particular, the menus and buttons that you use in a program should appear in a language that the user knows. Your task in this problem is to write a definition for a class called Localizer designed to help with the localization process. The constructor for the class has the form public Localizer(String filename) The constructor creates a new Localizer object and initializes it by reading the contents of the data file. The data file consists of an English word, followed by any number of lines of the form xx=translation where xx is a standardized two-letter language code, such as de for German, es for Spanish, and fr for French. Part of such a data file, therefore, might look like this: Localizations.txt Cancel de=abbrechen es=cancelar fr=annuler Close de=schließen es=cerrar fr=fermer OK fr=approuver Open de=öffnen es=abrir fr=ouvrir This file tells us, for example, that the English word Cancel should be rendered in German as Abbrechen, in Spanish as Ayudar, and in French as Annuler. Beyond the constructor, the only public method you need to define for Localizer is public String localize(string word, String language) which returns the English word passed as the first parameter translated into the language indicated by the two-letter language code passed as the second parameter. For example, assuming that you had initialized a variable mylocalizer by calling Localizer mylocalizer = new Localizer("Localizations.txt"); you could then call

9 mylocalizer.localize("open", "de"); and expect it to return the string "Öffnen". If no entry appears in the table for a particular word, localize should return the English word unchanged. Thus, OK becomes Approuver in French, but would remain as OK in Spanish or German. As you write your answer to this problem, here are a few points to keep in mind: You can determine when a new entry starts in the data file by checking for a line without an equal sign. As long as an equal sign appears, what you have is a new translation for the most recent English word into a new language. For this problem, you don t have to work about distinctions between uppercase and lowercase letters and may assume that the word passed to localize appears exactly as it does in the data file. The data file shown on the previous page is just a small example; your program must be general enough to work with a much larger file. You may not assume that there are only three languages or, worse yet, only four words. You don t have to do anything special for the characters in other languages that are not part of standard English, such as the ö and ß that appear in this data file. They re just characters in the expanded Unicode set that Java uses. It may help you solve this problem if you observe that it is the combination of an English word and a language code that has a unique translation. Thus, although there are several different translations of the word Close in the localizer and German translations of many words, there is only one entry for the combination Close+de.

10 Problem 7 Essay: Extensions to the assignments (10 points) In many ways, the assignment that offers the greatest opportunities for extension is the Breakout game from Assignment #3. For this problem, imagine that you are trying to create a Super Breakout in which multiple balls can be in play at the same time. Here is how you would like your new program to work. In the initial layout of bricks at the top of the screen, a random collection of bricks (a different set each time) are designated superbricks and are marked on the display by having a black border instead of being solidly colored throughout. Superbricks are relatively rare; as you lay them out, each brick has a 5% chance of being a superbrick. One possible initial configuration with four superbricks appears at the left side of Figure 1 at the bottom of the page. The game proceeds exactly as it did before until the ball hits a superbrick, at which point the program fires off another ball just as it did at the beginning of the turn. The diagram at the right of Figure 1 shows the situation shortly after the ball has collided with the cyan superbrick in the second row. There are now two balls in play. In each time step of the game, both balls move, bounce off the walls and the paddle, and collide with bricks (and possibly each other). The number of balls increases as you hit more superbricks, and decreases when a ball falls off the bottom. The current turn, however, ends only when there are no balls remaining on the board. Discuss the changes that would be necessary in the Breakout implementation to implement this extension to the game. Keep in mind that this is an essay question, and you should not feel compelled to write any actual code unless you feel that doing so is the best way to convey your ideas. You should, however, explain as clearly as you can what parts of the program will need to change and what data structures you would need to add to the program to implement this feature. Figure 1. Scenes from Super Breakout