活跃农民
- 积分
- 923
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-9-13
- 最后登录
- 1970-1-1
|
来交作业了:
IntList.java:
import java.util.Formatter;
/** Scheme-like pairs that can be used to form a list of integers.
* @author P. N. Hilfinger, with some modifications by Josh Hug and melaniecebula
* [Do not modify this file.]
*/
public class IntList {
/** First element of list. */
public int head;
/** Remaining elements of list. */
public IntList tail;
/** A List with head HEAD0 and tail TAIL0. */
public IntList(int head0, IntList tail0) {
head = head0;
tail = tail0;
}
/** A List with null tail, and head = 0. */
public IntList() {
/* NOTE: public IntList () { } would also work. */
this(0, null);
}
/** Returns a list equal to L with all elements squared. Destructive. */
public static void dSquareList(IntList L) {
while (L != null) {
L.head = L.head * L.head;
L = L.tail;
}
}
/** Returns a list equal to L with all elements squared. Non-destructive. */
public static IntList squareListIterative(IntList L) {
if (L == null) {
return null;
}
IntList res = new IntList(L.head * L.head, null);
IntList ptr = res;
L = L.tail;
while (L != null) {
ptr.tail = new IntList(L.head * L.head, null);
L = L.tail;
ptr = ptr.tail;
}
return res;
}
/** Returns a list equal to L with all elements squared. Non-destructive. */
public static IntList squareListRecursive(IntList L) {
if (L == null) {
return null;
}
return new IntList(L.head * L.head, squareListRecursive(L.tail));
}
/** DO NOT MODIFY ANYTHING ABOVE THIS LINE! */
/** Returns a list consisting of the elements of A followed by the
** elements of B. May modify items of A. Don't use 'new'. */
public static IntList dcatenate(IntList A, IntList B) {
//TODO: fill in method
IntList a = A;
while(a.tail!=null){
a = a.tail;
}
a.tail = B;
return A;
}
/** Returns a list consisting of the elements of A followed by the
** elements of B. May NOT modify items of A. Use 'new'. */
public static IntList catenate(IntList A, IntList B) {
//TODO: fill in method
IntList a = A;
IntList b = B;
IntList temp = null;
IntList newlist = null;
if(A==null){
return B;
}
newlist = new IntList(A.head,null);
temp = newlist;
a = a.tail;
while(a!=null){
temp.tail = new IntList(a.head,null);
temp = temp.tail;
a = a.tail;
}
while(b!=null){
temp.tail = new IntList(b.head,null);
temp = temp.tail;
b = b.tail;
}
return newlist;
}
/** DO NOT MODIFY ANYTHING BELOW THIS LINE! In fact, I wouldn't even
* look below this line since it's likely to confuse you. */
@Override
public int hashCode() {
return head;
}
/** Returns a new IntList containing the ints in ARGS. You are not
* expected to read or understand this method. */
public static IntList list(Integer ... args) {
IntList result, p;
if (args.length > 0) {
result = new IntList(args[0], null);
} else {
return null;
}
int k;
for (k = 1, p = result; k < args.length; k += 1, p = p.tail) {
p.tail = new IntList(args[k], null);
}
return result;
}
/** Returns true iff X is an IntList containing the same sequence of ints
* as THIS. Cannot handle IntLists with cycles. You are not expected to
* read or understand this method. */
public boolean equals(Object x) {
if (!(x instanceof IntList)) {
return false;
}
IntList L = (IntList) x;
IntList p;
for (p = this; p != null && L != null; p = p.tail, L = L.tail) {
if (p.head != L.head) {
return false;
}
}
if (p != null || L != null) {
return false;
}
return true;
}
/** If a cycle exists in the IntList, this method
* returns an integer equal to the item number of the location where the
* cycle is detected.
*
* If there is no cycle, the number 0 is returned instead. This is a
* utility method for lab2. You are not expected to read, understand, or
* even use this method. The point of this method is so that if you convert
* an IntList into a String and that IntList has a loop, your computer
* don't get stuck in an infinite loop.
*/
private int detectCycles(IntList A) {
IntList tortoise = A;
IntList hare = A;
if (A == null)
return 0;
int cnt = 0;
while (true) {
cnt++;
if (hare.tail != null)
hare = hare.tail.tail;
else
return 0;
tortoise = tortoise.tail;
if (tortoise == null || hare == null)
return 0;
if (hare == tortoise)
return cnt;
}
}
@Override
/** Outputs the IntList as a String. You are not expected to read
* or understand this method. */
public String toString() {
Formatter out = new Formatter();
String sep;
sep = "(";
int cycleLocation = detectCycles(this);
int cnt = 0;
for (IntList p = this; p != null; p = p.tail) {
out.format("%s%d", sep, p.head);
sep = ", ";
cnt++;
if ((cnt > cycleLocation) && (cycleLocation > 0)) {
out.format("... (cycle exists) ...");
break;
}
}
out.format(")");
return out.toString();
}
}
IntlistTest.java
import static org.junit.Assert.*;
import org.junit.Test;
public class IntListTest {
/** Example test that verifies correctness of the IntList.list static
* method. The main point of this is to convince you that
* assertEquals knows how to handle IntLists just fine.
*/
@Test
public void testList() {
IntList one = new IntList(1, null);
IntList twoOne = new IntList(2, one);
IntList threeTwoOne = new IntList(3, twoOne);
IntList x = IntList.list(3, 2, 1);
assertEquals(threeTwoOne, x);
}
@Test
public void testdSquareList() {
IntList L = IntList.list(1, 2, 3);
IntList.dSquareList(L);
assertEquals(IntList.list(1, 4, 9), L);
}
/** Do not use the new keyword in your tests. You can create
* lists using the handy IntList.list method.
*
* Make sure to include test cases involving lists of various sizes
* on both sides of the operation. That includes the empty list, which
* can be instantiated, for example, with
* IntList empty = IntList.list().
*
* Keep in mind that dcatenate(A, B) is NOT required to leave A untouched.
* Anything can happen to A.
*/
//TODO: Create testSquareListRecursive()
@Test
public void testSquareListRecursive(){
IntList L = IntList.list(1,2,3);
IntList result = IntList.squareListRecursive(L);
assertEquals(IntList.list(1,4,9),result);
}
//TODO: Create testDcatenate and testCatenate
@Test
public void testDcatenate(){
IntList A = IntList.list(1,2,3);
IntList B = IntList.list(4,5,6);
IntList C = IntList.dcatenate(A,B);
assertEquals(IntList.list(1,2,3,4,5,6),C);
}
@Test
public void testCatenate(){
IntList A = IntList.list(1,2,3);
IntList B = IntList.list(4,5,6);
IntList C = IntList.catenate(A,B);
assertEquals(IntList.list(1,2,3,4,5,6),C);
assertEquals(IntList.list(1,2,3),A);
}
/* Run the unit tests in this file. */
public static void main(String... args) {
jh61b.junit.textui.runClasses(IntListTest.class);
}
}
|
|