diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..f45a85e --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,408 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file 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..36564b2 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,17 @@ 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_5.png](img_5.png) 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 +blank object in the provided Java code is mutable. + This can be inferred from the fact that its state changes + after being passed as a parameter to the riddle method. + In the riddle method, both the x parameter and the p + parameter's x and y values are used in calculations, +indicating that the Point object is mutable and its state can be modified. ``` ```java @@ -41,13 +47,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_1.png](img_1.png) 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_2.png](img_2.png) 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 +#3 (findCenter(Rectangle box)) returns a Point object representing a position, + while #4(distance(center, blank)) returns a double value representing a distance. ``` ```java @@ -83,18 +92,24 @@ 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_3.png](img_3.png) 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? * Put your answer in the text block below ```text -PUT ANSWER TO #3 HERE + +At the end of main, p1 and p2 are not aliased. Aliasing occurs +when two variables refer to the same object in memory. In this case, +p1 and p2 refer to different Point objects returned by the findCenter + method for different Rectangle objects (box1 before and after grow method call). + Therefore, p1 and p2 are not aliased; they reference distinct Point objects. ``` ```java @@ -140,6 +155,7 @@ 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_4.png](img_4.png) 3. Generalize the code so that it works on any string. * What could you do to generalize it more? diff --git a/img.png b/img.png new file mode 100644 index 0000000..f80ad51 Binary files /dev/null and b/img.png differ diff --git a/img_1.png b/img_1.png new file mode 100644 index 0000000..4fe9064 Binary files /dev/null and b/img_1.png differ diff --git a/img_2.png b/img_2.png new file mode 100644 index 0000000..c93fb25 Binary files /dev/null and b/img_2.png differ diff --git a/img_3.png b/img_3.png new file mode 100644 index 0000000..b1ec047 Binary files /dev/null and b/img_3.png differ diff --git a/img_4.png b/img_4.png new file mode 100644 index 0000000..eb5bfdc Binary files /dev/null and b/img_4.png differ diff --git a/img_5.png b/img_5.png new file mode 100644 index 0000000..d40aa3e Binary files /dev/null and b/img_5.png differ diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..c3c3eef 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,24 @@ +import java.math.BigInteger; + public class BigIntRewrite { - public static void main(String[] args) { + public static BigInteger pow(int x, int n) { + 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(BigInteger.valueOf(x)); + } + } + + public static void main(String[] args) { + int x = 2; + int n = 10; + BigInteger result = pow(x, n); + System.out.println(result); } } diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..36e326b 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,33 @@ 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 == ')') { + if (count <= 0) { + throw new IllegalArgumentException("Unbalanced parentheses in the string: " + s); + } + count--; + } + } + if (count != 0) { + throw new IllegalArgumentException("Unbalanced parentheses in the string: " + s); + } + return count; + } + public static void main(String[] args) { + String s1 = "((3 + 7) * 2)"; + String s2 = "((3 + 7) * 2))"; + String s3 = "(3 + 7) * 2)"; + try { + System.out.println("Count for s1: " + countParentheses(s1)); + System.out.println("Count for s2: " + countParentheses(s2)); + System.out.println("Count for s3: " + countParentheses(s3)); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } } -} \ No newline at end of file +}