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..d7d1637 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 +It is mutable as it is a point, and points are mutable. ``` ```java @@ -47,7 +47,7 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +Number 3 returns a primitive, number 4 returns an object. ``` ```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) +(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 +p1 and p2 are aliased as they both refer to box1. ``` ```java diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..28c1b0a 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,22 @@ +import java.math.BigInteger; + + public class BigIntRewrite { public static void main(String[] args) { + BigInteger pow = pow(6, 4); + System.out.println(pow); } + public static BigInteger pow(int x, int n) { + if (n == 0) return BigInteger.ONE; // BigInteger constant for 1 (had to look this up) + // 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)); + } + } } diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..8f780e6 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,28 @@ +import java.util.Scanner; + public class StringPlayground { public static void main(String[] args) { + String s; + Scanner in = new Scanner(System.in); + System.out.println("Input what you wish to check."); + s = in.nextLine(); + int count = findChar(s); + System.out.println(count); } + + + + public static int findChar(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; + } + in.close(); } \ No newline at end of file