Skip to content

Commit

Permalink
finish lab2
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-jiahao committed Dec 7, 2024
1 parent 758257f commit fac9c69
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lab2/Arithmetic/Arithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static int product(int a, int b) {
* @return Sum of a and b
* */
public static int sum(int a, int b) {
return a * b;
return a + b;
}
}
4 changes: 2 additions & 2 deletions lab2/DebugExercise/DebugExercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
public class DebugExercise1 {
public static int divideThenRound(int top, int bottom) {
int quotient = top / bottom;
int result = Math.round(quotient);
double quotient = (double) top / bottom;
int result = (int) Math.round(quotient);
return result;
}

Expand Down
39 changes: 22 additions & 17 deletions lab2/DebugExercise/DebugExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
* Code adapted from https://stackoverflow.com/questions/4895173/bitwise-multiply-and-add-in-java and https://stackoverflow.com/questions/1533131/what-useful-bitwise-operator-code-tricks-should-a-developer-know-about
*/
public class DebugExercise2 {
/** Returns the max of a and b. Do not step into this function. */
/**
* Returns the max of a and b. Do not step into this function.
*/
public static int max(int a, int b) {
int w = (b - a) >> 31;
/* If you're stepping into this function, click the
step out button because you're not going to learn anything. */
int z = ~(b - a) >> 31;

int max = b & w | a & z;
return max;
if (a >= b) {
return a;
}
return b;
}


/** Returns the sum of a and b. Do not step into this function. */
/**
* Returns the sum of a and b. Do not step into this function.
*/
public static int add(int a, int b) {
int x = a, y = b;
/* If you're stepping into this function, click the
Expand All @@ -35,10 +36,11 @@ public static int add(int a, int b) {
return xor;
}

/** Returns a new array where entry i is the max of
/**
* Returns a new array where entry i is the max of
* a[i] and b[i]. For example, if a = {1, -10, 3}
* and b = {0, 20, 5}, this function will return {1, 20, 5}.
* */
*/
public static int[] arrayMax(int[] a, int[] b) {
if (a.length != b.length) {
System.out.println("ERROR! Arrays don't match");
Expand All @@ -53,21 +55,24 @@ public static int[] arrayMax(int[] a, int[] b) {
return returnArray;
}

/** Returns the sum of all elements in x. */
/**
* Returns the sum of all elements in x.
*/
public static int arraySum(int[] x) {
int i = 0;
int sum = 0;
while (i < x.length) {
sum = sum + add(sum, x[i]);
sum = sum + x[i];
i = i + 1;
}
return sum;
}

/** Returns the sum of the element-wise max of a and b.
* For example if a = {2, 0, 10, 14} and b = {-5, 5, 20, 30},
* the result should be 57.
* */
/**
* Returns the sum of the element-wise max of a and b.
* For example if a = {2, 0, 10, 14} and b = {-5, 5, 20, 30},
* the result should be 57.
*/
public static int sumOfElementwiseMaxes(int[] a, int[] b) {
int[] maxes = arrayMax(a, b);
int sumofMaxes = arraySum(maxes);
Expand Down
6 changes: 3 additions & 3 deletions lab2/IntList/IntListExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class IntListExercises {
*/
public static void addConstant(IntList lst, int c) {
IntList head = lst;
while (head.rest != null) {
while (head != null) {
head.first += c;
head = head.rest;
}
Expand Down Expand Up @@ -51,7 +51,7 @@ public static int max(IntList L) {
*/
public static boolean firstDigitEqualsLastDigit(int x) {
int lastDigit = x % 10;
while (x > 10) {
while (x >= 10) {
x = x / 10;
}
int firstDigit = x % 10;
Expand All @@ -77,6 +77,6 @@ public static boolean squarePrimes(IntList lst) {
lst.first *= lst.first;
}

return currElemIsPrime || squarePrimes(lst.rest);
return squarePrimes(lst.rest)||currElemIsPrime ;
}
}
7 changes: 7 additions & 0 deletions lab2/IntList/SquarePrimesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ public void testSquarePrimesSimple() {
assertEquals("14 -> 15 -> 16 -> 289 -> 18", lst.toString());
assertTrue(changed);
}
@Test
public void testSquarePrimes2() {
IntList lst = IntList.of(11, 15, 16, 20, 13);
boolean changed = IntListExercises.squarePrimes(lst);
assertEquals("121 -> 15 -> 16 -> 20 -> 169", lst.toString());
assertTrue(changed);
}
}
8 changes: 4 additions & 4 deletions proj0/game2048/GUISource.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class GUISource implements InputSource {
public String getKey() {
String command = _source.readKey();
switch (command) {
case "" :
case "W" :
command = "Up";
break;
case "" :
case "D" :
command = "Right";
break;
case "" :
case "S" :
command = "Down";
break;
case "" :
case "A" :
command = "Left";
break;
default :
Expand Down

0 comments on commit fac9c69

Please sign in to comment.