CMPSCI 250: Introduction to Computation

David Mix Barrington

Spring, 2006

Homework #1 Questions and Answers

Question 1.8, 12 February 2006

In Problem 1.1.5, do I have to write constructor methods or any other methods that will make a Cset object have the correct int value? You don't explicitly ask for any of these methods.

That's right, I didn't. You should assume that these methods would be written by someone else, and thus that the Cset instance that runs your methods will have had its int value set properly. If we were writing the full Cset class, it would probably have a constructor that would create an empty Cset, and methods to add and delete elements from an existing Cset.

In particular, part (b) of 1.1.5 asks you only for the header and instance variable, and the last three parts ask you for one method each.

By the way, may I write the code in C++ instead of Java?

Actually, no, because I asked for it in Java. I don't know whether Dan is familiar with C++ (I'm not) but I would rather have you than him be responsible for the small differences between the languages. Of course the exact syntax is only a small part of the problem, so you wouldn't lose very many points by writing it in C++.

Question 1.7, 10 February 2006

In Problem 1.6.5, how big a truth table should I be prepared to produce? If the method is called with k = 100, am I responsible for being able to produce a table of 2100 lines?

A good point -- you may assume that the table will contain no more than a billion lines, so that the number of lines can be stored as an int. (It wouldn't be hard to use long variables to make your code work in principle up to 1018 lines or so, but as you say, it would be silly.)

Question 1.6, 10 February 2006

In Problem 1.1.5, may I use the toBinaryString method in the Java Integer class? Your representation of sets is very similar to binary notation.

Yes, it is, which is why I'd prefer that you not use the pre-written method to do what is essentially the same job. (In 1.6.5, on the other hand, you're welcome to use toBinaryString if you like.) If you don't want to do it another way, you could include the code of that standard Java method, which I'm sure you can find on-line, and add a sentence or two to convince Dan that you understand how that method does what it does.

Question 1.5, 10 February 2006

In Problem 1.2.2, it says that u and v are "over the same alphabet". Does this mean the same alphabet as in Problem 1.2.1, A = {a}?

No, A could be any finite alphabet and u and v are just over the same alphabet as each other. I'll put a rephrasing of this on the errata page.

Question 1.4, 10 February 2006

What's this about a typo in Problem 1.8.5?

Check out the assignment page.

Question 1.3, 9 February 2006

In Problem 1.1.5 (d), should I be modeling the set D as a Java Set, from the Collections package? My first thought on how to count the elements is to use an Iterator.

That's a perfectly reasonable idea, though for this particular kind of set you can program the desired method much more easily. You're told to store the set D by storing the number f(D) as an int -- so that for example if D were {0,2} you would store the number 5. The task of this method is to look at the number f(d) and decide how many elements are in D.

If you were to write the necessary methods for an iterator -- "first", "next", and "hasNext", you could then iterate through all the elements in the set and count them. (If you don't know about Java Sets and Iterators, don't worry about it.) But your task here is much easier. In part (c) you have already written a method to test whether a given element is in the set. From that, it's an easy step to loop through all the possible elements, the numbers from 0 through 15, and count how many are in the set.

Question 1.2, 9 February 2006

In Problem 1.1.5, I was thinking that a given set could only have one number. But you said in the first lecture that the sets {3,4,7} and {3,4,4,7} were the same set. Does that mean that the number for this set could be either 23 + 24 + 27 = 152 or 23 + 24 + 24 + 27 = 168?

No, an element is either in a set once or not at all, even though it could be listed as an element more than once. In this case the conversion from the set D to the number f(D) assumes that we have a proper listing of the set where each member is listed only once and thus contributes only once to the sum.

Question 1.1, 3 February 2006

I'm not sure quite what is being asked for in Problem 1.6.5 -- can you be more specific?

Sure. Your program is supposed to take a natural input (given as an int, since we are using real Java here) k, and produce 2k lines of output. Each line of output is supposed to be a setting of k boolean variables, so there should be k numbers on each line, separated by spaces. If k = 0, then, there should be one line of output, but is should have no numbers on it. If k = 1, there should be two lines (since 21 = 2), the first with "0", the second with "1". If k = 2 there should be four lines, with contents "0 0", "0 1", "1 0", and "1 1" respectively. I hope you now see the pattern -- for k = 3 there should be eight lines from "0 0 0" through "1 1 1", and so forth. Your job is to write a Java method to produce the output. You are not required to compile and run it, but you might be more confident of your answer if you do.

Last modified 12 February 2006