AP CSA Unit 2
Yes, but it only checks whether it refers to the same object. Note that == is only for literals, or numbers. Use str.equals(str2) for strings.
Can you use == for Strings?
to test whether two Strings are equal to each other, use the equals method (do not use == operator) or the compareTo method to compare their lexiographic order
Comparing Strings
Runs ("do") a specified subprocess while a specified condition is true.
Do while
Top left
Does array[0][0] point to the array's bottom left or top left?
for (dataType elementVariable : array){ statement; }
Format of enhanced for loop
Both arrays would reference the same actual array.
Given an array "double[] values = new double[6]", would "double[] prices = values" create a new array or point to the same reference as the other?
Array: .length, Arraylist: .size(), String: .length()
How do you find the lenghts of an Array, Arraylist, and String?
Random r = new Random(); double q = r.nextDouble();
How do you generate a random double?
George.get(5)
How do you get value 5 from an ArrayList named George?
for(int i = 0; i< array.length; i++){ for(int j = 0; j< array.length; j++){ number = array[i][j]; } }
How do you iterate through a two dimensional array?
array.remove(position);
How do you remove an element in an array list from a specific position in the array?
ArrayList<String> array = new ArrayList<String>();
How do you set up an arraylist for a list of Strings?
use ==
How do you test if two object references (not values) are identical?
Use a nested loop. for (int i = 0; i < array.length; i ++) { for (int j = 0; j < array[0].length; j ++) { code; } }
How do you traverse through a 2D array?
doubles have a decimal point.
How does a double differ from an int?
Step through the instructions and track the values of each variable.
How does one execute a hand trace?
Int[][] name;
How would you initialize a 2d integer array?
Use Arrays.copyOf(array, n)- continuing with the example one could do double[] prices = Arrays.copyOf(values, values.length); You could also use this to expand an existing array with new slots by creating a copy with n > values.length
How would you make a copy of an array that does not result in a reference to the same array?
Nothing will be stored; the null value represents the lack of an object, not an empty string
If a String variable str1 is set equal to the "null" value, what String literal will be stored in the variable?
Yes, automatically.
If you remove a value from an ArrayList, do the other values shift down?
No
If you remove a value from an array, do the other values shift down?
No. If it is only a single operation, it can be converted to a method.
Is turning a single operation into a class advisable?
Int, Char, Float, Double, Guassian, boolean
Name one primitive type
A loop that is contained in another loop
Nested loop
A set of related classes
Package
Yes
Should the names of classes be capitalized?
No
Should the names of packages be capitalized?
A class that gives access to the use of multi-character words, phrases, etc.; does not require an import statement
String class
True
True or False: a constructor is a method that is automatically called when an object is created
An array of arrays. Represents a table of variables with rows and columns.
Two-dimensional Arrays3
They are a quick way to check if one variable is equal to many things without having to use a lot of if statements
What are switch case statements useful for?
Boolean is easier to use for while loops, for loops allow initialization of variables and conditions, do loops check the condition at the end, so it goes through at least once.
What are the advantages a for each loop?
The name for a class should be a noun and capitalized. It cannot contain special characters or start with a number.
What are the guidelines for naming a class?
Value < 0 = str1 comes before str2 in lexicographic order Value = 0 = str1 and str2 are the same string Value > 0 = str2 comes before str1 in lexicographic order
What are the possible return values of the str1.compareTo(str2) method, and what do they represent?
A break statement breaks out of a loop and any loop that it may be nested in. A continue statement will follow a condition and move to the next iteration of the loop.
What are the purposes of break and continue statements?
1) have a loop that continues until the specific value or values are entered Ex: while (input != -1) 2) create a boolean set to false and have the loop run until a sentinel value is entered, in which case the boolean returns a true and the loop is broken Ex: while (!done)
What are two possible ways to implement a sentinel variable?
BufferedImage
What class are images stored in?
Java.util.Scanner;
What class needs to be imported to use a scanner?
i++
What code increments a for loop with int i?
Relational Operators
What compares values?
Executes certain code for a set number of times (based on a condition)
What does a for loop do?
Executed certain code while a statement is true
What does a while loop do?
You receive an error out of bounds exception
What happens when you call a value outside of an arrays bound.
For each: for(int x: array)
What how would you write a for each loop filled with ints?
Input validation, because it runs at least once. Use a sentinel variable to denote the end of a data set.
What is a do Loop best for?
Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. These are NOT java objects.
What is a literal?
Primitive types are the most basic data types available within the Java language. There are 8: boolean, byte, char, short, int, long, float and double.
What is a primitive value?
A class containing a sequence of characters
What is a string
An arraylist.
What is an expandable list called.
The common programming structure that implements "conditional statements".
What is an if statement?
ArrayList = easier to add and remove things; Array = difficult to add and remove things
What is one benefit of an ArrayList compared to an Array?
They have a fixed size which can't change after initialization.
What is one limitation of arrays?
For a user to denote the end of the data set but not part of the data set itself. Ex: Enter -1 to quit
What is one possible use of sentinel variables?
for(int i=1; i<10; i++){ }
What is proper format for a for loop?
an int stores a number and is a primitive type a String stores an sequence of characters and is an object
What is the difference between an int and a String
value comparison works because each value is a primitive type, while trying to compare object references only work if the object reference is compared to itself
What is the difference between comparing values and objects?
'i' is not declared. It should be 'int i'. Because the value of i is never less than -20, it will never run.
What is wrong with the following code? for (i = 10; i < -20; i ++){ sumRandomCodeHere(); }
X: 0 Y: 1079
What would the X and Y coordinates be for the bottom left pixel in a 1920x1080 picture?
Byte is a class with methods for bytes while byte is a primitive type
What's the difference Byte and byte?
Character is a class with methods for chars while char is a primitive type
What's the difference Character and char?
Double is a class with methods for doubles while double is a primitive type
What's the difference Double and double?
Integer is a class with methods for ints while int is a primitive type
What's the difference Integer and int?
Long is a class with methods for longs while long is a primitive type
What's the difference Long and long?
Short is a class with methods for shorts while short is a primitive type
What's the difference Short and short?
Boolean is a class with methods for the primitive type of booleans.
What's the difference between boolean and Boolean?
Boolean variable
Where can you store the outcome of a condition?
'count' only exists in the while loop, it can not be accessed outside of it. For each iteration of the while loop, 'count' is created again
Where does the variable 'count' exist? while (count != 5){ sumOtherRandomCode(); count++ }
C is the correct answer. A will throw a syntax error (= always goes on RIGHT), B is a boolean, D is an assignment operator.
Which of the following operators is a relational operator? A) =< B) ! C) <= D) =
Input validation is important as all data which is entered into the system needs to be validated in order to ensure that the program will run as intended.
Why is input validation important?
It can store more data
Why would one use a long instead of an int?
fetches private data stored within an object
accessor method
denotes the end of a data set, but it is not part of the data
define sentinel value
Runs when the program went through none of the if/else if statement(s) above; always placed at the very end
else
Checks and runs if the condition is met and the program did not go through the previous if/else if statement(s)
else if
a method that changes the state of an object
mutator method
For loop, while loop, do-while loop, improved for loop/for each loop
name of different types of loops
[Y][X], also as [row][column]
Are arrays indexed [X][Y] or [Y][X]
No, because a String (notice the capital S) is a java object and not a data type.
Are strings a primitive value?
A type with two possible values: true and false.
Boolean type