AP CSA Using Classes and Objects
AP CSA Using Classes and Objects
a. Math.abs(14/5);
b.Math.pow(14,5);
Answer: C would be the only option that fits your needs because you don’t want an absolute
value for your number of chocolates. It has to be accurate, and you don’t need exponents either.
You just want to find out how to split the chocolate amount. So, this would be your answer.
String Class
A string is also an object data type, which means it has a class with many methods
that describe the behavior of an object.
Here are some commonly used methods for the AP Exam:
Ex: Consider the following code segment, What will be the value of n?
String t = s.substring(5);
int n = t.indexOf(“the”);
Answer: The value of n will be 3. The question is asking for the value of n, which is the indexOf
“the” in String t, which is essentially just a substring of string s. Determine t first, which is the
substring that begins at the index 5 of s. Start at 0 and count the spaces as different characters.
Index 5 is the letter i, at the beginning of the word is. Since string t is a substring of s, the
sentence for this substring would be “is the beginning”. the index of “the” is 3 because it begins
at index 3.
There are a lot more methods that are in the String class. However, these are just the
main ones.
Remember that the data in the String class is just an array of characters. This
means that String’s have similar functions to arrays.
IndexOutOfBoundsException’s occur when the index that you use in any of the
methods above goes beyond the index of the String.
The String class is an immutable class. This means that if you would like to change
the values or content of a String object the new contents need to be rearranged to
help find the variable.
Note: When using methods found in any class, you should remember that parameters
that are used to call the method need to match the data types of the arguments that
are found in that specific method.
Make sure that parameters match the correct data types so there are no issues with
the execution of the code.