Ternary Operator
A shortcut to assigning one of two values to a variable depending on a given condition |
Like an if-then-else statement |
Question mark comes after the condition |
After the question mark, two values that can return are separated by a colon (:) |
Takes 3 operands: |
condition ? |
operand1 : |
operand2 |
|
Condition we're testing against |
First value to assign if first condition was true |
Second value to assign if first condition was false |
Example: |
|
|
|
|
|
|
is age equal to 20? |
|
if false, isOver18 = false
|
Access Modifiers
These are java keywords |
Allows defining the scope, how other parts of the code can access this code |
Access Modifiers |
Access Levels |
public |
Same Class, same package, other subclass, other package |
protected |
Same Class, same package, other subclass |
no access modifier |
Same Class, same package |
private |
Same Class |
Code comments
These are used to describe methods for quick reference with an IDE |
Start comment block: |
/** |
End comment block: |
*/ |
Describe method: |
Computes sum of two arguments
|
Describe parameters |
|
an int operand |
|
|
an int operand |
Describe what method returns: |
|
the sum of a and b |
Method described: |
|
|
|
Full example:
/**
* Computes the sum of two arguments.
*
* @param a an int operand to be added
* @param b another int operand
* @return the sum of a and b
*/
public static int sum(int a, int b)
For loop
For loops are used when you know exactly how many times you want to loop through a block of code |
|
|
|
|
|
|
|
|
|
sets a variable before the loop starts |
Defines the condition for the loop to run. If true, the loop will start over again, if false, the loop will end. |
increases a value (i++) each time the code block in the loop has been executed. |
For-Each Loop
Used exclusively to loop through elements in an array: |
|
|
|
e.g. |
|
{"Volvo", "BMW", "Ford", "Mazda"};
|
for ` |
|
|
|
System.out.println(i);
|
While Loops
A while loop will execute the enclosed statement as long as a boolean condition remains true. |
Syntax: |
while (boolean_condition) statemen
|
The condition must be boolean. |
If the condition never becomes false, the loop never exits, and the program never stops. |
Example:
n = 1;
while (n < 4) {
System.out.println(n + " squared is " + (n * n));
n = n + 1;
}
Result:
1 squared is 1
2 squared is 4
3 squared is 9
When do you use each loop
for loop |
if you know ahead of time how many times you want to go through the loop. |
while loop |
in almost all other cases. |
do-while loop |
if you must go through the loop at least once before it makes sense to do the test. |
|
|
ArrayList
Create arraylist: |
|
|
new ArrayList<Type>();
|
e.g. |
|
|
new ArrayList<String>();
|
Add object to arraylist |
|
e.g. |
|
Get size of arraylist: |
|
e.g. |
|
Change object with index |
varName.set(index, object);
|
e.g. |
|
Remove object with index |
varName.remove(index)
|
e.g. |
|
Passing as a parameter example: |
public static double average(ArrayList<Integer> x)
|
Arrays
Create array: |
type[] varName = new type[size]; |
e.g. |
double[] arr= new double[5]; //5 objects |
Get length of array: |
varName.length |
e.g. |
arr.length |
Access object with index |
varName[index] |
|
arr[1] |
Reading input
Structure |
Import Scanner class: |
import java.util.Scanner;
|
Create a scanner, assign it to a variable: |
Scanner scanner = new Scanner(System.in);
|
|
new Scanner(..)
creates a new one |
|
System.in says scanner is to take input from the keyboard |
Request user to input number |
System.out.print("Please input data: ");
|
Read in number: |
myNumber = scanner.nextInt();
|
Read in String |
String myString = scanner.nextLine(); |
Read in double |
String myDouble = scanner.nextDouble(); |
Read in char: |
char myChar = scanner.next().charAt(0); |
break statement
Inside any loop, the break statement will immediately get you out of the loop. |
If you are in nested loops, break gets you out of the innermost loop |
It doesn’t make any sense to break out of a loop unconditionally; you should do it only as the result of an if test |
break should not be the normal way to leave a loop |
Use it when necessary, but don’t overuse it. |
Example:
for (int i = 1; i <= 12; i++) {
if (badEgg(i))
break;
}
Inheritance
subclass (child) |
the class that inherits from another class |
superclass (parent) |
the class being inherited from |
To inherit from a class we use |
|
class A extends B |
means class A (subclass) inherits attributes and methods from class B(superclass) |
|