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..ef03873 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ ```text PUT ANSWER TO #2 HERE +- The object is immutable +because it's a string. ``` ```java @@ -48,6 +50,12 @@ public class Puzzler { ```text PUT ANSWER TO #5 HERE +- The return value from +#3 was blank and returns +to the point instance, +but #4 returns 5.0 and +does not connect back +to anything ``` ```java @@ -88,6 +96,8 @@ Recall that aliases are two variables that refer to the same object. ```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? @@ -95,6 +105,9 @@ PUT ANSWER TO #2 HERE ```text PUT ANSWER TO #3 HERE +- No because all of the +frames are not overlapp +ing. ``` ```java @@ -142,7 +155,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? - +* Turn the s variable to a scanner input, so any string may be used in the program. ## Part 4: Large Numbers Many encryption algorithms depend on the ability to raise large integers to a power. Below is a method that implements an efficient algorithm for integer exponentiation: diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..178cc5c 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,4 +1,18 @@ +import java.math.BigInteger; public class BigIntRewrite { + + public static BigInteger pow(int x, int n) { + if (n==0) return BigInteger.valueOf(1); + + BigInteger a = pow(x, n/2); + + if (n%2 == 0) { + return a.multiply(a); + } + else { + return a.multiply(a).multiply(BigInteger.valueOf(x)); + } + } public static void main(String[] args) { } diff --git a/src/Part1_2.jpg b/src/Part1_2.jpg new file mode 100644 index 0000000..ebee52b 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..dac5ad0 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..8ddd1df 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..c0c12ab Binary files /dev/null and b/src/Part2_1.jpg differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..5fb31fe 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,13 @@ public class StringPlayground { + public static void main(String[] args) { + String s = "((3 + 7) * 2)"; + int count = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(') { count++; } + else if (c == ')') { count--; } + } + System.out.println(); } } \ No newline at end of file