AP Computer Science Study Guide Take 2
Introduction
This guide was started in a previous article with comments for language
elements an AP Student should know and be able to give a code example. This
incantation of the same topic fills in most of the blanks with coding examples.
If you were a good student you should be able to compare yours to mine. If
you are a lazy student (meaning you didn't try to add any of your own code
to the study guide) you better look through the code examples carefully and
make sure they look familiar to you.
How to use this guide:
How to use this guide:
- Pull the code into your IDE in its own project and run the program.
- Generate the javadoc for the project. There should be a menu command to do it. In netbeans in was found in the run menu. For eclipse users if you can't find a quick web search should give you an answer.
- Read through the code. The comments tell the story of what is going on.
- Print out
- the JavaDoc
- the code
- the output
- Place them side by side on a large table and look through line by line and find where things are happening and why.
Things to look for
Different Types of Java Comments
The AP overview indicates you should be aware of the various types of comments.
The most complex is the Javadoc style block comment. It contains @param and @return
metafunctions in them. These comments start with /** and have *'s beginning each
line with a terminating */ (on the last line). If they occur just before a method or class
declaration the text within the comment will appear in the generated Javadoc.
There are 2 other forms of comments one the // is used extensively in the
APSubset code. The regular block comment /* . . . */ is shown only once.
The reason to point this out to you is that large portions of the code have to be used to engage the Java comments in an insignificant way. Where most of the java code examples can be accomplished with a few lines of code and a few output statements.
The reason to point this out to you is that large portions of the code have to be used to engage the Java comments in an insignificant way. Where most of the java code examples can be accomplished with a few lines of code and a few output statements.
Method overloading
This functionality is spread in 2 different sections of code. The reverse method
is defined twice. Once for Strings and then for integers. It is then used in the
main method showing how the call signatures are the same and only the parameters
change.
Exceptions
This could almost be done within the main method alone except that you also
need to know about throwing an Exception. For this I created a small method that
looks at its 'int' input parameter and throws an Exception if the parameter is
the 'int' 3. Otherwise it just prints some diagnostic text to let you know it
was called. The AP document was not too specific about which 'Exception's you
should know about. I picked 3 that I thought you may have come across already
in your projects. The fourth 'Exception' used is the parent class for Exceptions.
Using this class is a useful way to catch Exceptions when your not sure which
ones are likely to be thrown.
The reason for try-catch-finally and 'Exceptions' are to allow a program to either die gracefully or recover from an error. They are used extensively in large programming projects and Java does a nice job of integrating them in a modular way.
The 'finally' clause is run after every exception 'catch' clause has completed. In the code set up in APSubset the try catch block is run in a loop. The loop is set up to simulate a different error during each iteration. In a more robust program the try-catch block may be around your entire code. Remember most of the time Exceptions are things to avoid in the design of your code. You put them in because you may not be aware of every error scenario that could occur. In a small example such as this I had to force things to happen in a very small space. The idea behind 'finally' is that it gives you a guarenteed exit point from any Exception. A place where you can reset things or decide that the error involved was too grave and you must terminate the program. It's a place where common error handling or recovery can be done.
In the exception code there is a line commented out. It does a divide by zero but it is outside the try-catch block. Uncomment it in the IDE and run the code. You should find that the program will terminate there and no other statements past that point will be executed. Now compare that with the way the zero divide works inside the try-catch block. That should give you an idea of the utility of the Exception handling facilities in Java.
The reason for try-catch-finally and 'Exceptions' are to allow a program to either die gracefully or recover from an error. They are used extensively in large programming projects and Java does a nice job of integrating them in a modular way.
The 'finally' clause is run after every exception 'catch' clause has completed. In the code set up in APSubset the try catch block is run in a loop. The loop is set up to simulate a different error during each iteration. In a more robust program the try-catch block may be around your entire code. Remember most of the time Exceptions are things to avoid in the design of your code. You put them in because you may not be aware of every error scenario that could occur. In a small example such as this I had to force things to happen in a very small space. The idea behind 'finally' is that it gives you a guarenteed exit point from any Exception. A place where you can reset things or decide that the error involved was too grave and you must terminate the program. It's a place where common error handling or recovery can be done.
In the exception code there is a line commented out. It does a divide by zero but it is outside the try-catch block. Uncomment it in the IDE and run the code. You should find that the program will terminate there and no other statements past that point will be executed. Now compare that with the way the zero divide works inside the try-catch block. That should give you an idea of the utility of the Exception handling facilities in Java.
APSubset.java
The code is 600+ lines of code so rather than place it in the text of the article
I have made it into a downloadable file. Go ahead and download it and run it
yourself in your own Java IDE.
APSubset.java
APSubset.java
Output (Run from Netbeans IDE)
run: APSubset: BASIC TYPES APSubset: int a = 0 APSubset: double b = 0.0 APSubset: boolean c = false APSubset: APSubset: BASIC OPERATORS APSubset: int a = 1 + 2 = 3 APSubset: double b = 2.0 + 3.5 = 5.5 APSubset: int a = 2 - 3 = -1 APSubset: double b = 2.0 - 3.5 = -1.5 APSubset: int a = 2 * 3 = 6 APSubset: double b = 2.0 * 3.5 = 7.0 APSubset: int a = 3 / 2 = 1 APSubset: double b = 3.5 / 2.0 = 1.75 APSubset: int a = 17 % 5 = 2 APSubset: double b = 17.0 % 6.0 = 5.0 APSubset: APSubset: Pre/Post-increment and Pre/Post-decrement APSubset: int a = 2 APSubset: a++ = 2 APSubset: int a = 3 APSubset: ++a = 4 APSubset: int a = 4 APSubset: a-- = 4 APSubset: int a = 3 APSubset: --a = 2 APSubset: int a = 2 APSubset: double b = 5.0 APSubset: b++ = 5.0 APSubset: double b = 6.0 APSubset: ++b = 7.0 APSubset: double b = 7.0 APSubset: b-- = 7.0 APSubset: double b = 6.0 APSubset: --b = 5.0 APSubset: double b = 5.0 APSubset: APSubset: Assignment operators +=, -=, *=, /=, %= APSubset: int a = 13 APSubset: double b = 13.0 APSubset: int a += 7 = 20 APSubset: double b += 7.5 = 20.5 APSubset: int a -= 3 = 17 APSubset: double b -= 3.5 = 17.0 APSubset: int a *= 2 = 34 APSubset: double b *= 2.0 = 34.0 APSubset: int a /= 2 = 17 APSubset: double b /= 2.0 = 17.0 APSubset: int a %= 5 = 2 APSubset: double b %= 6.0 = 5.0 APSubset: boolean c = 1 == 1 = true APSubset: boolean c = 2 != 3 = true APSubset: boolean c = 2 < 1 = false APSubset: boolean c = 2 < 3 = true APSubset: boolean c = 2 <= 4 = true APSubset: boolean c = 2 <= 2 = true APSubset: boolean c = 4 > 1 = true APSubset: boolean c = 4 >= 5 = false APSubset: boolean c = 5 >= 5 = true APSubset: APSubset: Logical Operators ||, &&, ! APSubset: boolean c = true || true = true APSubset: boolean c = true || false = true APSubset: boolean c = false || false = false APSubset: boolean c = true && true = true APSubset: boolean c = true && false = false APSubset: boolean c = false && false = false APSubset: int[] d = null: in else clause due to short circuit APSubset: int[] d = 1 2 3 4 5 APSubset: double b = 5.0/2 = 2.5 APSubset: double b = (int) b = 2.0 APSubset: int a = (int) 5.0/2 = 2 APSubset: double b = 5.0/(int) 2 = 2.5 APSubset: double b = (int) 5.0/(int) 2 = 2.0 APSubset: double b = ((int) 5.0)/(double) 2 = 2.5 APSubset: double b = ((int) 5.0)/ 2 = 2.0 APSubset: Take a string, concat a number13and another string APSubset: to see a back slash must escape it with a backslash \\ = \ APSubset: to print a double quote use backslash double quote \" = " APSubset: to get extra lines use backslash-n (\n) APSubset: Look at the code for this because there are some interesting \ (backslash) uses APSubset: One Dimension Array: Size = 5 array = 0 1 2 3 4 APSubset: Notice in the code that the size is 5 and the indexes range from 0 - 4 APSubset: Output 2 x 2 array in matrix form 0 1 1 0 APSubset: twod[0].size = 2 APSubset: twod[1].size = 2 APSubset: twod.size = 2 APSubset: if statements: single statement follows APSubset: stmt1: APSubset: single statement if: 1 < 2 APSubset: stmt2: APSubset: if statements: compound statements APSubset: stmt1: APSubset: compound if: APSubset: 1 < 2 is true APSubset: all statements are executed within braces APSubset: stmt2: APSubset: only 1 of the if statement is executed in the compound ifs APSubset: then clause: num + 5 < 20 is true APSubset: else clause: num + 15 < 20 is false APSubset: while loop: count numbers from 0 to 9 0 1 2 3 4 5 6 7 8 9 APSubset: end while loop APSubset: for loop: version of above while loop APSubset: for(i = 0; i < 10; i++): all loop controls are in first line (you don't have to search through the loop) 0 1 2 3 4 5 6 7 8 9 APSubset: end for loop APSubset: for each: use on intarr defined above, check the difference APSubset: this version uses the fact that if you are going to visit each element APSubset: the computer knows the size and can automatically code the loop parameters 0 1 2 3 4 APSubset: end for each style loop APSubset: new operator: using new to create an object instance of APSubset APSubset: this will give us an object to use the 'reverse' methods defined above APSubset: apsubset instantiated to an APSubset object APSubset: method over loading: 2 reverses have been defined: APSubset: one to reverse strings and one to give a string of reversed 'int' APSubset: look at their definitions they have different call parameters APSubset: the compiler figures out which one to use based on parameter type APSubset: apsubset.reverse("abcdefg") = gfedcba APSubset: apsubset.reverse(12345) = 54321 APSubset: The methods do similar things but to different types of parameters. APSubset: Run your IDE JavaDoc on this class and you will see what different APSubset: comment styles do for the JavaDoc Documentation. APSubset: Single line comments starting with // are used copiously in this class APSubset: these 'log' statements are defined as static they can be used anywhere APSubset: inside this class definition. If one were to use it in another class APSubset: would have to use: APSubset.log(); APSubset: APSubset.log() call check the code APSubset: static field: APSubset.ANSWER = 42 APSubset: String nullstr = null; can't print this because it's null it has no String value yet APSubset: We can test if the variable is a null variable APSubset: null: reserved variable name value is null APSubset: if (e = true): Forgot the extra equal sign setting e to true rather than testing with == APSubset: e = true; f = false APSubset: if (e = f): in else clause because of '=' assignment now e is false APSubset: e = true; f = true APSubset: if (e == f): Now this works as expected. APSubset: Be careful with your use of = and == especially with booleans APSubset: exceptionGeneratorMethod executed no exception thrown ctr = 0 APSubset: try-catch ArithmeticException: / by zero APSubset: finally clause executed: j = 0 APSubset: exceptionGeneratorMethod executed no exception thrown ctr = 1 APSubset: try-catch NullPointerException: null APSubset: finally clause executed: j = 1 APSubset: exceptionGeneratorMethod executed no exception thrown ctr = 2 APSubset: loop past intarr boundry 0 1 2 3 4 APSubset: try-catch IndexOutOfBoundsException: 5 APSubset: finally clause executed: j = 2 APSubset: try-catch Exception: exeptionGenerator Method: ctr = 3 APSubset: finally clause executed: j = 3 APSubset: java.lang.Object APSubset: APSubset: java.lang.Integer APSubset: Integer myint = new Integer(55) therefore myint.intValue() = 55 APSubset: Integer.MIN_VALUE = -2147483648 APSubset: Integer.MAX_VALUE - 2147483647 APSubset: APSubset: java.lang.Double APSubset: Double mydub = new Double(35.75) therefore mydub.doubleValue() = 35.75 APSubset: APSubset: java.lang.String APSubset: String test_str = "this has 22 characters" APSubset: test_str.length = 22 APSubset: test_str.substring(4,10) = has 2 APSubset: test_str.substring(4) - has 22 characters APSubset: test_str.indexOf("has") = 5 APSubset: test_str.compareTo("this has 22 characters") = 0 APSubset: test_str.compareTo("this has 22 characters") = 19 APSubset: test_str.compareTo("this has 22 characters") = -6 APSubset: APSubset: java.lang.Math APSubset: Math.abs(-3) = 3 APSubset: Math.abs(-3.56) = 3.56 APSubset: Math.pow(2.0,5.0) = 32.0 APSubset: Math.sqrt(2.0) = 1.4142135623730951 APSubset: Math.random() = 0.025126650838768416 APSubset: Math.random() = 0.8468764801673784 APSubset: Math.random() = 0.5932715934489992 APSubset: APSubset: Added values to end of list in succession: 1 2 3 APSubset: intList.size() = 3 APSubset: Added 4 to end of list: inList.size() = 4 APSubset: intList.get(2) = 3 APSubset: intList.add(2,5): 1 2 5 3 4 APSubset: Added 5 to each value using intList set: 6 7 10 8 9 APSubset: intList.remove(1): 6 10 8 9 APSubset: intList.remove(2): 6 10 9 APSubset: Because remove shifts the elements the 2 method calls end up removing APSubset: elements at index 1 and 3 of the original array BUILD SUCCESSFUL (total time: 1 second)
APSubset JavaDoc
A pdf of the JavaDoc Generated from netbeans can be downloaded from:
APSubset JavaDoc
APSubset JavaDoc
Conclusion
There are still some things from the AP document not coded. Some of them were
outside the scope of this type of study guide. Some I couldn't think of a
simple yet elegant example. I've marked those in the comments of the APSubset
code. Most of them you should have been exposed to in projects you've done in
your class.
This study guide is meant to remind you of the functional elements of Java available to you during your AP Test. I think it is more of a guide for the free response problems. Helping you to cast your answers in the subset so that you don't over think the problem or over utilize functionality available in advanced libraries.
I did not cover the Grid World case study in this and certainly that is something you need to look over and understand. The idea here was to cover the foundational elements of Java by way of a program so you may remember them if you need them.
This study guide is meant to remind you of the functional elements of Java available to you during your AP Test. I think it is more of a guide for the free response problems. Helping you to cast your answers in the subset so that you don't over think the problem or over utilize functionality available in advanced libraries.
I did not cover the Grid World case study in this and certainly that is something you need to look over and understand. The idea here was to cover the foundational elements of Java by way of a program so you may remember them if you need them.
References
- https://apstudent.collegeboard.org/apcourse/ap-computer-science-a/course-details "Course Details." AP Computer Science A. N.p., n.d. Web. 29 Mar. 2013.
THANK YOU. This is immensely beneficial.
ReplyDeletethank you
ReplyDeletethis was very helpful
ReplyDelete