1/13
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Static methods and variables
Static:
variable: shared across the class rather than to individual object/instance (ex: cumulative count of every time user creates a new obj)
method: non static methods can use static vars, but static methods can’t use instance vars.
method call in main class: call ClassName.method() instead of objOfClass.
Public vs Private
Public: variable or method can be accessed from outside classes
Private: variable or method can only be accessed within the class
Classes
main class ex:
public class ClassName {
public static void main(String[] args) {
random class ex:
public ClassName {
}
instance variables
declaring vs initializing
Constructors
allows user to create new objects for that class by filling in different parameters. There can be multiple different constructor versions for one class
ex:
private String s;
private int i;
private double d;
private boolean b;
public ClassName() {
s = "";
i = 0;
d = 0.0;
b = false;
}
public ClassName(String s, int in) {
this.s = s;
i = in;
d = 0.0;
b = false;
}
//etc. can be any combo; just make sure to match same types
//implementing in main class:
ClassName objName = new ClassName();
ClassName objName2 = new ClassName("hi", 5);
getters, setters, and toString
//getter
public int getNum() {
return num;
}
//setter
public void setNum(int n) {
num = n;
}
//toString
public String toString() {
return "number: " + num;
}
// call in main class:
int n = objName.getNum(); //or can print
objName.setNum(5);
System.out.println(objName);
importing - scanner and random
// USING SCANNERS
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name:");
String name = input.nextLine;
// if var = int: input.nextInt; char: nextChar, double: nextDouble
//USING RANDOM
import java.util.Random;
int range = max - min
(int)(Math.random() * (range + 1)) + min
adding strings, ints, and doubles
ex:
String s = “you”
int i = 5
int j = 9
return i + j => 14
return “hi there “ + i + j => hi there 59
return “hi there ” + s => hi there you
converting char/str/int/doub
int can be added to doub, but doub has to be converted to int first if you want to add it to int.
(int)doubleNum => trunkates (gets rid of everything after decimal point. rounds down)
ints and doubles can be turned into str: (String)intNum
DeMorgan’s laws
!(A && B) == !A || !B
!(A || B) == !A && !B
for loops and iterations
for loop:
for (var initialization; boolean expression; increment)
for (int i = 0; i < 10; i++){}
# iterations:
|limit - initialization|/update
OR (range/interval) + 1(if ≤ or ≥)
EX
for(int i = 12; i <= 374; i += 18) =>
374 - 12 = 362
362/18 = 20
20 + 1 = 21
JavaDocs comments format
example
/**
explains what method does
Initializes a Power object.
post + pre conditions to be met
Precondition:
(for getters + setters)
ActivityLog object must be initialized
(for constructors)
Power object must take a String theName and int theStrength
Postcondition: Instance variables String name and int strength are initialized with String theName, and int theStrength.
parameters taken in
@param theName - String to initialize instance variable name for Power object
@param theStrength - int to initialize instance variable strength for Power object
AND/OR: return
@return strength -the strength of the Power object
*/
compareTo
str1.compareTo(str2)
if str1 > str2: +num
if str1 < str2: -num
if str1 = str2: 0
if Strings: will compare by alphabetical order. also case sensitive
ex: s1 = “hello”
s2 = “jello”
system.out.println(s1.compareTo(s2))
^ returns -2 (‘h’ is 2 times lower than ‘j’ bc j comes after h)