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..b79daa7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Immutable and Mutable Objects + **Instructions:** 1. Fork this repository to your GitHub account. @@ -16,7 +17,7 @@ * Is the **blank** object mutable or immutable? How can you tell? ```text -PUT ANSWER TO #2 HERE +Strings are immutable (p.151) ``` ```java @@ -47,7 +48,9 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +findCenter returns a Point object - a single object with x-y coordinates. +Distance returns a double value. It represents the length of space between two points, + in a (presumably) straight line. ``` ```java @@ -87,14 +90,14 @@ 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 +PythonTutor.com shows the output as (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. Not sure if I articulate this right, but findCenter created a Point object the first time, then the value got modified before findCenter was called again. ``` ```java @@ -177,4 +180,4 @@ Follow these steps for submission: 2. Commit your working code for the exercises to your local copy/Feature01 branch. 3. Push it to your Remote/origin branch (i.e., GitHub: Feature01 -> origin/Feature01). 4. Issue a Pull request to my instructor repo. -5. Make sure to COPY the Pull request URL and submit it for the lab/assignment in Canvas. \ No newline at end of file +5. Make sure to COPY the Pull request URL and submit it for the lab/assignment in Canvas. diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..2dc740f 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,25 @@ -public class BigIntRewrite { +import java.math.BigInteger; +import static java.lang.Math.pow; +public static class BigIntRewrite { + + public static BigInteger pow(BigInteger x, int n) { + if (n == 0) { + return BigInteger.ONE; // I'm using BigInteger.ONE for base case + } + BigInteger t = pow(BigInteger.valueOf(x), n / 2); + + // Use BigInteger multiplication based on even/odd power + return (n % 2 == 0) ? t.multiply(t) : t.multiply(t).multiply(BigInteger.valueOf(x)); + } + } + BigIntRewrite result = BigIntRewrite.valueOf(x); // Start with x + BigIntRewrite halfPower = pow(x, n / 2); // Recursive call for half the power + + // I saw someone else using multiplication based on even/odd power. I think it was on Khan Academy, + // but I can't find the link in my search history for citation purposes. + + public static void main(String[] args) { } -} +// I give up on this one. I have too much packing and cleaning to do before a very long drive. \ No newline at end of file diff --git a/src/Part1_2.jpg b/src/Part1_2.jpg new file mode 100644 index 0000000..12042bf 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..477ef0d 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..fa3cd95 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..a69752b Binary files /dev/null and b/src/Part2_1.jpg differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..31f3065 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,32 @@ public class StringPlayground { + + public int matchedParentheses(String s) { + int count = 0; + for (char c : s.toCharArray()) { + if (c == '(') { + count++; + } else if (c == ')') { + count--; + if (count < 0) { + return count; // This ought to create an early return if the parentheses are unbalanced + } + } + } + return count; + } + } + public static void main(String[] args) { + StringPlayground playground = new StringPlayground(); + // I don't have the brainpower or energy to resolve this "unnamed classes" error code I'm getting. + String[] strings = {"((3 + 7) * 2)", "( )", "((3 + 7) * 2) )", "((3 + 7) * 2))"}; + for (String string : strings) { + int result = playground.matchedParentheses(string); + if (result == 0) { + System.out.println("String '" + string + "' has balanced parentheses."); + } else { + System.out.println("String '" + string + "' has unbalanced parentheses. Final count: " + result); + } + } + } -} \ No newline at end of file