1 / 29

Introduction to Computer Science

Introduction to Computer Science. Unit 20. Exam Information. What to Concentrate On. The exam will not ask any questions about the robot world material

Download Presentation

Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Science Unit 20 • Exam Information

  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. 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. 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. 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. 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. Translating from Previous Tests Java class Node {int _data; Node _left, _right;} type NodePOINT = ^NodeType;NodeType = record Data: integer; LeftPtr, RightPtr: NodePOINT end; Pascal

  8. Sample Question 1 • There is an object of class DataText, that holds text • It has the following methods defined:public DataText( ) {…} // constructor, empty objectpublic 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 objectpublic void putChar( ) {…}// puts a character in an object, updates// index to next position

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

  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. 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 replacekkk…kkk{ n timeswith\nk?

  12. Problem • What if the character k is a number? Then we'd have trouble distinguishing\98\101\10aand\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. 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\9and repeat for sequences of characters longer than 9

  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:192837465into\11\19\12\18\13\17\14\16\15 • Not good

  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:aabbcccwould becomeaabb\3c • Anybody see the problem?

  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. 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. 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. 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 9 2 5 7 3 2 3 6 1 7 9 2 3 8 8 1

  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. 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. 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. 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. 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. 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. 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. 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. 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. 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

More Related