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..1d6ae07 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ * Is the **blank** object mutable or immutable? How can you tell? ```text -PUT ANSWER TO #2 HERE +The blank object is mutable because it is a Point object. Points are mutable x,y values, and we can see them being added to x in the code. ``` ```java @@ -47,7 +47,7 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +findCenter assigns two values to a new Point object and returns that object, distance returns a double. ``` ```java @@ -87,14 +87,15 @@ 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. If the two variables were aliased, p1 and p2 would be pointing to the same object. ``` ```java diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..c7517b8 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,30 @@ +import java.math.BigInteger; + public class BigIntRewrite { + public static void main(String[] args) { + int base = 6543; + int exponent = 21; + BigInteger result = pow(base, exponent); + System.out.println(base + " raised to the power of " + exponent + " is: " + result); + } + + public static BigInteger pow(int x, int n) { + // convert int to BigInt + BigInteger base = BigInteger.valueOf(x); + + // base case + if (n == 0) return BigInteger.ONE; + + // find x to the n/2 recursively + BigInteger t = pow(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(base); + } } } diff --git a/src/Part1_2.png b/src/Part1_2.png new file mode 100644 index 0000000..b9d8e1d Binary files /dev/null and b/src/Part1_2.png differ diff --git a/src/Part1_3.png b/src/Part1_3.png new file mode 100644 index 0000000..dbcf8ca Binary files /dev/null and b/src/Part1_3.png differ diff --git a/src/Part1_4.png b/src/Part1_4.png new file mode 100644 index 0000000..b20b356 Binary files /dev/null and b/src/Part1_4.png differ diff --git a/src/Part2_1.png b/src/Part2_1.png new file mode 100644 index 0000000..ff1989a Binary files /dev/null and b/src/Part2_1.png differ diff --git a/src/Part3_2.png b/src/Part3_2.png new file mode 100644 index 0000000..d614da0 Binary files /dev/null and b/src/Part3_2.png differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..92c254f 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,26 @@ public class StringPlayground { + public static void main(String[] args) { + printer("((3 + 7) * 2)"); + printer("(3 + 7) * 2)"); + printer("(((3 + 7) * 2)"); + printer(")(3 + 7) * 2("); + printer("(((((( )(())(( (((( [][][{}{}{}"); + printer("))))))))))) )))))))))) (#@($*^(@#*! )"); + printer("any string"); + } + + public static int parenthesesBalance(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; + } + + public static void printer(String s) { + System.out.println("Count for '" + s + "': " + parenthesesBalance(s)); } } \ No newline at end of file