Uploaded by beatsbyHIGHTHUMB

Study Guide for Java 2

advertisement
Review for Exam #2
1. ) Watch https://www.youtube.com/watch?v=Tcz3VjBVaCI
2. ) Describe what you saw
3. ) Write algorithm for selection, insertion, bubble
4. 4. ) What are the differences? Selection checks the rest of the
array for the smallest number. Bubble will always check the numbers
next to each other and swaps adjacently.
5. 5. )
What are the similarities? Use loops that are nested.
Both use nested loops.
6. ) Write a recursive method that gets the product of all numbers
from 1 to n. The method will be called product,
and the parameter will be called n.
Public static int recursProd {
If ( n==1)
{
Return 1;
}
else
{
Return n; * recursProd(n-1);
}
recursProd(4) = 4 * 3 * 2 * 1;
}
7. ) Write a recursive method that gets the sum of the squares of all
numbers from 1 to n. The method will be called sumSquares and the
parameter will be called n.
Public static int sumSquares {
If ( n==0)
{
Return 1;
}
else
{
Return n * n + sumSquares(n -1);
}
sumSquares(3) = 14
= 3 * 3 + sumSquares(2)
=
+ 2 * 2 + sumSquares(1)
=
+ 2 * 2 + 1
8. ) Create an array of 10 weights. Write a loop that will input the
weight after being prompted. Validate that the weight is
numeric by using a try-catch block and allowing user to enter
another weight.
PracticeExam2 in IntelliJea
9. ) Create an array of 5 gpas. Write a loop that will input the gpa
after being prompted. Validate that the gpa is
numeric by using a try-catch block and allowing user to enter
another gpa.
PracticeExam2 in IntelliJea
How does the following try-catch block work?
try
{
if (…..) {
throw new Exception(“…..”);
}
}
catch (Exception e)
{
…..
}
How does the following try-catch block work?
public static int methodX(…) throws Exception {
if (….) {
throw new Exception(….);
}
return …;
Then, in main:
try
{
methodX(…);
methodY(…);
}
catch(…..)
{
…..
}
catch(….)
{
…
}
The above is a situation where main calls a method, and the method has an error but no try-catch block.
So, when the error occurs, control goes back to main, and is caught by main’s try-catch block.
Sometimes you want to close a file as part of the try-catch-finally block. But, you must first check if the
file was ever opened. Otherwise, you will get an error when trying to close a file that was never opened:
try
{
…..
}
catch (….)
{
….
} finally
{
if (inFile != null)
{
inFIle.close();
}
}
Recursion – Can a recursive method have more than 1 base case? Yes
How does a recursive binary search work? (Where does it start it’s search?)
8 = searchVal
{1, 2, 3, 4, 7, 10, 25}
<----- Starts in the middle
{1, 2}
<----- Checks if its =, <, >
{2}
What if you had to write a recursive factorial method to account for a negative exponent? For
Example:
2 ^-2 = ½ * ½
2^2=2*2
public int pow2(int exp)
{
if (exp ==0)
{
return 1;
}
else if (exp < 0)
{
return ½ * pow2(exp + 1);
}
else
{
return 2 * pow2(exp – 1);
}
}
2 ^2 = 4
= 2 * 2^1
= 2 * 2 * 2^0
= 2 * 2 * 1
= 4
}
Factorial Example
public int recursFact(int n)
{
If (n==1)
{
return 1
}
else
{
return n * recursFact(n-1);
}
// 3! = 3 x 3 x 1
// 3! = 3 x 2!
//
= 3 x 2 x 1!
//
= 3 x 2 x 1 x 1
}
Stack overflow is when a recursion never ends (no base case).
Binary Search –
If you had an array with { 5, 6, 10, 11, 12, 13, 15} how many elements would be checked before finding
11? How about 12? For 11, it checked it once. For 12, it checked it three times.
Selection Sort –
If you had an array with {8, 5, 3, 1, 7} what will be the 0th element after the first pass over the outer loop?
What would be the 1st element after the second pass over the outer loop?
0th element would be 1.
1st element would be 3.
{8, 5, 3, 1, 7} à {1, 5, 3, 8, 7} à {1, 3, 5, 8, 7}
What is the difference between a pivot point used in a Quick sort vs a midpoint used in the Merge sort?
Ex.
{ 9, 4, 8, 3, 7, 1, 6, 5 }
{9, 4, 8, 3} ßà {7, 1, 6, 5}
{9, 4} {8, 3} ----- {7, 1} {6, 5}
9 4 8 3
7 1 6 5
4, 9
1, 7
3, 8
3, 4, 8, 9
Splits it into halves
5, 6
1, 5, 6, 7
compares left and right array in order ex. 3 and 1 are compared
1, 3 ,4, 5, 6, 7, 8, 9
{9, 4, 8, 3, 7, 1, 6, 5}
{4, 3, 1, 5, 6, 9, 8, 7}
Mid Point in the middle more or less.
Pivot Point is number is in the middle by numerical value, NOT location in array.
Download