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.

13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Immutable and Mutable Objects


**Instructions:**

1. Fork this repository to your GitHub account.
Expand All @@ -16,7 +17,7 @@
* Is the **blank** object mutable or immutable? How can you tell?

```text
PUT ANSWER TO #2 HERE
Strings are immutable (p.151)
```

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

```text
PUT ANSWER TO #5 HERE
findCenter returns a Point object - a single object with x-y coordinates.
Distance returns a double value. It represents the length of space between two points,
in a (presumably) straight line.
```

```java
Expand Down Expand Up @@ -87,14 +90,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
PythonTutor.com shows the output as (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. Not sure if I articulate this right, but findCenter created a Point object the first time, then the value got modified before findCenter was called again.
```

```java
Expand Down Expand Up @@ -177,4 +180,4 @@ Follow these steps for submission:
2. Commit your working code for the exercises to your local copy/Feature01 branch.
3. Push it to your Remote/origin branch (i.e., GitHub: Feature01 -> origin/Feature01).
4. Issue a Pull request to my instructor repo.
5. Make sure to COPY the Pull request URL and submit it for the lab/assignment in Canvas.
5. Make sure to COPY the Pull request URL and submit it for the lab/assignment in Canvas.
24 changes: 22 additions & 2 deletions src/BigIntRewrite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
public class BigIntRewrite {
import java.math.BigInteger;
import static java.lang.Math.pow;
public static class BigIntRewrite {

public static BigInteger pow(BigInteger x, int n) {
if (n == 0) {
return BigInteger.ONE; // I'm using BigInteger.ONE for base case
}
BigInteger t = pow(BigInteger.valueOf(x), n / 2);

// Use BigInteger multiplication based on even/odd power
return (n % 2 == 0) ? t.multiply(t) : t.multiply(t).multiply(BigInteger.valueOf(x));
}
}
BigIntRewrite result = BigIntRewrite.valueOf(x); // Start with x
BigIntRewrite halfPower = pow(x, n / 2); // Recursive call for half the power

// I saw someone else using multiplication based on even/odd power. I think it was on Khan Academy,
// but I can't find the link in my search history for citation purposes.


public static void main(String[] args) {

}
}
// I give up on this one. I have too much packing and cleaning to do before a very long drive.
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.
30 changes: 29 additions & 1 deletion src/StringPlayground.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
public class StringPlayground {

public int matchedParentheses(String s) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == '(') {
count++;
} else if (c == ')') {
count--;
if (count < 0) {
return count; // This ought to create an early return if the parentheses are unbalanced
}
}
}
return count;
}
}

public static void main(String[] args) {
StringPlayground playground = new StringPlayground();
// I don't have the brainpower or energy to resolve this "unnamed classes" error code I'm getting.
String[] strings = {"((3 + 7) * 2)", "( )", "((3 + 7) * 2) )", "((3 + 7) * 2))"};
for (String string : strings) {
int result = playground.matchedParentheses(string);
if (result == 0) {
System.out.println("String '" + string + "' has balanced parentheses.");
} else {
System.out.println("String '" + string + "' has unbalanced parentheses. Final count: " + result);
}
}

}
}