Java Reference Card 1. Classes The following is an example of a main class: public class Calculator { public static void main(string[] args) { and the following is an example of a utility class (with no methods): public class Geometry { 2. Methods The following is an example of a method declaration with an empty body: public static double circlearea(double radius) { and the following is an example of an invocation of this method (assuming that it is in the Geometry class): area = Geometry.circleArea(radius); 3. Conditionals The following is an example of an if statement with an else clause: if (price > 100.00) { discount = 0.40; else { discount = 0.10; 4. Operators Arithmetic Operators Logical Operators Relational Operators Addition + And & && Equal == Decrement Excl. Or ^ Greater than > Division / Incl. Or Greater than or equal >= Increment ++ Not! Less Than < Int. Division / Less than or equal <= Multiplication * Not equal!= Modulus % Negation - Subtraction -
4. Type Conversion Example Expression Type Value (1 + 2 + 3 + 4) / 4.0 double 2.5 "1234" + 99 String "123499" 11 * 0.25 double 2.75 (int)2.71828 int 2 (int)11 * 0.25 double 2.75 11 * (int)0.25 int 0 (int)(11 * 0.25) int 2 5. Library Methods/Constants Signature Double.parseDouble(String s) Purpose Convert s to a double Integer.parseInt(String s) Convert s to an int Math.abs(double v) Math.cos(double a) Math.max(double x, double y) Math.min(double x, double y) Math.pow(double v, double p) Math.sin(double a) Math.sqrt(double v) Math.tan(double a) Math.toDegrees(double r) Math.toRadians(double d) Absolute value Cosine Maximum Minimum v raised to the p power Since Square root Tangent Radians to degrees Degrees to radians Constant Math.E Math.PI Value The base of the natural log The circumference over the radius
6. Input Input Using a Scanner Object import java.util.scanner; double d; int i; Scanner in; String s; in = new Scanner(System.in); d = in.nextdouble(); i = in.nextint(); s = in.nextline(); 7. Output The System.out object has the following methods. print() println() printf() Can be passed a double, int, or String Can be passed a double, int, or String Is passed a format string and one value for each format specifier Example Specifier Description %d Integer %5d Integer in a field of width 5 %f Floating-point %f5.2 Floating-point in a field of width 5 with 2 places to the right of the. %s String Complete Example 0123456789102345678901234567890 printf("%2d%5.2f", 5, 8.1) 5 8.10 printf("%10d%8.4f", 5, 8.1) 5 8.1000
8. Arrays The following is an example of an array declaration: double[] grades; The following is an example of allocating memory for an array: grades = new double[3]; The following are examples of using elements of an array (that assumes that all of the variables have been declared earlier): grades[2] = 85.5; final = grades[2]; 9. Loops The following is an example of a for loop (that assumes that all of the variables have been declared for (i = 0; i < n; i++) { The following is an example of a while loop (that assumes that all of the variables have been declared while (i < n) { The following is an example of a do-while loop (that assumes that all of the variables have been declared do { while (i < n);
Linux Reference Card 1. Shell Commands Command Description Symbol Meaning cd Change the working directory * Match any characters cp Copy? Match any one character less Display the contents of a file ~ Home directory ls List the contents of a directory / Root directory man Display a manual entry.. The directory above mkdir Make a directory. The current directory mv Move a file rm Remove a file rmdir Remove a directory pwd Display the path of the working directory 2. Tools Compiling: javac ClassName.java Executing: java ClassName java ClassName < test.in > test.out Comparing: meld test.exp test.out