1 AP CS - Rodriguez Front Basic Setup Java Quick Sheet ~ 35 Flashcards Back public class HelloWorld public static void main ( String arg[] ) // end method main // end class HelloWorld Print Line System.out.println( "Hello everyone out there! "); System.out.print( Hello ); Concatenation System.out.println( "Hello, " + name +! How are you? ); Print Characters Increments/ Decrements Integers \t insert tab \n insert new line \" insert double quote \' insert single quote \\ insert backslash i--; i++; --i; ++i; i+=2; or i = i + 2 i*=3; or i = i * 3 int a = 21; Double Boolean Strings Arrays double b = 2.15 boolean ison = true; String name = new String( "Ms. R" ); String name = ""; int[] ListOfItems = new int[7]; String[] ListOfItems = "zero", "one", "two" boolean[] TFList = true, false, true, true; Array Length MySong[] playlist = new MySong[4]; int listlength = myarray.length; //Number of elements in array Boolean Operators < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to!= Not Equal to Comparison && "and" Both must be true "or" One must me true
If-Else Statement Comparing Strings Searching and Comparing Array of Strings Modulus 2 AP CS - Rodriguez if( a < b ) System.out.println( "a is " + a + " and is less than " + b ); else System.out.println( "a is greater or equal to b ); String name = new String( "Ms. R" ); if( name.equals( "Ms. R" ) ) System.out.println( "Username Correct" ); // end if for( int i =0; i<food.length; i++) if( food[index].equals( "Apple" ) ) System.out.println( "We found an apple!" ); // end if % Remainder Ex. 15 % 2 = 1 Ex. 3 % 10 = 3 Ex. 15 % 5 = 0 Ex. 20 % 3 = 2 Even numbers if( num % 2 = 0 ) Odd numbers if( num % 2!= 0 ) For Loop Initial condition increment/decrement i--, --i for(int i = 0; i < max; i++) <Code/statement> Ex. for(int i = 0; i < array.length; i++) System.out.print( array[i] ); While Loop (Array) int i = 0; while( i < songs.length ) System.out.print( songs[index] ); index++; //end while If Else if( counter%2 == 0 ) // System.out.println( counter + " is an even number" ); // end if else System.out.println( counter + " is an odd number" ); // end else
3 AP CS - Rodriguez Math Random //Math.random() returns a number from 0 to.999999 // Random number between two values (Min and Max) 1-10 int min = 1; int max = 11; //add one more to the max int randomnum = min + (int) ( Math.random() *( max - min ) ); Casting double num = 2.7; int newnum = (int) num; // this would make newnum 2 //it does NOT round up, it casts it off Array lengths Calling items in array Compound Assignment operators int index = 0; while( index < songs.length ) System.out.print( songs[index] ); index++; //end while x += 3; same as x = x + 3; x -= 3; same as x = x - 3; x *= y; same as x = x * y; x /= y; same as x = x / y; x %= y; same as x = x % y; x *= y + 1; same as x = x * (y + 1); String String name = new String( Rodriguez ); //index starts at 0 System.out.print( name.length() ) //prints 9 System.out.print( name.substring(3) ) //prints riguez System.out.print( name.substring(3, 6) )//prints rig (do NOT include 6 th index) System.out.print( name.substring( name.length() 1 ) ) // prints z, the last index System.out.print( name.substring(4, 5) )//prints i (do NOT include 5 th index) For each loop for( Type nickname : Array/List) <Code/statement> Ex. for( doubles num : MyFavNums) <Code/statement>
4 AP CS - Rodriguez 2D Array //(2D array with 5 rows and 4 cols) int[][] MyArray = new int[5][4] // The 2D Array is empty //myarray[row][col] //Fill up Array one by one myarray[0][0] = 1; myarray[0][1] = 12; myarray[1][0] = 15; myarray[4][3] = 13; //Access a value in the array System.out.print( myarray[3][4] ); // prints the item in row 3 col 4 int totalrows = myarray.length; // Number of rows int totalcols = myarray[0].length; // Number of columns Filling up a 2D array with Nested for loop Nested Loop (2D Array) Right to left Nested for each loop (2D Arrays) for( int row = 0; row < myarray.length; row++ ) for ( int col = 0; col <myarray[0].length; col++) myarray[row][col] = 2*(row)*(col); //you can put anything here for( int row = 0; row < myarray.length; row++ ) for ( int col = 0; col <myarray[0].length; col++) <Code/statement> System.out.printnl( ); //adds a new line for every row for (int[] row: MyArray) for (int col: row) System.out.print( row[col] );
5 AP CS - Rodriguez ArrayList **import java.util.arraylist; ArrayList<String> nums = new ArrayList<String>(); nums.add( zero ) nums.add( new String( "two" ) ); nums.get(0) //Gets element in index 0) nums.remove(0) //Removes elements at index 0) nums.set(1, one ) //Set index 2 to a new String) nums.size() //Gets size of ArrayList or the number of elements) nums.get(1).length() //Get the length of the item in index 1 one, // the length is 3 //Make an ArrayLust of objects ArrayList<MyDevice> inventory = new ArrayList<MyDevice>(); inventory.add( new MyPhone( 64, Blue ) ); Memory and color ArrayList of Objects Ex. ArrayList<MyDevice> inventory = new ArrayList<>(); inventory.add(new MyPhone( 64, Blue ) ); //Memory and color For loop ArrayList Find min or max in array of integers ArrayList<String> nums = new ArrayList<String>(); for(int i = 0; i < nums.size(); i++) System.out.print( array.get(i) ); //prints element in index i int max = 0; for(int i=0; i < myarray.length; i++) if( i == 0) max = myarray[0]; if( myarray[i] > max) max = myarray[i];
6 AP CS - Rodriguez The Big 3 1. instance variables 2. constructors (zero-arg and multi-arg) 3. tostring() method The Dynamic Duo 4. getter methods 5. setter methods The The Brain (public or private) 6. processor method(s) (algorithmic processing) Instance Variables Constructors Zeroargument Multiargument tostring() Getter Setter Methods are accessed by tostring() or item.getprice() or getprice() (if used within the class) public class Shoes // Instance variables private String brand; private int size; //Constructor (Zero-Argument) public Shoes() brand = new String("none"); size = 5; //Constructor (Multi-Argument) public Shoes( String brand, int size) this.brand = brand; this.size = size; //tostring() public String tostring() String output = new String(); output = "Brand: " + brand + "Size: " + size; return output; //Getter public int getsize() return size; //Getter public String getbrand() return brand; //Setter public void setsize( int size) this.size = size;
7 AP CS - Rodriguez Brain Example Brain Example Signature //Other Methods/ Brains public String wideaval() if(size > 5 && brand.equals("jordans") return "Does not come in wide sizes"; else return "Avaiable in wide"; //Another Brain example public double gettotal(int price) double total=price + (.08) *(price); //taxes if(size > 6) total += 9.99; //for shipping return total; Inheritance extends This extends a class to inherent instance variables but NOT methods Must us super(), super(args), super.tostring(), super.gettotal() to use super s methods Interface Abstract implements abstract This allows for multiple classes to have a similar method, connecttobluetooth() but each class can have a different code to implement it. Abstract Class: This makes a class abstract ( existing in thought or as an idea but not having a physical or concrete existence ). You cannot make an object in the driver of this class
8 AP CS - Rodriguez Unified Modeling Language (UML) Class Diagram