Random+Tips

This page is for random tips or reminders that may be useful when writing a program. Add anything you can think of (especially if you don't know what else you could contribute to this wiki).

How can we switch the values of integers x and y? code format="java5" //Create a temporary variable that can store the value for x: temp int temp; temp = x;   //set your temporary variable to x x = y;       //set x equal to y, so now x has a new value y = temp;   //set y equal to temp, so now y is equal to x's initial value code Do __not__ write all of your code and check it by trying to compile it. It will take you much longer trying to let the compiler, which is not always accurate in telling you where the mistake occured, show you your mistakes than if you simply check it on your own. If you still cannot fix it, have someone else look at your program, a fresh set of eyes can pick things out that you aren't seeing.
 * How to swap variable values:**
 * The worst mistake that people make.**

Check each section of code before you move on to another part to avoid lots of pain later.
 * Advice for Beginners:**

It is important to remember "==" (double equal signs) are only used in a comparison. One equal sign ( = ) is used to initiate the value for a variable.

Method names have after.

Set BlueJ error screen to clear at each execution, otherwise, one might think there are errors when the program is perfectly fine.

Remember that some methods written in one IDE, such as Greenfoot, may not be in another, such as BlueJ. Ex: isKeyDown;

Remember that Java is case sensitive, one accidental uppercase letter can screw up your entire program

Don't use keywords for anything in your program or else your program won't compile correctly. These words are meant for a specific purpose and using them out of context can be disasterous.

When naming variables, give them a name that actually means something. This way, it is harder to forget what you are using for something and also makes it easier for an outsider (any one other than you) to know what that variable is for.

Write out your program in pseudo-code first. Write down the steps that your code will need to go through. After that, then try writing the code to follow the pseudo-code.


 * What to do if my program won't compile/work properly:**

First, check your program for the most common mistakes: - forgetting a semicolon after a line of code - too few or too many brackets - confusing "=" with "==" (= sets a variable to that value, == checks if two values are equal) - if you are using recursion, make sure you have a base case - create a default case for when using switch statements - remember Java is case sensitive; bob does not equal BOB - sometimes restarting the Blue J or Greenfoot, will actually fix a program that just won't compile. - remember that a method or variable from one class only exists in that particular class. - make sure that everything is spelled correctly. Spelling is also an important part of programming.


 * Determining your approach to creating a program:**

You can't just write a giant heap of code, press compile, and expect for your program to work perfectly everytime. Writing a program takes more deliberation and planning. When I'm making a program, I determine what type it is (like objectdraw or scanner). Once I've figured that out, I utilize one of my old programs of a similar type as a template. After gutting it, I consider the main objectives of my program. Then I break the main objectives up into step by step pieces, sometimes these are method frames and other times they are code blocks within a method like a loop framework or simply a method call. Once I've done that, I create my variables and add in the particulars. With each step, I mentally verify that my code is logical, leaving no gaps or unaccounted possibilities.


 * Here are some things you might want to look over before the AP exam to make sure you know them:**
 * boolean, int, and double primitive data types
 * use of arithmetic (+, -, *, %, /), logical (&&, ||, !), relational (<, >, <=, >=, ==, !=), and incremental (++, - -) symbols
 * string concatenation
 * Printing
 * one dimentional arrays
 * conditionals
 * recursion: loops (for, for each, while)
 * classes, interfaces, and abstract classes
 * methods: constructors, mutators, and accessors; public and private, static methods
 * variables: instance (always private), constants (static final),


 * Design/Organizing Your Code:**
 * Use reasonabl/consistent style with proper indentation and spacing between statements.
 * Input your own comments, preconditions, or postconditions although they're not required.
 * Choose appropriate names for your class, methods, their parameters, and variables.
 * Put all constructors and public methods first. In addition to specified constructors, it's a good idea to provide a no-args constructor.
 * Make all instance variables private and group them all together.
 * Specify the appropriate return types for all methods.
 * Group all accessors together in a group and all modifiers in another group o make your class definition more readable.
 * Double-check and make sure all brackets, semi-brackets, and parentheses are closed to prevent simple errors.