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..3595ad8 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ * Is the **blank** object mutable or immutable? How can you tell? ```text -PUT ANSWER TO #2 HERE +The blank object is mutable because it is a Point object and it's instances can be modified +such as when it's x and y values were changed. ``` ```java @@ -47,7 +48,9 @@ public class Puzzler { * Explain how the return values from #3 and #4 differ. ```text -PUT ANSWER TO #5 HERE +The return values from #3 and #4 differ because Part #3 is creating a new point from the dimensions given + by Rectangle rect and the return object is a Point. Part #4 is trying to determine the distance between + two points, center and blank, and the return value is a double. ``` ```java @@ -87,14 +90,18 @@ 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 +The output is: + +(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 not aliased because they refer to two different objects. Even though box1 grew by one, p1 and p2 were +assigned different points. ``` ```java @@ -142,6 +149,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? + * I added an if statement that would break the loop and return the count as zero if the String was empty. ## Part 4: Large Numbers diff --git a/src/BigIntRewrite.java b/src/BigIntRewrite.java index 8f0dfce..1c81df7 100644 --- a/src/BigIntRewrite.java +++ b/src/BigIntRewrite.java @@ -1,5 +1,22 @@ +import java.math.BigInteger; public class BigIntRewrite { + + public static BigInteger pow(int x, int n) { + BigInteger newX= BigInteger.valueOf(x); + if (n == 0) return BigInteger.valueOf(1); + // 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 { + BigInteger theBigInteger=t.multiply(t); + return theBigInteger.multiply(newX); + } + } public static void main(String[] args) { + System.out.println(pow(20,20)); } } diff --git a/src/Part1_2.jpg b/src/Part1_2.jpg new file mode 100644 index 0000000..b6fe2c1 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..25b7a04 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..b01fe6e 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..8f7db2d Binary files /dev/null and b/src/Part2_1.jpg differ diff --git a/src/Part3_2.jpg b/src/Part3_2.jpg new file mode 100644 index 0000000..7e776b5 Binary files /dev/null and b/src/Part3_2.jpg differ diff --git a/src/StringPlayground.java b/src/StringPlayground.java index a930a71..1ff55ca 100644 --- a/src/StringPlayground.java +++ b/src/StringPlayground.java @@ -1,4 +1,22 @@ public class StringPlayground { - public static void main(String[] args) { + public static int parantheses(String s) { + + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (s.length()==0){break;} + char c = s.charAt(i); + if (c == '(') { + count++; + } else if (c == ')') { + count--; + } + } + return count; } -} \ No newline at end of file + public static void main(String[] args) { + String string = "(3^2)+(15+2)=3"; + String not ="(This sentence is not punctuated correctly.) )"; + System.out.println(parantheses(string)); + System.out.println(parantheses(not)); + } + } \ No newline at end of file