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..bcc617c 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 +blank is mutable ``` ```java @@ -47,7 +47,8 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +findCenter returns a 'Point' object that represents the center of the provided 'Rectangle' object. It also calcualtes the center coordinates and returns a new 'Point' object. +distance method returns a 'double' that represents the distance between two 'Point' objects. Calculates distance and returns distance as a 'double' value. ``` ```java @@ -87,14 +88,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) +(6, 9) ``` 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 +even though p1 and p2 both represent points at the center of rectangles, they refer to different Point objects in memory so there is no aliasing between p1 and p2. ``` ```java @@ -142,6 +144,7 @@ for (int i = 0; i < s.length(); i++) { * Screenshot your output and add it to your project as Part3_2.jpg 3. Generalize the code so that it works on any string. * What could you do to generalize it more? + * I could incorporate other types of brackets or delimiters besides parentheses ## Part 4: Large Numbers diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..8ea2496 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,26 @@ +import java.math.BigInteger; + public class BigIntRewrite { + public static BigInteger pow(int x, int n) { + if (n == 0) return BigInteger.ONE; + + BigInteger bigX = BigInteger.valueOf(x); + BigInteger t = pow(x, n / 2); + + if (n % 2 == 0) { + return t.multiply(t); + } else { + return t.multiply(t).multiply(bigX); + } + + } + public static void main(String[] args) { + int base = 2; + int exponent = 10; + BigInteger result = pow(base, exponent); + System.out.println(base + " raised to the power of " + exponent + " is: " + result); + } } diff --git a/src/Part1_2.jpg b/src/Part1_2.jpg new file mode 100644 index 0000000..4c46606 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..ce97fc7 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..832765b 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..1e268f2 Binary files /dev/null and b/src/Part2_1.jpg differ diff --git a/src/Part3_2.jpg b/src/Part3_2.jpg new file mode 100644 index 0000000..59f874a Binary files /dev/null and b/src/Part3_2.jpg differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..b91e735 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,28 @@ 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[] testStrings = { + "((3 + 7) * 2)", + "((3 + 7) * 2", + "()", + "(()))" + }; + + for (String testString : testStrings) { + int result = countParentheses(testString); + System.out.println("Count for \"" + testString + "\": " + result); + + } } } \ No newline at end of file