===== A Collection of Helpful Tips and Information for CS161 ===== 1. Compile continously. Avoid the bad habit of writing large amounts of code without compiling. After every "logical unit" (e.g. method), stop and compile your code. 2. Run your code continuously. Use print statements to inspect the value of variables to make sure your code is logically correct. 3. Review your code when finished. Is it well-commented? Are all of your variables being used? Should some instance variables actually be local variables? Am I using whitespace to make my code more readable? ===== Thoughts on Programming Conventions ===== In programming, we adhere to many conventions: classes are capitalized, methods are not capitalized, every class (and some methods) should have a Javadoc comment, the exact form of the Javadoc tags are "@author" not "@authors", etc. These are truly conventions in the sense that your code will still compile and run even if you do not obey them. And often, it may seem annoying (or burdensome) to have to remember them all -- especially when you are just learning how to program. However, in the same way that you follow grammatical rules when writing an essay to increase the probability that your meaning makes it across to the reader, so to in CS, we follow conventions to make sure our code is understandable to those reading/using it. When you write code for an airplane guidance system, or an electrical grid, or a radiation therapy machine, it becomes *critical* (in a literal sense) that your code is understandable and clear. Following conventions is one of the biggest ways to make your code understandable. (And, after awhile, it becomes a habit that you don't even think about anymore). ===== BlueJ Shortcuts ===== 1. When you create a new class in BlueJ, it pre-populates your class with code. Delete this and start with a blank class! The pre-populated code will only serve to confuse you. 2. When you have the console open, there are 2 useful options in the "Options" menu: "Clear Screen at Method Call" -- This will clear the console every time you run your code. Otherwise, the print statements will simply accumulate in the console. "Unlimited buffering" -- This is especially important for some of the later homework assignments. This option gives you unlimited room to print. Otherwise, the console is limited to displaying some number of lines. ===== Common Compile Time and Runtime Error Messages And What They Mean ===== === incompatible type === The variable on the left side of the assignment operator (=) and the value on the right side have different types. For example, String str = 5; The variable on the left is of type String and the variable on the right is of type int. You cannont convert one to the other. === cannot find symbol -- method nextString === This method doesn't exist in the class. Either you mis-spelled the method or you are incorrectly remembering the name of the method. Go to the class API and look at the available methods === cannot find symbol -- method lastIndexOf === This method does exist and you have spelled it correctly. There is another syntax error somewhere on this line. For example, int position = lastIndexOf(' '); // incorrect -- you need to use the dot operator int position = name.lastIndexOf(' '); // correct === NullPointerException === An object was not initialized (and thus is null) and you used the dot operator to call a method An array was not initialized (and thus is null) and you used the bracket operator to index [] An object in an array is not initialized (i.e. array[i] is null) and you used the dot operator For example, Random rng; // declared but not initialized. rng holds the value null rng.nextInt(100); // you are calling the nextInt() method on a null variable