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
1 change: 0 additions & 1 deletion .idea/misc.xml

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

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.

Binary file added 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 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 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 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 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.
10 changes: 6 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 blank object is mutable because this object can be changed after it is created.
```

```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
For the findCenter method, a Point object is returned, and for distance a double value is returned. The Point object allows for coordinates to be given, and the double object utilizes a more calculated value with the formula given in the code.
```

```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)
(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
Yes, p1 and p2 are aliased. These two variables refer to the same object, and the Point objects are the same.
```

```java
Expand Down Expand Up @@ -142,6 +143,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?
In order to generalize it more, we could account for curly brackets or square brackets.

## Part 4: Large Numbers

Expand Down
23 changes: 23 additions & 0 deletions src/Aliasing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.awt.Point;
import java.awt.Rectangle;

public class Aliasing {
public static void printPoint(Point p) {
System.out.println("(" + p.x + ", " + p.y + ")");
}

public static Point findCenter(Rectangle box) {
int x = box.x + box.width / 2;
int y = box.y + box.height / 2;
return new Point(x, y);
}

public static void main(String[] args) {
Rectangle box1 = new Rectangle(2, 4, 7, 9);
Point p1 = findCenter(box1);
printPoint(p1);
box1.grow(1, 1);
Point p2 = findCenter(box1);
printPoint(p2);
}
}
15 changes: 15 additions & 0 deletions src/BigIntRewrite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import java.math.BigInteger;
public class BigIntRewrite {
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) {
System.out.println(pow(8,3));
System.out.println(pow(20,10));

}
}
21 changes: 21 additions & 0 deletions src/StringPlayground.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
public class StringPlayground {
public static int countParentheses(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;
}
public static void main(String[] args) {

String s = "((3 + 7) * 2)";
String s2 = "(())()(()";
String s3 = "()(()(";
String s4 = ")(()(()))";
String s5 = "))()()(()()()(()(((";

System.out.println("Amount of parentheses for s: " + countParentheses(s));
System.out.println("Amount of parentheses for s2: " + countParentheses(s2));
System.out.println("Amount of parentheses for s3: " + countParentheses(s3));
System.out.println("Amount of parentheses for s4: " + countParentheses(s4));
System.out.println("Amount of parentheses for s5: " + countParentheses(s5));
}
}