Welcome to the AP Computer Science Study Guide. Let's jump right in.
/* * This is * a long comment. */
// This is a short comment.
\n and \" will be the escape sequence you will used most frequently.
int = integer (-2, 147, 9, . )
double = floating point number, twice as much memory than float
char = single character
double square = (int)98.28; // Becomes 98
+= add then assign
-= subtract then assign
*= multiply then assign
/= divide then assign
%= get the reminder then assign
int abs(int num) // returns absolute value
double pow(double num, double power) // returns number to the power
double sqrt(double num) // returns square root of positive number
double random() // returns number between 0.0 & 1.0
Random generator = new Random(); generator.nextInt(n); //return 1 to n-1 generator.nextDouble(); // returns same as math double random()
== means equals (= is the assignment operator)
!= means not equal to
! means not
Example: !true // means not true or false
&& means and
|| means or
Example: if (i < 0 || i >1) // i has to be less than 0 or greater than 1 to be true
Example: if (cuteness > 5)
Example: if (housing != livesWithParents) < girl.giveRealNumber(); >else
Example: int n = 1; int endPoint = 10; while (n
for (initialization; termination; increment)
Examples: for (int i = 1; i for (int i = 0; i
Example: int month = 8; String monthString; switch (month) < case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; >System.out.println(monthString);
Remember String is an object.
string1 == string2 tests for reference equality and is for testing whether two strings are the same object. Use string1.equals(string2) to test for value equality.
The Comparable interface contains only one method, compareTo, which takes an object as a parameter and returns an integer. If you need comparison functionality, you should implement the comparable interface. The String class implements the Comparable interface. Strings are compared character by character. Each character is encoded and stored in binary.
I the first characters are the same, then the next ones are compared and son. This basically does an alphabetization. Think of the string in the parenthesis as "the OTHER string".
Example: String s1, s2; // s1 and s2 are assigned values int result = s1.compareTo( s2 ); if ( result > 0 ) < System.out.println( s1 + " is greater" ); >else if ( result < 0 ) < System.out.println( s2 + " is greater" ); >else
Character values are numbers in the Unicode character set. They are in order, but upper and lowercase is seperator, so try not to use a>b.
final int POWER = 5;
int[] powerLevels = new int[POWER];
Example: for (int index = 0; index < array.length; index++) < // do something here >
Example: // for each digit in the array numbers, goes through the entire array for (int digit: numbers) < // do something here >
String is an array of chars.
Try the following:int[][] multi = new int[5][10];
. which is a short hand for something like this:int[][] multi = new int[5][]; multi[0] = new int[10]; multi[1] = new int[10]; multi[2] = new int[10]; multi[3] = new int[10]; multi[4] = new int[10];
Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:int[][] multi = new int[][]< < 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 >, < 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 >, < 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 >, < 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 >, < 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 >>;
for(int i = 0; i < array.length; i++) < for(int j = 0; j < array[0].length; j++) < // do something here >> //I'd recommend j < array[i] here. Nothing //changes on this case, but it would bring //some trouble if the second dimension of the //array was not the same for every case.
Example: public class Object extends SuperClass < // do something here >
Example: public int factorial (int x) < if (x >1) < //recursive case: return factorial(x-1) * x; >else /*base case*/ return 1; > >
Following the code when it is factorial(3):