AP Computer Science Study Guide

Created by Philip La

Welcome to the AP Computer Science Study Guide. Let's jump right in.


Table of Contents

1. Comments
2. Intro to Strings
3. Variables
4. Primitive Data types
5. Using Mathematical Operators
6. The Math Class
7. Random Class
8. Intro to Conditionals
9. Objects
10. Naming Conventions
11. Advanced Conditionals Statements
12. Arrays
13. A more in depth look at strings
14. 2D Arrays
15. Array list
16. Inheritance and Polymorphism
17. Recursion


Comments


                    /*
                     *  This is
                     *  a long comment.
                     */
                

Intro to Strings

Escape Sequence Table

\n and \" will be the escape sequence you will used most frequently.


Variables


Primitive Data Types

int = integer (-2, 147, 9, ...)

double = floating point number, twice as much memory than float

boolean = true/false

char = single character


Using Mathematical Operators

Defining a variable example

The Math Class


Random Class


                Random generator = new Random();
                generator.nextInt(n); //return 1 to n-1
                generator.nextDouble(); // returns same as math double random()
                

Intro to Conditionals

The if statement

                Example:
                
                if (cuteness > 5) {
                    girl.askForNumber();
                }
                

The else statement

                Example:
                
                if (housing != livesWithParents) {
                    girl.giveRealNumber(); 
                } else {
                    girl.giveRandomNumber();
                }
                

The while statement

                Example:
                
                int n = 1;
                int endPoint = 10;
                while (n <= endPoint) {
                    System.out.println(number);
                    n++;
                }
                
Sentinel Value

The for statement


                for (initialization; termination; increment) {
                    statement(s)
                }
                
                Examples:
                
                for (int i = 1; i <= 10; i++) {
                    System.out.println(i);
                }

                for (int i = 0; i < 50; i++) {
                    System.out.println("I will not throw paper planes in class");
                }
                
                

The switch statement

                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);
                
                

Objects

Defining a constructor example
  • Methods do the work and are usually verbs.

  • Naming Conventions


    Advanced Conditional Statements

    String Comparison

    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 {
                            System.out.println( "equal" );
                        }
                    
                    

    Float Comparison

    It's a general rule that floating point number should never be compared like (a==b), but rather like (abs(a-b) < delta) where delta is a small number. A floating point value having fixed number of digits in decimal form does not necessary have fixed number of digits in binary form.

    Character Comparison

    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.


    Arrays

    Array Definition Example

    A more in depth look at strings

    Primitive Data Type - Char

    String is an array of chars.

    Methods of the String Class


    2D Arrays

                    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.
                    

    Array List


    Inheritance and Polymorphism


    Recursion