StudyHub+ 1 StudyHub: AP Java Semester One Final Review
StudyHub+ 2 Terminology: Primitive Data Type: Most basic data types in the Java language. The eight primitive data types are: Char: A single character in the alphabet. A character can be converted into a number value using the ASCII chart Int: A numeric data type for integers. Boolean: A data type that only uses true or false. Double: A numeric data type for numbers with decimals. Byte: A numeric data type in Java. Float: A numeric data type in Java Short: A numeric data type in Java Long: A numeric data type in Java. If you are wondering why there are so many different numeric data types, it is because of saving memory and different storage location. Using an int instead of a long would save several bytes of memory. Floating point numbers and integer numbers are stored differently in a computer. These data types serve as a basic foundation for coding in and data manipulation in Java. Each data type has one purpose, or one specific type or kind of value. Each variable must be assigned a data type. Basic Operations: Addition (+) The basic function to add variables together would be x + y. x += y x = x + y Subtraction (-) The basic function to subtract variables together would be x - y. x -= y x = x - y Multiplication (*) The basic function to multiply variables together would be x * y. x *= y x = x * y Division (/) The basic function to divide variables together would be x / y. x /= y x = x / y Modulus (%) The remainder of dividing two numbers. The basic function to modulus variables together would be x % y.
StudyHub+ 3 The operations occur from left to right. Anything in Parentheses is calculated first. Multiplication, Division, and Modulus are calculated next from left to right. Subtraction and Addition are then calculated from left to right. Example: 3 / 14 % 7 / (1.0 * 2) + 10 / 6 We begin with calculating all the operations in parentheses. 3 / 14 % 7 / 2.0 + 10 / 6 We then look at the equation from left to right and calculate the multiplications, divisions, and modulus first. 3 / 14 is calculated first 0. Then the answer from 3 / 14 is then modulus by 7 7. The answer from the previous bullet point is then divided by 2.0 to form a decimal 3.5. After looking at the operations on the left, we continue on and find another divide operator for 10 / 6 1. 3.5 + 1 Then, we add the two numbers together. 4.5 When dividing, if one of the numbers or both of the numbers is a decimal, then divide it with decimals as an answer. Examples: 3.0 / 2 = 1.5 3.0 / 2.0 = 1.5 3 / 2.0 = 1.5 Strings in Expressions: Anything after a String acts like a String. Examples: 4 + 1 + 9 +. + (-3 + 10) + 11/3 The first step is to solve for the parentheses. 4 + 1 + 9 +. + 7 + 11/3 Then, add the operations before the String. 14 +. + 7 + 11/3 The operations after a String act like a String. Therefore, the addition signs are concatenating the numbers together. Concatenating Chicken + Nuggets results in Chicken Nuggets.
StudyHub+ 4 Thus, the final result is 14.73 (11/3 is 3). Basic Functions from Different Classes: String Class:.toUpperCase(): Converts the whole String to Uppercase letters..tolowercase(): Converts the whole String to Lowercase letters..charat((integer Value)): Determines the character at a specific index / location. The position of a String starts at 0 and ends at one less than the length of the String. String x = ChickeN ; char y = x.charat(0); char z = x.charat(x.length() - 1); System.out.println(y); System.out.println(z); The result is: C N.compareTo((String)) Compares two strings lexicographically Subtracts the first character of the String inside the method by the first character of the String before the method is called. If the first character is the same, the java virtual machine continues to run, one character after the next The characters are converted into ASCII values String chicken = apple ; String apple = chicken ; System.out.println(chicken.compareTo(apple)); The result is -2 because the first character of chicken, a, which turns into 97, is subtracted by the first character of apple, c, which turns into 99, System.out.println( apple.compareto( chicken ); The result is -2..contains((String)) Checks whether a String is located inside another String. Returns true or false. String chicken = Hello my name is Bob ;
StudyHub+ 5 if(chicken.contains( Bob )) System.out.println( So, your name is not Do? ); else System.out.println( Hello, Bob ); The result is So, your name is not Do? because the string chicken does contain the string Bob..equals((String)) Checks whether the string is equal to another String. Returns true or false. String chicken = butter ; String butter = butter ; if(chicken.equals(butter)) System.out.println( Hello ); else System.out.println( No Hello ); The result is Hello because the String chicken does equal the String butter..length() Used to find the length of a String. String joseph = choi ; System.out.println(joseph.length()); The result is 4 because there are four letters in the string joseph.substring() Used to isolate a specific part of a string Begins at index 0 and ends at one less than the number you want String jeffrey = says hi ; System.out.println(jeffrey.substring(0, 6); The result is says h because it begins at the first character, index 0, and keeps going until it reaches index 5, which is h int class: All you really need to know for the finals is Integer.parseInt() Used to convert a string into an integer String number = 5 ; int therealnumber = Integer.parseInt(number); System.out.println(therealnumber + 5);
StudyHub+ 6 The result is 10 because the string variable number is converted into an integer value and stored in the variable therealnumber Math Class: This class includes all the basic math operations e.g. square root, cube root, absolute value Math.abs() This method takes the absolute value of a number double num = 2.867449; double pi = 3.14159265; System.out.println(Math.abs(num - pi)); The result is 0.27414365 because it takes the absolute value of the subtraction Math.max(x,y) Takes the maximum of two integers. Math.min(x,y) Takes the minimum of two integers. Math.pow((value), (raised to this number)) Takes a value and raises it to the power of a certain number Loop Control Variable: Used in order to regulate the number of times the body of a loop is executed, and when to exit the series of instructions. Loops: They keywords while and for both indicate the start of a loop. The body of the loop will be continued for as long as the loop conditions remain true. To prime a loop statement means to set up code (such as methods) before the loop is executed, in order for each test to make logical sense. Example: If the test in the loop is while the user s response is yes, then the loop would have to be primed by getting a response from the user (or creating one) before the loop initializes. The priming statement is typically proceeding the while() method. While Loop: Repeats the body of the loop while a Boolean expression is true. Programmers tend to use the while loop when they do not know how many times the loops will execute. Do While Loop: Very similar to a While loop The Do While loop always executes at least one time because it reads the statements before testing whether the condition is true.
StudyHub+ 7 int count = 5; do { count++; }while(count > 6) This example will run one time because the Do happens first and then the While is tested. For Loop: Contains a header with three possible parts. Declaration / Initialization, Condition, and Change. Before the loop starts, the Declaration / Initialization is executed. Then, it repeats the body of the loop while the Condition remains true. The code in the Change portion is executed each time at the end of the body of the loop. Programmers tend to use the For loop when they know the number of times the loops will execute. Importing Easy Reader: Easy Reader allows the user to read numbers, characters, words, and Strings from the console and from the text file. The following code permits the user to open the standard input screen for reading keyboard inputs. EasyReader console = new EasyReader(); console is the name of the input stream, which the user decides. Examples for Reading Data: EasyReader read = new EasyReader(); int input = read.readint(); String word = read.readline(); char reference = read.readchar(); De Morgan s Law: De Morgan s Law show how to handle the negation of a complex conditional Example:!(x < 3 && y > 2) The result is (x >= 3 y <= 2) because the opposite of! is the absence of it, the opposite of < is >=, the opposite of && is, and the opposite of > is <=.