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/Part1_2.jpg b/Part1_2.jpg new file mode 100644 index 0000000..4feae01 Binary files /dev/null and b/Part1_2.jpg differ diff --git a/Part1_3.jpg b/Part1_3.jpg new file mode 100644 index 0000000..c4b8d1e Binary files /dev/null and b/Part1_3.jpg differ diff --git a/Part1_4.jpg b/Part1_4.jpg new file mode 100644 index 0000000..54f8f01 Binary files /dev/null and b/Part1_4.jpg differ diff --git a/Part2_1.jpg b/Part2_1.jpg new file mode 100644 index 0000000..191ab74 Binary files /dev/null and b/Part2_1.jpg differ diff --git a/Part3_2.jpg b/Part3_2.jpg new file mode 100644 index 0000000..9b5f468 Binary files /dev/null and b/Part3_2.jpg differ diff --git a/README.md b/README.md index e759bf3..0ae2788 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ ```text PUT ANSWER TO #2 HERE +The blank object is mutable, because an immutable object is an object that once created, cannot be modified, but the point class's x and y points can be modified at any point. ``` ```java @@ -48,6 +49,7 @@ public class Puzzler { ```text PUT ANSWER TO #5 HERE +findCenter returns a Point class, which provides 2 separate values and in this case, the method only takes one value to produce them. distance returns a double value by doing a simple math equation when provided with two values to produce it. ``` ```java @@ -88,6 +90,8 @@ Recall that aliases are two variables that refer to the same object. ```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? @@ -95,6 +99,7 @@ PUT ANSWER TO #2 HERE ```text PUT ANSWER TO #3 HERE +p1 and p2 are not aliased, even though they are both set to the value of findCenter(box1), box1 is grown before p2 is set, thus the value of their centers are different. ``` ```java diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..eb5ba56 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) { + System.out.println(pow(1000,40)); + } + public static BigInteger pow(int x, int n) { + if (n == 0) return BigInteger.valueOf(1); + // find x to the n/2 recursively + BigInteger t = pow(x, n / 2); + BigInteger xBig = BigInteger.valueOf(x); + // 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(xBig)); + } } } diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..685f92a 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,14 @@ public class StringPlayground { public static void main(String[] args) { + System.out.println(parenthesisBalance("({3 * 7] * [2 / 2)", 0)); + } + + public static String parenthesisBalance(String s, int count) { + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(' || c == '[' || c == '{') { count++; } + else if (c == ')' || c == ']' || c == '}') { count--; } + } + return Integer.toString(count); } } \ No newline at end of file