v.May.4 J v.May.3.2 Things learned from the Review Session: ● You can write on the AP Exam if() //one liner without the curly braces ● On one of the FRQs from 2021, I was confused because I thought you had to create the ArrayList, but apparently it was already done because it noted: //some methods, constructors, instance variables, etc. may have been ● A for each loop evaluates the ints, not the indices Questions: ● In a constructor are the methods static or not — they’re non-static just something like: public int/void method(int parameter){/*implementation not shown*/} v.May.3 Consider leaving a bit of space in between written code in case you need to add something! I just finished doing and reviewing the 2021 FRQ. v.May.2 can use charAt on AP Exam Review compareTo? ● check solutions for practice FRQ v.April.25.2 AP Exam Review Packet #4 Q22 ?? v.April.25 If it’s like: Superclass sus = new Subclass(), etc., then if you call sus.method(), which exists in both Subclass and Superclass, it will use the method in Subclass. If method doesn’t exist in the subclass, it will use the one from the Superclass. I will copy something worded really well which I think explains it perfectly: /*A Person reference type can only do methods that exist in its own class. If that method exists, it will look in the subclass for the same method. If the method has same name and parameters, etc., it will execute the method in the subclass. Java is not able to execute any method that does not exist in the superclass/reference type class. */ /* If it’s subclass <var>=new subclass(), then it can be able to execute methods in the superclass, but remember, it doesn't work the other way around! */ v.April.22 Selection sort — swapping Insertion sort — constant comparisons (yt video of ppl dancing) v.April.21 Length of an String: .length() Array: .length ArrayList: .size() How to spell length? Leng - th v.April.11.2 Printing with tabs: https://stackoverflow.com/questions/6000810/printing-with-t-tabs-does-not-result-in-aligne d-columns v.April.11 = v. == 1) use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java. 2) == return true if two references are of the same object. The result of the equals() method depends on overridden implementation. 3) For comparing String use equals() instead of == equality operator. AP Exam Review v.April.3.2 / v.April.6 Recursion — base case, and we built off that depending on previous found values/terms using a rule. We work from the base case up to finding more values/terms. SEARCHING ● Linear Search — traversing through the array one item at a time until the desired is found. It’s slow! ● Binary Search — we have a low, mid, and high. Depending on if the desired value is less than the mid or greater. We now only search in half of the array. At each iteration, we halve the amount of values we have to search for, until the desired value is found. Implementation: recursion or while loop. **Requires the array to be in sorted, i.e., increasing ≤ order, … which calls for the need of a way to sort the array. SORTING ● Selection Sort — we traverse through the list to find the smallest value which we put at the front. Actually, we swap the first number at index 0 with the smallest value. We continue to find the second smallest value, which we swap to with the number at index 1. We sort all the numbers this way, putting the smallest number at the front, then the second smallest, selecting the smallest Variations: sort max to back ● Insertion Sort — the first number is already sorted. Great. Now we look to the number to the right. We are now going to put the second term into the sorted numbers on the left. Great, now the first two terms are sorted. We insert the 3rd term, by continually comparing it to number by number to the left until its place is found. We sort all the numbers this way. Variations: sort max to back (the yt video of people folk dancing) ● Merge Sort — works by dividing the list into parts, then merging it back together in the correct order. See the tracing. ● Note: we are all sorting in ≤ order *There are more types of searching and sorting, but those presented here are the ones we learned in class. v.April.3 Remember that all method names have parentheses even if no parameters are being passed () v.April.1 Merge Sort Continually subdividing, then merging them together O(n log n) v.Mar.29 Insertion Sort Best case: O(n) Worst case: O(n^2) Avg case: v.Mar.25 Selection Sort: Best Case: O(n^2) Worst Case: O(n^2) Avg case: O(n^2) v.Mar.24 LINEAR SEARCH -------------------Best case: O (1) Worst case: O (n) Avg case: approx. O (n/2) big-O notation -- on the order of Linear search / sequential search */ /* BINARY SEARCH -----------------Best case: O(1) Worst case: O ceiling(log_2{n}) Avg case: O ceiling(log_2{n}) v.Mar.10 Recursion - a function definition that calls itself ex: a_n=a_n1+4 (a_0=3) a_n=a_n-1*4 (a_0=1) Unit 10: Recursion & Searching/Sorting Unit 9 Inheritance v.Mar.6.2 /*A Person reference type can only do methods that exist in its own class. If that method exists, it will look in the subclass for the same method. If the method has same name and parameters, etc., it will execute the method in the subclass. Java is not able to execute any method that does not exist in the superclass/reference type class. */ /* If its subclass <var>=new subclass(), then it can be able to execute methods in the superclass, but remember, it doesn't work the other way around! */ *v.Mar.6.1 **Reminder: int → double double --/--> int (an int can be passed as a double by adding .0, but a double cannot be passed as an int) v.Feb.27 Question: when do you have to use input.nextLine(); ? (CodeHS 9.5 Exercises) v.Feb.20 A method can be overridden and called at the same time (CodeHS 9.4.7) v.Feb.14 If not for this error, MyProgram.java:5: error: integer number too large: 3333333334 int n=Math.pow(3333333334,2); ^ 1 error #51 from the Pi Day Problems Week 3 could have been solved like this: int n=Math.pow(3333333334,2); int sum=0; while(n>0) { sum += (n%10); n/=10; } System.out.println(sum); Just traversing through the number and finding the sum of its digits. v.Feb.8.2 Remember to check for null! ● Make your variable names invoke some sort of meaning. A good example is nR and nL for neighbors on the right and left and n for the person in the middle. ● Don’t forget to return something at the end if it’s a non-void method! v.Feb.8 Rule: you can’t reassign a value with an array literal. something is a 2D Array something[1] = {3.1, 4.1, 5.9, 2.6, 8.4}; is thus Not Valid. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[1] = temp; is correct v.Feb.7 Unit 8 2D Arrays Test ● 8 MCQ ● 4 Methods ○ 2 involve 2D Arrays of primitive, Strings ○ 2 involve 2D Array of objects ■ check for null ○ Also should be able to traverse through a single row/column v.Feb.4 You can’t access a private instance variable in another class. You can in the same class, you would just do the object.instance variable, but in a different class, this doesn’t work. Here’s the error message that you would get: Theater.java:140: error: price has private access in Seat revenue+=seats[r][col].price; ^ 1 error On the other hand, you can call public methods from any class v.Jan.24 You can add objects into an ArrayList in different ways 1) Instrument i10=new Instrument("cello", 8, 7000); list.add(i10); 2) list.add(new Instrument("harp", 7, 15000)); Extra challenge in this project: write a method that determines the instrument name with the most vowels (“aeiou”) v.Jan.23 Remember if you remove something, you have to do i--; in a for loop going forwards Why is it static in 7.4.8? v.Jan.19 Is it possible to have null in an ArrayList? v.Jan.14 So if you created a new object, does this mean you can create an ArrayList of that object (or array I guess) or do you have to do something first? v.Jan.13 Go over Quiz Q#2? v.Jan.12 Remember, you need to import java.util.*; or import.java.util.Scanner; for ArrayLists to work (like the Scanner object!) v.Jan.11 Why does a for loop going backwards work? v.Dec.16 Review: ● #22: && || problem ● #28: what if something that causes an error is in the header (like division by 0) Remember: ● i is the special number (in for loops, etc.) ● “ “ go inside the parenthesis like ○ System.out.println(“Hi!”); ● Length of a String: ○ .length() ● Length of an array: ○ .length ● .equals(---) for Strings == for chars and others (comparing in header) Exam: ● Can bring scratch paper? ● Remember can use s.o.p(“ “) and s.o.pl(“ “) v.Dec.15 Questions: ● So basically static if methods are in the same class as main, and don’t use static if not? ● What’s a class? Remember: ● It’s .substring(inclusive, exclusive) Run-time errors v. Compile-time errors ● A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error. ○ ○ compile time -- syntax run time -- problem v.Dec.10 In an array, check for null Question: ● when to use .method() instead of method() ● when to use this/other Remember: ● always need () if no parameters, don’t forget!! v.Dec.6 Can you have 2 (or more) && or || in a header? v.Dec.2 Questions: ● CodeHS 6.3.9 ● different classes stuff/calling/when to use .method() ● CodeHS 6.4.8 had nice explanations (see deleted comments in history) v.Nov.16 while loops (3) ● game of chance ● sentinel / traversing through digits (or chars?) page of for loops (2) ● Strings Helpful tips: ASCII Art: ● create tables of the different characters ● use arithmetic sequences, to find the # of symbols on row i Nested For Loops: ● go from chronologically (top to bottom) ● out → in (do things, once done...) → out → in … v.Nov.12 Practice-it 2: Exercise #22 draft: public class DollarFigure { for(int i=1; i<=7; i++) { for(int a=1; a<=0+2*(i-1); a++) { System.out.print("*"); } for(int d=1; d<=7+(-1)*(i-1); d++) { System.out.print("$") } for(int a=1; a<=14+(-2)*(i-1); a++) } v.Nov.3 = v. ==? Ans: The main difference between == and equals in Java is that "==" is used to compare primitives while the equals() method is recommended to check the equality of objects. In headers, usually use ==. v.Oct.26 Research diff. between ifs, elseifs, elses, returning values vs. doing things. How do the blocks work? Do you just keep progressing through if blocks (and other blocks) or do you stop after just 1 block is true? v.Oct.25 Answers to MCQ Unit 3: 1. D 2. B 3. A 4. C 5. D 6. D 7. A 8. E 9. D 10. E 11. A 12. E 13. C 14. D 15. C 16. B 17. C 18. B 19. B 20. B 21. C TEST -10 Multiple Choice ?s -a Truth Table -DeMorgan’s Laws -3 or 4 methods -boolean expressions -if, else -if, else if, else Reminder: for else don’t need any (---) v.Oct.23 null = something w/o a value null then gets the value? 3.7.7 Ans: YES, null does not refer to anything yet -Remember to use == when comparing numeric values! v.Oct.19 Remember (int)(---)+1 or (int)(---) casts a double to an int. v.Oct.5 Math.random gives a random number from 0 inclusive to 1 exclusive v.Oct.3 ● int → double ○ but not ● double → int v.Sep.30 Study later: ● .intValue() ✔ v.Sep.28.2 Notes -- writing a method: 1. public 2. return type, void if doesn’t return anything 3. methodName 4. (parameter list, parameter listo) or ( ) if no parameters needed Ex: public double sphereVolume(double radius) {/* implementation not shown */} v.Sep.28 .equals vs == Explain the difference between .equals and == when comparing Strings in Java. .equals checks if the String objects point to the same reference in memory == checks if the String objects store the same value Question: is public double Calculator (int num) { … What it is returning, or what? Answer: yes v.Sep.27 .charAt -- returns a character -- ‘y’ .substring -- returns a String -- “yell” remember ( ); !!!!!!! v.Sep.22 remember input.nextLine(); not input.nextString(); v.16.9 ● get, set (accessor/mutator) Public chars -- ‘M’ void -- does something, doesn’t return anything non-void -- return something, must do something with it Example header of non-void method: public double calcArea(); On test -8->10 Multiple Choice questions ● attribute=instance variable null not on test given partial class definition Call methods Create objects Write full class header v.15.9 <<Make sure that you use proper names, don’t abbreviate like crazy!>> Notes v.14.9 ● final means a constant Question: 2.6.3--why no “int carArea=car.getArea();” ? Review - ✔️ ✔️ instance, instance variables Null X (not on test) Do voids take values num.add1(--)? Depends on creator class--what’s that called? public void vs. public (data type) ✔️ Notes v.9.9 1. Game.java 2. Line 1--no ; (either ; or { not both) 3. Line 3--private not public 4. Line 6--write “double t” 5. Line 8--instance variables on left 6. Line 9--needs ; 7. Line 17--swap Notes v.8.9 ● classes are public Notes v.2.9 ● Instance variables/bases of class template/attributes ● constructor -- creating objects ● Rectangle rect1=new Rectangle (3,5); 2.1 Checkin Objects Attributes True False - Only need say “int”, when declaring variable After that can change value as many times as want ● Know Casting https://docs.google.com/forms/d/e/1FAIpQLSeRo9IORACvkMBqRXpAKMDenMwM OMDIng7Vbi6becbtGhmzQA/viewform Review: ● Casting from double → int -----Anything in (---) does the operations there first. Say ➔ (int) (x/y), then it’s already an int ➔ (double) (x/y), also an int Question: 24 Given the following, what will be printed out? int a = 2; int b = 3; int c = 4; System.out.println(a * b + b / a + (a * c / 4.0) * c); 1. 2. 3. 4. 15.0 24 15 24.0 I put 15, but the correct answer was 15.0, here is my work. 2*3 + 3/2 + (2*4/4.0)*4 = 6 + 1 + (8/4.0)*4 = 7 + 2.0*4 = 7 + 8.0 =15.0 Nevermind, I got it! Some more notes: https://codehs.com/sandbox/id/u1-test-review-ZOFmyU NOTES: ★ int * double = double !!! ○ Multiplication is without problems, division, however, is tricky. ★ int + double = double !!! ○ I guess you could say the double is dominant. Multiplication, addition, and subtraction is easy. Division is tricky. ★ Variables are like substitutions. ★ Division with just 1 double is makes it a double [values] ★ If you have [variables] instead of values, put (double) in front of the division no (x/y), but if lots of math, and divides cleanly ★ How to Round: (int)(x+0.5) ★ Scanner object: ○ import java.util.Scanner; ○ Scanner input=new Scanner (System.in); ○ int num1=input.nextInt(); ● ● ● ● To make something a double, just do 2. or 2.0 If (---) is already correct then (double)(---) is correct too. Math part (---) with say 6+(double)3/2 + 1=6+1.5+1=8.5 4./2.=2.0 ok as well ✅ Scanner input=new Scanner (System.in); ✅