diff --git a/Part1_2.jpg b/Part1_2.jpg new file mode 100644 index 0000000..71e17b4 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..f94fb75 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..01338de 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..009940e 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..df9e306 Binary files /dev/null and b/Part3_2.jpg differ diff --git a/README.md b/README.md index e759bf3..4f1b798 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,13 @@ 1. Using the [Python Tutor code visualizer](https://pythontutor.com/java.html#mode=edit) for Java, capture an image of a diagram showing the local variables and parameters of main and riddle just before riddle returns for the code below. * Save the image taken above to your project as Part1_2.jpg. + * ![img.png](Part1_2.jpg) 2. Answer the following question in the **text** block below: * Is the **blank** object mutable or immutable? How can you tell? - ```text PUT ANSWER TO #2 HERE +Yes, the blank object is mutable. You can tell this because the object is adjusted inside the riddle method, which means +it can be changed. ``` ```java @@ -41,13 +43,16 @@ public class Puzzler { 3. Using the [Python Tutor code visualizer](https://pythontutor.com/java.html#mode=edit) for Java, capture a stack diagram showing the state of the below program just before **findCenter** returns. * Save the image taken above to your project as Part1_3.jpg. + * ![img.png](Part1_3.jpg) 4. Using the [Python Tutor code visualizer](https://pythontutor.com/java.html#mode=edit) for Java, capture a stack diagram showing the state of the program just before **distance** returns. * Save the image taken above to your project as Part1_4.jpg. + * ![img.png](Part1_4.jpg) 5. Answer the following question below in the **text** block below: * Explain how the return values from #3 and #4 differ. ```text PUT ANSWER TO #5 HERE +The return from #3 returns a value, while the return from #4 does not return any value ``` ```java @@ -83,11 +88,13 @@ Recall that aliases are two variables that refer to the same object. 1. Using the [Python Tutor code visualizer](https://pythontutor.com/java.html#mode=edit) for Java, capture a diagram that shows the state of the program just before the end of main. * Save the image taken above to your project as Part2_1.jpg. + * ![img.png](Part2_1.jpg) 2. What is the output of the program? * Put the output in the text block below ```text PUT ANSWER TO #2 HERE +(5, 8) ``` 3. At the end of main, are p1 and p2 aliased? Why or why not? @@ -95,6 +102,7 @@ PUT ANSWER TO #2 HERE ```text PUT ANSWER TO #3 HERE +No, they're not aliased. P1 and P2 both refer to different objects. ``` ```java @@ -140,8 +148,10 @@ for (int i = 0; i < s.length(); i++) { 1. Encapsulate the above fragment in a method in the [StringPlayground](src/StringPlayground.java) class that takes a string argument and returns the final value of count. 2. Test your method with multiple strings, including some that are balanced and some that are not. * Screenshot your output and add it to your project as Part3_2.jpg + * ![img.png](Part3_2.jpg) 3. Generalize the code so that it works on any string. * What could you do to generalize it more? + * You could add square brackets "[]" or curly brackets "{}" to generalize it more. ## Part 4: Large Numbers diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..25e7c38 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,18 @@ +import java.math.BigInteger; public class BigIntRewrite { + public static BigInteger pow(int x, int n) { + if (n == 0) return BigInteger.ONE; + BigInteger t = pow(x, n / 2); + if (n % 2 == 0) { + return t.multiply(t); + } else { + return t.multiply(t).multiply(BigInteger.valueOf(x)); + } + } public static void main(String[] args) { - + int x = 4; + int n = 10; + BigInteger result = pow(x, n); + System.out.println(x + " raised to the power of " + n + " is: " + result); } } diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..19a1d9d 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,24 @@ public class StringPlayground { + public static int countParentheses(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 main(String[] args) { + String s = "((3 + 7) * 2)"; + String s2 = "(3 + 7) * 2)"; + String s3 = "((3 + 7 * 2)"; + + System.out.println("Count of parentheses for s: " + countParentheses(s)); + System.out.println("Count of parentheses for s2: " + countParentheses(s2)); + System.out.println("Count of parentheses for s3: " + countParentheses(s3)); } } \ No newline at end of file