Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
* Is the **blank** object mutable or immutable? How can you tell?

```text
PUT ANSWER TO #2 HERE
Blank object is mutable because it's original value
can be changed by
a method
```

```java
Expand Down Expand Up @@ -47,7 +49,8 @@ public class Puzzler {
* Explain how the return values from #3 and #4 differ.

```text
PUT ANSWER TO #5 HERE
The return of 3 is double/variable
while the return type of 4 is an object
```

```java
Expand Down Expand Up @@ -87,14 +90,17 @@ 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
No, at the end of main p1 and p2
they both have the same value
they refer to different instances
```

```java
Expand Down Expand Up @@ -142,7 +148,10 @@ 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?

```
you could split up the for loop into a
seperate method to generalize it more.
```
## 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:
Expand Down
18 changes: 17 additions & 1 deletion src/BigIntRewrite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import java.math.BigInteger;
public class BigIntRewrite {
public static void main(String[] args) {

int x = 2;
int n = 10;
BigInteger result = bigPow(x,n);
System.out.println(x + "^" + n + "=" + result);
}
public static BigInteger bigPow(int x, int n) {
if (n == 0) return BigInteger.ONE;
// find x to the n/2 recursively
BigInteger t = bigPow(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));
}
}
}
Binary file added src/Part1_2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Part1_3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Part1_4.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Part2_1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Part3_2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/StringPlayground.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
public class StringPlayground {
public static void main(String[] args) {
System.out.println(cntParanthesis("(3+7))"));
System.out.println(cntParanthesis("(((((3+7))"));
System.out.println(cntParanthesis("((100+790909))"));
System.out.println(cntParanthesis("(3+7))))))"));

}

public static int cntParanthesis(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;
}
}