University of Toronto - Fall 1997
Department of Computer Science

CSC 148H: INTRODUCTION TO COMPUTER SCIENCE



Announcements and Advice about Assignment 2

Thursday 29 October: No electronic hand-in

Just to clarify: You are not to hand in anything electronically with this assignment. Everything is to be on paper. (Please, no disks either!)

Thursday 29 October: "ClassFormatError" during integration testing

Some of you have run into a ClassFormatError. Our TA Karen Reid has had some success dealing with it -- here is her advice:
> We successfully fixed this in one case by
> creating a "Default Package", copying the ListWithCurrent and Node classes
> into the default package, exporting those classes as .class files using the
> option "use one directory" (I might be paraphrasing).  This seemed to work.
> 
> A little more detail:  When you start up visual age you have to import your
> java files into a package.  If you create a package that you name yourself,
> I think the package name gets attached to the class and netscape's version of
> java gets confused.  So the package that you import your classes into should 
> be a "Default Package" which is one of the options on the create package 
> window.
> 
> I'm not sure if the "use one directory" option is neccessary.

Let me emphasize that the integration testing is not worth a great deal on this assignment. Just tell me what your experiences were with it: what problems you encountered, how you [tried to] fix them, etc. If things worked, tell me about what convinced you that everything was working.



Tuesday 28 October: Integration testing -- if you have a problem

If you try loading the html page and see no applet at all, then Java couldn't start the applet up because of some error when reading the input tape and turing machine program, creating two ListWithCurrents, and displaying everything.

If you pull down the Options menu in Netscape and select Show Java Console, you'll get a window that gives you more detailed error messages.



Tuesday 28 October: Integration testing -- get rid of my Node.class file

I accidentally included a file called Node.class in the original set of class files for you to download for integration testing. You do not need this file -- it's for a node class used by my own version of ListWithCurrent -- and it may interfere with your own Node class. So if you've already copied my Node.class file, delete it.

Tuesday 28 October: Integration testing ready

Everything you need to do the integration testing is now available on the main Assignments page.

Tuesday 28 October: Tape not highlighted in applet?

When you run my Turing Machine Simulator, you may not see a highlighted spot on the tape. This is a bug, and I probably won't have time to fix it. You can still follow the simulation though, because the "current" spot starts out at the leftmost character on the tape. After that, it is always the tape that shifts; the current position stays fixed. (Note also that the current position is at the middle of the tape's little window.)

Tuesday 28 October: Should you write a constructor?

Question: I was just wondering if we should be creating another public method as a CONSTRUCTOR for class ListWithCurrent in assignment 2. Is this necessary or should it be left out?
Answer: The only rule is that you can't change the interface to the class. That means you can't add a constructor that takes parameters. That leaves you with two choices: (1) Don't write a constructor; rely simply on the default constructor, or (2) Write a constructor that takes no parameters.

Monday 27 October: isFull again

I said earlier that you can check to see if there is enough memory to construct a new Node, but that even this isn't foolproof. It's okay if you take the easy way out and always return false. That is, always say that the list is not full. You should warn the user though that the method may falsely say that there is room when there isn't (although it will never do the opposite).

Why would we bother to put an isFull method into the class if even the fanciest implementation of it is not foolproof? Because we may change the implementation to something the really can get full and really can be checked for fullness, like an array. Because isFull was already in the interface to class ListWithCurrent, client code should already be using it, and so shouldn't have to change if we change the implementation of ListWithCurrent.



Monday 27 October: resetCurrent

The comments on resetCurrent say: "Set the current location to be the first element in my list." This means that the method should change which element it is that the current refers to. It should not change the list itself. (Some students have thought that the method should delete all the elements before the current one, so that it then becomes the first element. That is not what is intended.)

Friday 24 October: The isFull method

Since ListWithCurrent's isFull method isn't called by the simulator, you get to decide what it does.

It is possible for a dynamic data structure to fill up: what if there is no more memory? You can try constructing a new node. If new fails, you can say that the ListWithCurrent's is full, otherwise it's not. However, this still isn't a perfect solution: Between calling isFull and calling an insert method, memory may fill up (or some memory may become available when none was previously). There's nothing you can do about that.



Friday 24 October: Doing input in Java

Some of you may not have seen yet how to do input in Java, and you may want to do input in your test driver. Below is an example of code that reads an integer and stores it into a variable called num. There are copious comments to explain how everything works. Yes, input is painful in Java.
      // The code inside this method may cause an "exception" to happen
      // if something goes wrong during the input.  The specific kind of
      // exception is an "IOException".  Whenever a method may cause an
      // exception, you have two choices: handle it, or throw up your hands
      // and pass the problem on to whoever called you.  This code does
      // the latter.  The "throws IOException" part declares that this is so.
      // Since this is the main method, there is no other method to handle
      // an exception that it throws.  If this happens, the whole program
      // is stopped and an exception message is printed.
      public static void main (String[] args) throws IOException {
        
           // We construct an object of type DataInputStream, and make
           // it ready to read from the standard input (the keyboard),
           // which is what System.in refers to.
           // "System" is a class defined in Java.lang.
           // It has a static member called "in" which is an InputStream.
           DataInputStream stdin = new DataInputStream(System.in);

           // We can't read right into an int unfortunately.
           // Instead, we read a whole line into a String.
           String inputLine = stdin.readLine();

           // Then we convert it to an integer by calling the static method
           // parseInt, defined in the Integer class.  It takes a string
           // and gives back the corresponding int.
           int num = Integer.parseInt(inputLine);
      }
For more details on these classes and methods, see the online API documentation (linked from the main 148 web page.)

Friday 24 October: Comparing Objects

Some of the methods (isMember and delete) require you to compare a given Object o to the Objects in your list. In all cases, you should use "==" (that is, compare the references; check if you have two references to the very same object) rather than ".equals" (which would compare if the two Objects have the same contents).

There is no place in your class where you ever have to look inside an Object in your list.



Friday 24 October: Your test driver, and printing

Since your ListWithCurrent class can store any kind of Object, you can choose any kind of Object to use when doing your testing. For example, you can make a ListWithCurrent consisting of Strings, or of LiveCells, or any other kind of Object. You can even mix different kinds of Object in the same ListWithCurrent.

What you cannot put into a ListWithCurrent is a primitive such as an int or boolean. However, every primitive type in Java was a "wrapper class" named after it. Eg: Integer, Boolean. These just store a value of the primitive type, but wrap a class around it. Thus, you can put an Integer (although not an int) into a ListWithCurrent.

Below is an example of a small main method that shows how you can put a String or an Integer into a ListWithCurrent:

   public static void main(String[] args) {
        ListWithCurrent l = new ListWithCurrent();
        l.insertAtFront("hello");
        l.insertAtFront("goodbye");
        l.insertAtFront(new Integer(368));
        l.print();
   } 
The body of print can simply say something like:
  	System.out.println(  [the Object that's inside a node]  );


Wednesday 22 October: Problems executing the applet?

Question: I can't seem to find a "step" button on the applet page, and so I can't "get a sense" of how a Turing Machine works.
Answer: The applet may not work on your platform/browser. Try it from the cdfpc lab.

Note that you can do everything but the integration testing without knowing anything about Turing Machines.



Monday 20 October: Understanding class ListWithCurrent

Before you implement the class, you must of course understand exactly what each method is supposed to do. You can (and should) reach this understanding without thinking about the Turing Machine simulator at all. It's just one example of a program that uses a ListWithCurrent (in fact it uses two). But the class is much more general than this, and you can't possibly think through all the possible uses to which it might be put. Nor should you. Focus simply on what the specifications for each method say -- what each method has to do.

In order to understand the methods, it's a good idea to imagine that you have a ListWithCurrent, and make up some method calls to it. You can just use letters like "a" and "b" to stand for the objects that go into the ListWithCurrent. Trace through the calls, following the specs to see what should happen. Do not at this point draw a doubly-linked list. That's just one possible implementation (although it is the one you must use). Instead, think of ListWithCurrent as an ADT, and draw it using some sort of picture that doesn't imply any particular ADT. Eg:

             curr
       ---   ---   ---   ---
       |a|   |b|   |c|   |d|
       ---   ---   ---   ---
for a ListWithCurrent containing four objects a, b, c, and d, in that order from the front, and with current at the b object.

Monday 20 October: Why are so many parameters of type "Object"

There is no ListWithCurrent method that has to look at the items inside of it, so class ListWithCurrent doesn't need to know or care what sort of items they are. It just declares, eg, that insertAtFront is expecting to receive some sort of Object -- any sort will do.

The objects inside a ListWithCurrent are black boxes as far as it is concerned. This is analogous to you running a warehouse in which you store crates full of stuff. You don't need to know what's in the crates in order to stow them away and retrieve them later.

Of course, any program that creates its own ListWithCurrent will have particular kinds of object that it constructs and then inserts into the ListWithCurrent. But because every kind of object in Java is a descendent of the general Object class, it's okay to pass any specific kind of object to a method that's expecting an "Object".



Monday 20 October: ListWithCurrent's "delete" method

This method is not called by our simulator. I neglected to say so in the outline for the class that was originally posted.

Monday 20 October: Creating additional classes

You will probably want to create some sort of node class for holding the contents of a single node in your doubly-linked list. This is just fine. Do hand it in.


Back to the csc148 home page