CIS 211 Frequently Asked Questions

or, What We Forgot To Mention In The Labs


Q:How unreadable can I make my code?

A: It is amazing how unreadable you can make your code. Many of you have implemented the parseLine method in one big tangle of if, try, catch statements. Here is a good hint. If you EVER find yourself writing the same code over and over and over and over and over, you are doing something wrong. The simplest way to abstract over repeated code is to create a new private method and call it instead of repeating the code. Check this out!

Q:I am getting "return needed at the end of the method".

A:Whatever you do, don't fix it by adding a bogus return at the end of the method that returns some random value. This message tells you that you've made some error. Don't patch it by making another error. The most common case of this error is:

int m (int x) {
  if (x == 0) return 1;
}
The method won't compile because you are not guaranteed to always return an int. What happens when x is not equal to 0?

Q:Exceptions again

A:Do not write code like this:

int m () {
 try { s1; } catch (E1 e) { s11; }
 try { s2; } catch (E2 e) { s22; }
 try { s3; } catch (E3 e) { s33; }
}
This tangling of "normal" behavior with "exceptional" behavior is unreadable. A much better way is to write:
int m () {
 try { 
   s1; 
   s2; 
   s3; 
 }
 catch (E1 e) { s11; }
 catch (E2 e) { s22; }
 catch (E3 e) { s33; }
When somebody reads the method, they can concentrate on the common case (with no exceptions). This is much more important for readability than adding silly comments all over the place.

Q:Can I use == to compare strings?

A:NO!!!!! Try the following program:

class StringEqTest {
  public static void main (String[] args) {
    String s1 = new String("ABC");
    String s2 = new String("ABC");
    System.out.println(s1 == s2);
  }
}
The operator == compares the two values s1 and s2 for equality. The first value s1 is an object; the second value s2 is a DIFFERENT object. The fact that the two objects were initialized with a similar value is irrelevant.

Q:I am getting errors about invalid filenames when running the shell interface

A:The filenames you give to the run/step commands of the shell must be valid filenames of the host operating system. So if you running on Windows use \ as the separator.

Q: What about the missing files in Homework 7

A:Your homework won't compile or run unless you first copy my class files. Please copy all of my class files to your working directories. Then you can compile your code, and test the textual interaction with the machine as shown at the beginning of the lab 7 instructions.

Netscape has had problems downloading the jar file, so use an ftp program like WS_FTP. Log in to ftp.cs.uoregon.edu with the login name 'anonymous' and use your e-mail address as the password. Choose the pub directory, then the sabry directory. The duckMachine.jar file is the only file in that directory.

Q:What about the exceptions ObjectFileE and EOF that we have to use in Lab 7

A:Here are the definitions. I should have made them available earlier:

package duckMachine.operatingSystem;

/**
 * Exception raised to terminate parsing of object files and the 
 * textual shell loop.
 * @author Amr Sabry
 * @version jdk-1.1
 */

public class EOF extends ShellE {}

package duckMachine.operatingSystem;

/**
 * Exception raised for various errors that occur when parsing 
 * object files.
 * @author Amr Sabry
 * @version jdk-1.1
 */

public class ObjectFileE extends ShellE {
  public ObjectFileE (String s) { super(s); }
}

Q:My version of lab assignment n didn't work, and I can't test my current code without it. I'm doomed!

A:No you aren't. If you didn't get one of the previous assignments working perfectly, the best thing to do, of course, is to go back and make it perfect. In a pinch, however, you can use the binaries available from the class web page.

Grab the duckMachine.jar file via anonymous ftp at ftp.cs.uoregon.edu, in the directory /pub/sabry (make sure you set the file type to BINARY). Save the file to some temp directory (WARNING: don't save it to the directory where your own project is!).

Now unpack the archive file with the command "jar xf duckMachine.jar ". After the file is unpacked copy the .class file you need to the appropriate directory, and compile the test file only. For example, if you need a working ioUnit.class to test with your ExecIns code, copy the ioUnit.class file to the architecture directory, and then in the directory above duckMachine, type "javac duckMachine/operatingSystem/TestExec.java ", rather than "javac *.java".


Q:Intermittent I/O on Win95: Some of you have reported bizarre behavior on Win95 when running any test program that uses the ioUnit (TestIO, TestExec, etc).

A:This is a known jdk bug on Windows. It does not happen on Solaris.

Q:What should be done inside the method visitInIns(InIns i)? What does "inputs an integer value and stores it in location X" mean?

A:Ignore the word "inputs". You are going to use the methods of the ioUnit of the Machine to get a Word. You will then store it in Memory at the Address given by the Instruction i.


Q:In homework 4, you talk about a script, what's that?

A:Just print the output you get from running the TestIns class.

Q: I'm getting compiler errors like "class AddIns must be in a file called 'AddIns.java'", but it is in that file. What is going on?

A: Backslashes. When you compile on the PC, you must type "javac duckMachine\architecture\*.java".


Q: The InsVisitor file won't compile.

A: It appears that when I tested the lab, I did it in a different environment in which some old class files were present. Anyway, here is the fix. Copy this new InsVisitor.java file and use it instead of the one you downloaded. Also don't attempt to compile the files in the operatingSystem directory.

Q: What's supposed to happen when I run TestIO?

A: Here is a sample run:

> java duckMachine.architecture.TestIO
22
22
33
33
10000
10000
x
duckMachine.architecture.ioE: Illegal input
You type a number; the program prints it back. You type something other than a number, you get an exception.

Q: I get a -2 for everything I type. What's going on?

A: Your code probably looks like:

public Word readData() throws ioE {
  ...
  return new Data(s.nextToken());
  ...
}
The problem is that the method nextToken does not return the next token. If you read the API carefully, you will see that the method nextToken returns the type of the token read.


Q: Do I need to use exceptions in lab 2?

A: No; your job is to implement the interface ALUI which does not have exceptions. If you change the declarations of the methods in any way, the compiler will tell you that you are not properly implementing the interface.


Q: How do I run a class that is part of a package

A: Say that the class you want to run is TestMemory. That is, your main method is in the file TestMemory.java. Also, let's say that TestMemory is part of the package duckMachine.architecture. So, TestMemory.class should be in the architecture directory, which itself is in the duckMachine directory, which is in some directory (or on some drive) which we will call X.

First, go to the X directory. If you are in the X directory, a 'dir' (or 'ls', if you are running Unix) should display the duckMachine directory (with anything else that may be in the X directory). From here, type 'java duckMachine.architecture.TestMemory'. (Please don't type the quotes.)


Q: How do I compile packages

A: Like above. Go to the directory X and type 'javac duckMachine\architecture\*.java'.


Q: How does this faq page work?

A: If (when) the staff receives the same question several times, and that question is answerable with a few sentences, then it is worthy to be put on the faq. Most of the questions that are answered here will be general Java questions, or clarifications on the homework. Any late-breaking news or changes to the homework will probably be put on the announcements page.

Amr Sabry, John Fiskio-Lasseter, and Christopher Heino all have access to add to this page, although it is likely that Chris will be the one modifying it regularly.


Last Modified 21 January 1998 by
Lord Christopher Heino
cheino@gladstone.uoregon.edu