diff --git a/README.md b/README.md index e759bf3..ff148ee 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 point object is immutable. the p.x and p.y are accessed but not modified. the method will return a new value based on x, p.x, or p.y, it doesnt modify p by itself. ``` ```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 returns a point that represents a location, while distance returns a double that is a numerical value ``` ```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) +(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 +p1 and p2 arent aliased because aliasing refers to atleast 2 variables referring to the same object in memory. p1 and p2 refer to completely different point objects. ``` ```java diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..c1166c4 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,23 @@ +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(x + " raised to the power of " + n + " is " + result); } } diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..4a067a9 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,34 @@ +import java.util.Stack; + public class StringPlayground { + + public static boolean checkBalancedBrackets(String s) { + Stack stack = new Stack<>(); + for (char c : s.toCharArray()) { + if (c == '(' || c == '[' || c == '{') { + stack.push(c); + } else if (c == ')' || c == ']' || c == '}') { + if (stack.isEmpty()) { + return false; + } + char top = stack.pop(); + if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) { + return false; + } + } + } + return stack.isEmpty(); + } + public static void main(String[] args) { + String s1 = "{[()]}"; + String s2 = "{[(])}"; + String s3 = "([)]"; + String s4 = "((()))"; + + System.out.println("Is s1 balanced? " + checkBalancedBrackets(s1)); + System.out.println("Is s2 balanced? " + checkBalancedBrackets(s2)); + System.out.println("Is s3 balanced? " + checkBalancedBrackets(s3)); + System.out.println("Is s4 balanced? " + checkBalancedBrackets(s4)); } -} \ No newline at end of file +}