introduction to computer science exam information unit 20

29
Introduction to Computer Science Exam Information Unit 20

Post on 21-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Science Exam Information Unit 20

Introduction to Computer Science

• Exam Information

Unit 20Unit 20

Page 2: Introduction to Computer Science Exam Information Unit 20

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"

Page 3: Introduction to Computer Science Exam Information Unit 20

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"

Page 4: Introduction to Computer Science Exam Information Unit 20

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)

Page 5: Introduction to Computer Science Exam Information Unit 20

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

Page 6: Introduction to Computer Science Exam Information Unit 20

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:

NodePOINTend;

• In Java we write it as follows (next slide)

Page 7: Introduction to Computer Science Exam Information Unit 20

20- 7

Translating from Previous Tests

class Node {int _data;Node _left, _right;

}

type NodePOINT = ^NodeType;NodeType = record

Data: integer;LeftPtr, RightPtr: NodePOINT

end;

Page 8: Introduction to Computer Science Exam Information Unit 20

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

Page 9: Introduction to Computer Science Exam Information Unit 20

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

Page 10: Introduction to Computer Science Exam Information Unit 20

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

Page 11: Introduction to Computer Science Exam Information Unit 20

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 timeswith

\nk?

Page 12: Introduction to Computer Science Exam Information Unit 20

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

Page 13: Introduction to Computer Science Exam Information Unit 20

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

Page 14: Introduction to Computer Science Exam Information Unit 20

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:

192837465into

\11\19\12\18\13\17\14\16\15

• Not good

Page 15: Introduction to Computer Science Exam Information Unit 20

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:

aabbcccwould become

aabb\3c

• Anybody see the problem?

Page 16: Introduction to Computer Science Exam Information Unit 20

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

Page 17: Introduction to Computer Science Exam Information Unit 20

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

Page 18: Introduction to Computer Science Exam Information Unit 20

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

Page 19: Introduction to Computer Science Exam Information Unit 20

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

73

2

2

6 37 1 9

8 3 1 8

9

Page 20: Introduction to Computer Science Exam Information Unit 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

Page 21: Introduction to Computer Science Exam Information Unit 20

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

Page 22: Introduction to Computer Science Exam Information Unit 20

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);

}}

Page 23: Introduction to Computer Science Exam Information Unit 20

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?

Page 24: Introduction to Computer Science Exam Information Unit 20

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\PWhereas the party of the first part,

• Assume a SimpleInput method that returns a line of text from the user as a String:

String getLine( ) {…}

Page 25: Introduction to Computer Science Exam Information Unit 20

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:

\PWhereas the party of the first part,\Dthe party of the second part,\\\Pan unnamed plaintiff, and\Ddefendant (the University of California)…

definitions

use

}}

Page 26: Introduction to Computer Science Exam Information Unit 20

20- 26

Sample Question 3

• The method you write should output the following:

Whereas the party of the first part,an unnamed plaintiff, andthe 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

Page 27: Introduction to Computer Science Exam Information Unit 20

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) {…}

Page 28: Introduction to Computer Science Exam Information Unit 20

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

Page 29: Introduction to Computer Science Exam Information Unit 20

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