






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Eric Roberts Handout # CS 106A March 5, 2010
Review session: Sunday, March 14, 7:00–9:00 P. M. (Hewlett 201)
Scheduled finals: Monday, March 15, 12:15–3:15 P. M. (Hewlett 200) Friday, March 19, 12:15–3:15 P. M. (Hewlett 200)
This handout is intended to give you practice solving problems that are comparable in format and difficulty to those which will appear on the final examination. A solution set to this practice examination will be handed out at the optional class on Monday. (CS 106A does not hold required classes during Dead Week.)
Time of the exam
The final exam is scheduled at two different times during exam period, as shown at the top of this handout. Both finals are in the regular classroom. You may take the exam at either of the two scheduled times and need not give advance notice of which exam you plan to take. If you are unable to take the exam at either of the scheduled times, please send an e-mail message to eroberts@cs stating the following:
In order to take an alternate exam, I must receive this message from you by 5:00 P.M. on Wednesday, March 10. Replies will be sent by electronic mail on Friday, March 12.
Review session
Several section leaders will conduct a review session on Sunday evening from 7:00 to 9:00 P .M. in Hewlett 201. We’ll will announce the winners of the Adventure Contest and hold the random grand-prize drawing at the beginning of the review session.
Coverage
The exam covers the material presented in class through today, which means that you are responsible for the material in Chapters 1 through 13 of The Art and Science of Java, with the following exceptions:
General instructions
The instructions that will be used for the actual final look like this:
Answer each of the questions given below. Write all of your answers directly on the examination paper, including any work that you wish to be considered for partial credit.
Each question is marked with the number of points assigned to that problem. The total number of points is 90. We intend that the number of points be roughly equivalent to the number of minutes someone who is completely on top of the material would spend on that problem. Even so, we realize that some of you will still feel time pressure. If you find yourself spending a lot more time on a question than its point value suggests, you might move on to another question to make sure that you don’t run out of time before you’ve had a chance to work on all of them.
In all questions, you may include methods or definitions that have been developed in the course, either by writing the import line for the appropriate package or by giving the name of the method and the handout or chapter number in which that definition appears.
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.
Note: To conserve trees, I have cut back on answer space for the practice exams. The actual final will have much more room for your answers and for any scratch work.
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 has the following comment and prototype:
/* Method: insertValue(value, array) / /*
for (int i = 0; i < result.length; i++) { result[i] = array[i]; }
int pos = 0; for (int i = 0; i < array.length; i++) { if (value > array[i]) { pos = i; break; } }
for (int i = result.length; i >= pos; i--) { result[i] = result[i - 1]; }
result[pos] = value;
return result; }
Unfortunately, the code 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)
Although the definition of filling for an arc (see page 314 in the text) is not necessarily what you would want for all applications, it turns out to be perfect for the problem of displaying a traditional pie chart. Your job in this problem is to write a method
private GCompound createPieChart(double r, double[] data)
that creates a GCompound object that displays a pie chart for the specified data values. The parameter r represents the radius of the circle in which the pie chart is displayed, and data is the array of data values you want to plot in the chart.
The operation of the createPieChart method is easiest to illustrate by example. If you execute the run method
public void run() { double[] data = { 45, 25, 15, 10, 5 }; GCompound pieChart = createPieChart(50, data); add(pieChart, getWidth() / 2, getHeight() / 2); }
your program should generate the following pie chart in the center of the window:
PieChart
The red wedge corresponds to the 45 in the data array and extends 45% around the circle, which is not quite halfway. The yellow wedge then picks up where the red wedge left off and extends for 25% of a complete circle. The blue wedge takes up 15%, the green wedge takes up 10%, and the pink wedge the remaining 5%.
As you write your solution to this problem, you should keep the following points in mind:
private static Color[] WEDGE_COLORS = { Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN, Color.PINK, Color.CYAN, Color.MAGENTA, Color.ORANGE };
If you have more wedges than colors, you should just start the sequence over, so that the eighth wedge would be red, the ninth yellow, and so on.
Problem 4—Arrays (10 points)
If you spent any time at all playing your Yahtzee program, you know that it would be wonderful if there were a two-pair category for all the times you just missed getting a full house. Write a Java method
private boolean checkTwoPairCategory(int[] dice)
that takes an array of five die values and returns true if and only if the values in the dice array contain two pairs and a fifth value that does not match the others.
In writing your answer to this problem, you should keep the following points in mind:
Problem 5—Building graphical user interfaces (15 points)
Write a GraphicsProgram that does the following:
North South East West
EtchASketch
Keep in mind that each button click adds a new GLine that starts where the previous one left off. The result is therefore a line that charts the path of the cross as it moves in response to the buttons. For example, if you clicked East, North, West, North, and East in that order, the screen would show a Stanford “S” like this:
North South East West
EtchASketch
Problem 7—Essay: Extensions to the assignments (10 points)
In Crowther’s original version of Adventure, the solution to one of the puzzles involves a magic word that automatically returns a treasure from wherever it happens to be to its initial location. In this problem, your job is to discuss the changes that would be necessary in the Adventure implementation to implement a similar feature in the game from Assignment #6. This question requires an essay answer, 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, indicate by name what classes and methods need to change.
To make this question more concrete, suppose that you have been asked to add a command
ZAP object
that has the effect of returning the specified object to its initial room. Like LOOK, QUIT, INVENTORY, and the other action commands, the ZAP command can be executed in any room. Unlike the TAKE command, however, the object is not ordinarily in that room, but can be anywhere in the cave, including the player’s inventory.
Before you get the idea that this question is too easy, note that there is no way in the current design to determine where an object is currently located. Given a room, you can find the objects in that room, and you have presumably added an array list somewhere to keep track of the player’s inventory. Adding the object to the list in its starting room is relatively easy, but taking it out of its current location is hard. You need to think about the changes you need to make in the overall design to implement this feature.