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..d0433bc 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's mutable because I can modify the x and y coordinates. ``` ```java @@ -87,14 +87,14 @@ 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) ``` 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 +No, they refer to different objects ``` ```java @@ -142,7 +142,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? - +Make it handle things like brackets and curly braces ## 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..e89a33e 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,19 @@ +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; + } + BigInteger result = BigInteger.valueOf(x); + BigInteger power = BigInteger.valueOf(n); + return result.pow(n); + } + public static void main(String[] args) { + int x = 44564; + int n = 123423; + BigInteger result = pow(x, n); + System.out.println(x + " raised to the power of " + n + " is: " + result); } } diff --git a/src/Part1_2.png b/src/Part1_2.png new file mode 100644 index 0000000..2fefa7b Binary files /dev/null and b/src/Part1_2.png differ diff --git a/src/Part1_3.png b/src/Part1_3.png new file mode 100644 index 0000000..54fb103 Binary files /dev/null and b/src/Part1_3.png differ diff --git a/src/Part1_4.png b/src/Part1_4.png new file mode 100644 index 0000000..8092481 Binary files /dev/null and b/src/Part1_4.png differ diff --git a/src/Part2_1.png b/src/Part2_1.png new file mode 100644 index 0000000..c62be0d Binary files /dev/null and b/src/Part2_1.png differ diff --git a/src/Part3_1.png b/src/Part3_1.png new file mode 100644 index 0000000..582175d Binary files /dev/null and b/src/Part3_1.png differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..3324fee 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,18 @@ public class StringPlayground { + public static int countParentheses(String s) { + int count = 0; + for (char c : s.toCharArray()) { + if (c == '(') { count++; } + else if (c == ')') { count--; } + } + return count; + } + public static void main(String[] args) { + String[] testStrings = {"((3 + 7) * 2)", "(()(()))", "((()))("}; + + for (String s : testStrings) { + System.out.println("Count for \"" + s + "\": " + countParentheses(s)); + } } -} \ No newline at end of file +}