midterm stuff Flashcards
why do we need getter/accessor methods?
because of private fields. we need them to see whats happening on the console in the runner
where should the main method go? the class or the runner?
runner
true/false: you shouldn’t use == with doubles because of limited storage rounding
true, a double of 1.00000000000000000000001 will read as == 1 when in reality it should be < or >
true/false: a 2d array is just an array of arrays
true
EFFICIENCY:
is the circumference method in:
private double r = 123412312;
public double diameter()
{
return r*2;
}
public double circumference()
{
return 2rMath.PI;
}
efficient? why/why not?
no, as the diameter() method is not used. on an AP test, there is no wasted code. so if you see something like that diameter method, it should be used in the solution to the circumference. its also simpler on you as youre less likely to make mistakes on more complicated already given methods.
is Math.PI a method?
no, property (no parenthesis)
true/false: 2d arrays start both rows and columns at index 0
true
what do the first and second set of brackets in a 2d array:
arr[][]
indicate?
first = ROW starting at index 0
second = COLUMN starting at index 0
two ways of initializing 2d arrays:
- int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}};
- int[][] arrTwo = new int[][] {{1, 2, 3}, {4, 5, 6}};
row-major vs. column major order
row:
row/first bracket iterator = OUTSIDE loop and column is the inside loop
column:
column/second bracket iterator = outside loop and row is the inside loop
what happens when you print a non-string object?
you get the name of the object@the memory location
ex. Store@6bc7c054
what is the purpose of the toString() method?
turning things into strings, particularly objects which, when printed, will only return their name@memory location which is very very not helpful.
true/false: toString is automatically called by java every time you print smth
true, BUT to get the value you might want out of an object you need to OVERRIDE the toString method as so:
public String toString()
{
return (concatenated values and strings here);
}
and can be called with
System.out.print(e);
or
System.out.print(e.toString());
where e is an object,
true/false: toStrings can be overridden to explicitly print basic strings
true, java does this implicitly with
System.out.print(str);
where
String str = “java”;
or EXPLICITLY with
System.out.print(str.toString());
the toString() method is automatically called whenever a print is called (its part of the print method!)
compareTo vs. equals vs. == vs. valueOf()
compareTo() :returns a value negative if string is less than another alphabetically, 0 if same characters in same order, positive if greater
.equals: do they have the same value? (might not work well with custom objects unless overridden, ends up doing the same thing as ==)
==: compares the memory location/if theyre the SAME OBJECT and if a primitive type
valueOf(): returns the string version of a given value (called as String.valueOf(whatever valuae))