diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index e759bf3..a4b018b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,9 @@ * Is the **blank** object mutable or immutable? How can you tell? ```text -PUT ANSWER TO #2 HERE +Blank object is mutable because it's original value + can be changed by +a method ``` ```java @@ -47,7 +49,8 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +The return of 3 is double/variable +while the return type of 4 is an object ``` ```java @@ -87,14 +90,17 @@ Recall that aliases are two variables that refer to the same object. * Put the output in the text block below ```text -PUT ANSWER TO #2 HERE +(5, 8) +(5, 8) ``` 3. At the end of main, are p1 and p2 aliased? Why or why not? * Put your answer in the text block below ```text -PUT ANSWER TO #3 HERE +No, at the end of main p1 and p2 +they both have the same value +they refer to different instances ``` ```java @@ -142,7 +148,10 @@ for (int i = 0; i < s.length(); i++) { * Screenshot your output and add it to your project as Part3_2.jpg 3. Generalize the code so that it works on any string. * What could you do to generalize it more? - +``` + you could split up the for loop into a + seperate method to generalize it more. +``` ## Part 4: Large Numbers Many encryption algorithms depend on the ability to raise large integers to a power. Below is a method that implements an efficient algorithm for integer exponentiation: diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..00375c3 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,21 @@ +import java.math.BigInteger; public class BigIntRewrite { public static void main(String[] args) { - + int x = 2; + int n = 10; + BigInteger result = bigPow(x,n); + System.out.println(x + "^" + n + "=" + result); + } + public static BigInteger bigPow(int x, int n) { + if (n == 0) return BigInteger.ONE; + // find x to the n/2 recursively + BigInteger t = bigPow(x, n / 2); + // if n is even, the result is t squared + // if n is odd, the result is t squared times x + if (n % 2 == 0) { + return t.multiply(t); + } else { + return t.multiply(t).multiply(BigInteger.valueOf(x)); + } } } diff --git a/src/Part1_2.JPG b/src/Part1_2.JPG new file mode 100644 index 0000000..f0ed1be Binary files /dev/null and b/src/Part1_2.JPG differ diff --git a/src/Part1_3.JPG b/src/Part1_3.JPG new file mode 100644 index 0000000..217a71a Binary files /dev/null and b/src/Part1_3.JPG differ diff --git a/src/Part1_4.JPG b/src/Part1_4.JPG new file mode 100644 index 0000000..82595fc Binary files /dev/null and b/src/Part1_4.JPG differ diff --git a/src/Part2_1.JPG b/src/Part2_1.JPG new file mode 100644 index 0000000..cc4f09c Binary files /dev/null and b/src/Part2_1.JPG differ diff --git a/src/Part3_2.JPG b/src/Part3_2.JPG new file mode 100644 index 0000000..071b4ab Binary files /dev/null and b/src/Part3_2.JPG differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..93314a0 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,22 @@ public class StringPlayground { public static void main(String[] args) { + System.out.println(cntParanthesis("(3+7))")); + System.out.println(cntParanthesis("(((((3+7))")); + System.out.println(cntParanthesis("((100+790909))")); + System.out.println(cntParanthesis("(3+7))))))")); + + } + + public static int cntParanthesis(String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(') { + count++; + } else if (c == ')') { + count--; + } + } + return count; } } \ No newline at end of file