高级农民
- 积分
- 1014
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-1-12
- 最后登录
- 1970-1-1
|
1. DoubleChain.java
public class DoubleChain {
private DNode head;
public DoubleChain(){
}
public DoubleChain(double val) {
/* your code here. */
head = new DNode(val);
}
public DNode getFront() {
return head;
}
/** Returns the last item in the DoubleChain. */
public DNode getBack() {
/* your code here */
DNode p = head;
while(p.next!=null){
p = p.next;
}
return p;
}
/** Adds D to the front of the DoubleChain. */
public void insertFront(double d) {
/* your code here */
DNode oldF = head;
head = new DNode(d);
head.next = oldF;
oldF.prev = head;
}
/** Adds D to the back of the DoubleChain. */
public void insertBack(double d) {
/* your code here */
DNode p = head;
if (p == null){
head = new DNode(d);
}else{
while (p.next!=null){
p = p.next;
}
DNode back = new DNode(p,d,null);
p.next = back;
}
}
/** Removes the last item in the DoubleChain and returns it.
* This is an extra challenge problem. */
public DNode deleteBack() {
/* your code here */
DNode p = head;
if (p != null){
while (p.next!=null){
p = p.next;
}
if (p.prev == null){
head = null;
}else{
p.prev.next = null;
}
return p;
}
return null;
}
/** Returns a string representation of the DoubleChain.
* This is an extra challenge problem. */
public String toString() {
/* your code here */
DNode p = head;
String r = "<[";
if (p!=null){
while (p.next!=null){
r += ""+p.val;
}
}
r+="]>";
return r;
}
public static class DNode {
public DNode prev;
public DNode next;
public double val;
private DNode(double val) {
this(null, val, null);
}
private DNode(DNode prev, double val, DNode next) {
this.prev = prev;
this.val = val;
this.next =next;
}
}
}
由于JH61B好像有些问题,运行时提示不能支持该类,故改用ECLIPSE自带的JUNIT4进行测试,3个测试METHOD全部通过
2. TestDoubleChain.java
import static org.junit.Assert.*;
import org.junit.Test;
/** Perform tests of the DoubleChain class
*/
public class TestDoubleChain {
/** Tests the constructor of DoubleChain */
@Test
public void testConstructor() {
DoubleChain d = new DoubleChain(5);
assertEquals(5,d.getFront().val, 1e-6);
assertEquals(null, d.getFront().prev);
assertEquals(null, d.getFront().next);
}
/** Tests some basic DoubleChain operations. */
@Test
public void testBasicOperations() {
DoubleChain d = new DoubleChain(5);
assertEquals(5, d.getFront().val, 1e-11);
assertEquals(5, d.getBack().val, 1e-11);
d.insertFront(2);
d.insertFront(1);
d.insertBack(7);
d.insertBack(8);
assertEquals(1, d.getFront().val, 1e-11);
assertEquals(8, d.getBack().val, 1e-11);
}
/** Tests some advanced DoubleChain operations. */
@Test
public void testAdvancedOperations() {
DoubleChain d = new DoubleChain(5);
assertEquals(5, d.deleteBack().val, 1e-11);
String emptychain = "<[]>";
assertEquals(emptychain, d.toString());
d.insertBack(1.0);
d.insertBack(2.1);
d.insertBack(3.3);
d.insertBack(4.6);
String expected = "<[5.0,1.0,2.1,3.3,4.6]>";
assertEquals(expected, d.toString());
}
public static void main(String[] args) {
TestDoubleChain t= new TestDoubleChain();
t.testConstructor();
t.testBasicOperations();
t.testAdvancedOperations();
}
}
|
|