Skip to content
Open

d #15

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
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/DebugExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static int max(int a, int b) {
step out button because you're not going to learn anything. */
int z = ~(b - a) >> 31;

int max = b & w | a & z;
int max = a & w | b & z;
return max;
}

Expand Down Expand Up @@ -58,7 +58,7 @@ public static int arraySum(int[] x) {
int i = 0;
int sum = 0;
while (i < x.length) {
sum = sum + add(sum, x[i]);
sum = add(sum, x[i]);
i = i + 1;
}
return sum;
Expand Down
10 changes: 5 additions & 5 deletions lab2/IntList/IntListExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class IntListExercises {
* @param lst IntList from Lecture
*/
public static void addConstant(IntList lst, int c) {
IntList head = lst;
IntList head=new IntList(0,lst);
while (head.rest != null) {
head.first += c;
head.rest.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 @@ -76,7 +76,7 @@ public static boolean squarePrimes(IntList lst) {
if (currElemIsPrime) {
lst.first *= lst.first;
}

return currElemIsPrime || squarePrimes(lst.rest);
boolean restResult = squarePrimes(lst.rest);
return currElemIsPrime || restResult;
}
}
8 changes: 8 additions & 0 deletions lab2/IntList/SquarePrimesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public void testSquarePrimesSimple() {
assertEquals("14 -> 15 -> 16 -> 289 -> 18", lst.toString());
assertTrue(changed);
}

@Test
public void testSquarePrimesSimpe() {
IntList lst = IntList.of(2, 15, 16, 17, 18);
boolean changed = IntListExercises.squarePrimes(lst);
assertEquals("4 -> 15 -> 16 -> 289 -> 18", lst.toString());
assertTrue(changed);
}
}
54 changes: 43 additions & 11 deletions lab2setup/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- 保留父级配置 -->
<parent>
<groupId>CS61B</groupId>
<artifactId>61BMasterPom</artifactId>
Expand All @@ -15,30 +16,61 @@
<artifactId>lab2setup</artifactId>
<version>1.0-SNAPSHOT</version>

<!-- 添加本地依赖项 -->
<dependencies>
<!-- JUnit 核心库 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>system</scope>
<systemPath>${basedir}/../library-sp21/javalib/junit-4.12.jar</systemPath>
</dependency>

<!-- Hamcrest 匹配器 -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/../library-sp21/javalib/hamcrest-core-1.3.jar</systemPath>
</dependency>

<!-- 其他课程核心库 -->
<dependency>
<groupId>CS61B</groupId>
<artifactId>jh61b</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/../library-sp21/javalib/jh61b.jar</systemPath>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}</sourceDirectory>
<testSourceDirectory>${project.basedir}</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
<!-- 简化编译器插件配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>9</source>
<target>9</target>
<compilerArgs>
<arg>-J-XX:+ShowCodeDetailsInExceptionMessages</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>

<!-- 禁用远程仓库 -->
<repositories>
<repository>
<id>local-only</id>
<url>file://${basedir}/../library-sp21/javalib</url>
</repository>
</repositories>
</project>
2 changes: 1 addition & 1 deletion lab3/randomizedtest/BuggyAList.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int size() {
* returns deleted item. */
public Item removeLast() {
if ((size < items.length / 4) && (size > 4)) {
resize(size / 4);
resize(size );
}
Item x = getLast();
items[size - 1] = null;
Expand Down
58 changes: 58 additions & 0 deletions lab3/randomizedtest/TestBuggyAList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package randomizedtest;

import afu.org.checkerframework.checker.igj.qual.I;
import edu.princeton.cs.algs4.StdRandom;
import net.sf.saxon.om.Item;
import org.junit.Test;
import static org.junit.Assert.*;

Expand All @@ -9,4 +11,60 @@
*/
public class TestBuggyAList {
// YOUR TESTS HERE
@Test
public void testThreeAddThreeRemove() {
AListNoResizing<Integer> correct=new AListNoResizing<>();
BuggyAList<Integer> broken =new BuggyAList<>();
correct.addLast(5);
correct.addLast(20);
correct.addLast(10);

broken.addLast(5);
broken.addLast(20);
broken.addLast(10);

assertEquals(correct.size(),broken.size());

assertEquals(correct.removeLast(), broken.removeLast());
assertEquals(correct.removeLast(), broken.removeLast());
assertEquals(correct.removeLast(), broken.removeLast());
}
@Test
public void JUnit() {
AListNoResizing<Integer> correct = new AListNoResizing<>();
BuggyAList<Integer> broken = new BuggyAList<>();
int N = 5000;
for (int i = 0; i < N; i += 1) {
int operationNumber = StdRandom.uniform(0, 4);

if (operationNumber == 0) {
// addLast
int randVal = StdRandom.uniform(0, 100);
correct.addLast(randVal);
broken.addLast(randVal);
System.out.println("addLast(" + randVal + ")");

} else if (operationNumber == 1) {
// size
int correct_size = correct.size();
int broken_size=broken.size();
assertEquals("Size mismatch after operation " + i,correct_size,broken_size);
System.out.println("size: " + correct_size);

}else if (operationNumber == 2 && correct.size() > 0) {
// getLast - 仅当列表非空时执行
Integer correctLast = correct.getLast();
Integer brokenLast = broken.getLast();
assertEquals("getLast mismatch after operation " + i, correctLast, brokenLast);
System.out.println("getLast: " + correctLast);

}else if (operationNumber == 3 && correct.size() > 0) {
Integer correctRemoved = correct.removeLast();
Integer brokenRemoved = broken.removeLast();
assertEquals("removeLast mismatch after operation " + i, correctRemoved, brokenRemoved);
System.out.println("removeLast: " + correctRemoved);
}
assertEquals("Size mismatch after operation " + i, correct.size(), broken.size());
}
}
}
4 changes: 3 additions & 1 deletion lab3/timingtest/AList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package timingtest;

/** Array based list.
* @author Josh Hug
*/
Expand Down Expand Up @@ -63,4 +62,7 @@ public Item removeLast() {
size = size - 1;
return x;
}


}

19 changes: 19 additions & 0 deletions lab3/timingtest/TimeAList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,24 @@ public static void main(String[] args) {

public static void timeAListConstruction() {
// TODO: YOUR CODE HERE
AList<Integer> Ns = new AList<>();
AList<Double> times = new AList<>();
AList<Integer> opCounts = new AList<>();

int currentN = 1000;
while (currentN <= 128000) {
Stopwatch sw = new Stopwatch();
AList<Integer> list = new AList<>();
for (int i = 0; i < currentN; i++) {
list.addLast(0);
}
double time = sw.elapsedTime();
Ns.addLast(currentN);
times.addLast(time);
opCounts.addLast(currentN);
currentN *= 2;
}

printTimingTable(Ns, times, opCounts);
}
}
25 changes: 24 additions & 1 deletion lab3/timingtest/TimeSLList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ public static void main(String[] args) {

public static void timeGetLast() {
// TODO: YOUR CODE HERE
}
AList<Integer> Ns = new AList<>();
AList<Double> times = new AList<>();
AList<Integer> opCounts = new AList<>();

int currentN = 1000;
int M = 10000; // 固定每次调用 M 次 getLast

while (currentN<=128000) {
SLList<Integer> list=new SLList<>();
for (int i=0;i<currentN;i++) {
list.addLast(0);
}
Stopwatch sw=new Stopwatch();
for (int i=0;i<M;i++) {
list.getLast();
}
double time=sw.elapsedTime();
Ns.addLast(currentN);
times.addLast(time);
opCounts.addLast(M);

currentN*=2;
}
printTimingTable(Ns, times, opCounts);
}
}
Loading