KNOW THE COMMON PITFALLS!! Like the following.
Type conversion errors – refer to 2019 FRQ #2 and 2018 FRQ #1(b). In these questions, you have things like
- public class SomeClass{
- public int someData;
- public int someOtherData;
- /* some constructor things */
- /** Returns the ratio between someData and someOtherData */
- public double someMethod(){
- // does something
- return ___________;
- }
- }
In the blank they ask for a datatype “double”. Integer division truncates, and you should ALWAYS check for types. Line 10 should be
- return someData / (double)someOtherData;
or equivalent forms.
Also, another pitfall lies in ArrayList datatype’s remove(int) method that removes the object at a certain index. Like:
- public class MyClass{
- private ArrayList myList;
- /* some constructor things */
- /** Removes all «1»s in myList.
- public void removeAllOnes(){
- // CODE HERE
- _____________
- _____________
- }
- }
Remember that the “remove” method also shortens the overall length, so you CANNOT DO THINGS LIKE THIS:
- for(int x = 0; x < myList.size(); x++){
- if(myList.get(x) == 1){
- myList.remove(x);
- }
- }
You either do this:
- for(int x = 0; x < myList.size(); /* NO INCREMENT HERE */){
- if(myList.get(x) == 1){
- myList.remove(x);
- } else x++;
- }
OR this:
- for(int x = myList.size() – 1; x >= 0; x–){
- // go reverse.
- if(myList.get(x) == 1){
- myList.remove(x);
- }
- }
EVERY YEAR PEOPLE MAKE MISTAKES ON THESE. BOTH IN MULTIPLE CHOICE AND FRQS.
BE CAREFUL ON THESE PITFALLS.