Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science Exam Information Unit 20.

Similar presentations


Presentation on theme: "Introduction to Computer Science Exam Information Unit 20."— Presentation transcript:

1 Introduction to Computer Science Exam Information Unit 20

2 20- 2 What to Concentrate On The exam will not ask any questions about the robot world material The exam assumes knowledge of everything else that you've seen in this course, including simple understanding of complexity (repeat, simple) Concentrate more on algorithms and data structures than on "system building"

3 20- 3 Good/Bad Questions Good question: –"Write an algorithm using recursion to solve the following problem" –"Write a method that solves the following problem, using a linked list" Bad question (not the kind we'll ask): –"Develop a class hierarchy for representing employment information for Israel's Mas Hachnasa office"

4 20- 4 Other Things to Use as Study Resources Besides the old exams, you can look at the book "Introduction to Computer Science using Java" in the library There are questions in every chapter, with some answers at the back of the book Practice answering these questions (obviously, ignore applet sections)

5 20- 5 Translating from Previous Tests Some previous tests had file operations (reading and writing) Assume that if we asked those kinds of questions, we'll restrict ourselves to the input/output operations you've seen in the class SimpleInput, or provide you with others Most of the questions we used previously could be reworded to use the operations we've seen (although requiring interactive input); as above, we might also provide you with methods for input/output from files, to be used in your answers

6 20- 6 Translating from Previous Tests In Pascal, there are Node definitions like this (for a linked list): type NodePOINT = ^NodeType; NodeType = record Data: integer; LeftPtr, RightPtr: NodePOINT end; In Java we write it as follows (next slide)

7 20- 7 Translating from Previous Tests class Node { int_data; Node_left, _right; } type NodePOINT = ^NodeType; NodeType = record Data: integer; LeftPtr, RightPtr: NodePOINT end;

8 20- 8 Sample Question 1 There is an object of class DataText, that holds text It has the following methods defined: public DataText( ) {…}// constructor, empty object public char getChar( ) {…} //returns a character and updates internal index, so next // getChar( ) will give you the next character in the object // (uses internal static variable) public boolean noMore( ) {…} // true when the internal index reaches the last // char in the object public void putChar( ) {…} // puts a character in an object, updates // index to next position

9 20- 9 Sample Question 1 You are given a DataText object pre- filled with text, like: 222222222222dddddddddbbbbbaaaaa Write two methods, one DataText compress(DataText d) {…} that takes the original DataText object and returns a compressed version, and DataText uncompress(DataText d) {…} that takes a compressed version, and returns an uncompressed version

10 20- 10 Sample Question 1 Assume that the character '\' does not appear in the original uncompressed DataText object Any other character may appear You may use additional methods The compressed file must never be bigger than the uncompressed version Do your best to maximally compress the text, and explain the algorithm well

11 20- 11 Outline of Solution We obviously want to use the character '\' as an "escape character" How about, if we have n occurrences of a letter k, we will replace kkk…kkk{ n times with \nk ?

12 20- 12 Problem What if the character k is a number? Then we'd have trouble distinguishing \98\101\10a and \982\1017\101a This can be handled, because eventually there is a single character before the next '\' (or at the end of the DataText object), but it makes our parsing harder

13 20- 13 Another Possibility Use the escape character at the end of the number, too: \98\2\101\7\101\a This makes the file longer, but is not a terrible solution (all things, including grades, are relative) A worse solution: only allow up to \9 and repeat for sequences of characters longer than 9

14 20- 14 Another Requirement What about making sure the compressed DataText object is never longer than the uncompressed version? If we were naïve, we might have a loop that converted: 192837465 into \11\19\12\18\13\17\14\16\15 Not good

15 20- 15 Solution We want to allow the DataText object to include "uncompressed" runs, too, as well as those with the escape character, i.e., a run of one or two characters won't be compressed: aabbccc would become aabb\3c Anybody see the problem?

16 20- 16 Constrains our Technique Now we've got a problem if we don't terminate the "escape number" with '\' What is the meaning of the following: \4027 Is it 402 occurrences of 7? Or is it the run 000027? If we allow uncompressed runs, we need to write (for example) \4\027, and only compress for runs of 4 or above

17 20- 17 Outline of Solution Keep track of current "candidate char" c for compression Read in more characters, keeping track of how many times c recurred, until we come to a character different than c If the number of times is less than four, just write out c that many times; otherwise, write '\', the number of times, '\' again, then c Repeat this until there are no more characters in the input

18 20- 18 Sample Question 2 We've got a binary tree, with the following Node definition: class Node { int_data; Node_left, _right; } Write a recursive method that writes out the data values at a given depth, from right to left

19 20- 19 Sample Question 2 The method receives a Node variable, pointing at the root of the tree, and a non-negative depth Example: print 7 2 3 for the following tree, given depth 2 5 2 7 3 2 2 6 3 7 1 9 8 3 1 8 9

20 20- 20 More Constraints The only two parameters to the method are the root Node and the desired depth The method must be recursive It should use no calls to other methods The method should not visit nodes deeper than the desired depth

21 20- 21 Outline of Solution There are two parameters, node and depth (which will start as root and desired depth) The recursive definition –If node is null, don't do anything –Else if depth is 0, print node's data –Else »call method on node.right, with depth - 1 »call method on node.left, with depth -1

22 20- 22 Java code void printDepth(Node n, int d) { if (n == null) return; else if (d == 0) System.out.print(n._data + " "); else { printDepth(n._right, d-1); printDepth(n._left, d-1); } }

23 20- 23 Twist on the Question What if we wanted to do this, but I required you to have only one parameter, a pointer to the root of the node No depth passed along as a parameter What's the answer?

24 20- 24 Sample Question 3 A macro is a short sequence of characters that represents a longer sequence of characters A macro definition (of \P) looks like this \P Whereas the party of the first part, Assume a SimpleInput method that returns a line of text from the user as a String: String getLine( ) {…}

25 20- 25 Sample Question 3 The input will consist of a series (possibly empty) of macro definitions, followed by \\, followed by text that uses the macros: \P Whereas the party of the first part, \D the party of the second part, \\ \P an unnamed plaintiff, and \D defendant (the University of California)… definitions use } }

26 20- 26 Sample Question 3 The method you write should output the following: Whereas the party of the first part, an unnamed plaintiff, and the party of the second part, defendant (the University of California)… Many details need to be followed: –Macros start on a line with '\' –Call to undefined macro will generate an error message

27 20- 27 Additional Information Use the following two methods to look at String information: To see the character at a specific location in a String: public char charAt(int index) {…} To compare two strings: public boolean equals(Object o) {…}

28 20- 28 First key issue What's the data structure that we want to use? What operations do we need to perform? –Storing macros (name and associated string) –Retrieving macros (given name, give back associated string) We might say you can use the class Vector, or we might require using, for example, a linked list

29 20- 29 Outline of Solution Loop to read in macro definitions, using getLine( ) Be on lookout for string \\, which you check using charAt( ) or equals( ) Place macro definitions into linked list or vector (as appropriate) When you get to \\, go into output section, still using getLine( ) to read Read and echo lines, replacing macro definitions with associated strings


Download ppt "Introduction to Computer Science Exam Information Unit 20."

Similar presentations


Ads by Google