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

```text
PUT ANSWER TO #2 HERE
The point object is immutable. the p.x and p.y are accessed but not modified. the method will return a new value based on x, p.x, or p.y, it doesnt modify p by itself.
```

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

```text
PUT ANSWER TO #5 HERE
findCenter returns a point that represents a location, while distance returns a double that is a numerical value
```

```java
Expand Down Expand Up @@ -87,14 +87,15 @@ 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)
(6, 9)
```

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 arent aliased because aliasing refers to atleast 2 variables referring to the same object in memory. p1 and p2 refer to completely different point objects.
```

```java
Expand Down
20 changes: 19 additions & 1 deletion src/BigIntRewrite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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;
// 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 {
return t.multiply(t).multiply(BigInteger.valueOf(x));
}
}

public static void main(String[] args) {
int x = 2;
int n = 10;
BigInteger result = pow(x, n);
System.out.println(x + " raised to the power of " + n + " is " + result);
}
}
32 changes: 31 additions & 1 deletion src/StringPlayground.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
import java.util.Stack;

public class StringPlayground {

public static boolean checkBalancedBrackets(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else if (c == ')' || c == ']' || c == '}') {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
return false;
}
}
}
return stack.isEmpty();
}

public static void main(String[] args) {
String s1 = "{[()]}";
String s2 = "{[(])}";
String s3 = "([)]";
String s4 = "((()))";

System.out.println("Is s1 balanced? " + checkBalancedBrackets(s1));
System.out.println("Is s2 balanced? " + checkBalancedBrackets(s2));
System.out.println("Is s3 balanced? " + checkBalancedBrackets(s3));
System.out.println("Is s4 balanced? " + checkBalancedBrackets(s4));
}
}
}