diff --git a/README.md b/README.md index e759bf3..04889c0 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 "blank" object in this code is mutable. It has defined coordinates in main and then riddle uses point p to modify blank. ``` ```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 provides the center coordinates of a rectangle while distance calculates the distance between two points. ``` ```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 +At the end of main method, p1 and p2 are not pointing to the same object. They're both from box1 but if you change box1 it won't directly affect p1 or p2. ``` ```java @@ -142,6 +142,9 @@ 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? +```text +You can add more specific parameters +``` ## Part 4: Large Numbers diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..9a03207 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,25 @@ -public class BigIntRewrite { - public static void main(String[] args) { +/** + * @authot Trevor Hartman + * @author Alexei Iachkov + * @date 3-31-24 + * @version 1.0 + */ +import java.math.BigInteger; +public class BigIntRewrite { + public static BigInteger pow(int x, int n) { + if (n == 0) { + return BigInteger.valueOf(1); + } else if (n == 1) { + return BigInteger.valueOf(x); + } else { + BigInteger result = BigInteger.valueOf(x); + int count = 1; + while (count < n) { + result = result.multiply(BigInteger.valueOf(x)); + count++; + } + return result; + } } } diff --git a/src/Part1_2.png b/src/Part1_2.png new file mode 100644 index 0000000..8d65e94 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..fd2ce49 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..6761aa0 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..d9118d0 Binary files /dev/null and b/src/Part2_1.png differ diff --git a/src/Part3_2.png b/src/Part3_2.png new file mode 100644 index 0000000..5565c74 Binary files /dev/null and b/src/Part3_2.png differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..348f40d 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,36 @@ +/** + * @author Trevor Hartman + * @author Alexei Iachkov + * @date 3-31-24 + * @version 1.0 + */ public class StringPlayground { + + public static int countParentheses(String input) { + int count = 0; + + for (int i = 0; i < input.length(); i++) { + char c = input.charAt(i); + if (c == '(') { + count++; + } else if (c == ')') { + count--; + } + } + return count; + } + public static void main(String[] args) { + String testyString1 = "((3 + 7) * 2)"; + String testyString2 = "()())))"; + String testyString3 = "(()(()()(())))"; + String testyString4 = "((()))))"; + + System.out.println("Parentheses count for String1: " + countParentheses(testyString1)); + System.out.println("Parentheses count for String2: " + countParentheses(testyString2)); + System.out.println("Parentheses count for String3: " + countParentheses(testyString3)); + System.out.println("Parentheses count for String4: " + countParentheses(testyString4)); } -} \ No newline at end of file +} + +//this code can be generalized more by adding more specific parameters such as other brackets and using true/false instead of integers \ No newline at end of file