diff --git a/group14/296933284/Note/README.md b/group14/296933284/Note/README.md index 0b08b8f0bb..8741e7204b 100644 --- a/group14/296933284/Note/README.md +++ b/group14/296933284/Note/README.md @@ -2,4 +2,5 @@ --- 1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) -2. [Java中解析XML的方法 2017-03-04](http://tennyson.ren/2017/03/04/Java%E4%B8%AD%E8%A7%A3%E6%9E%90XML%E7%9A%84%E6%96%B9%E6%B3%95/#more) \ No newline at end of file +2. [Java中解析XML的方法 2017-03-04](http://tennyson.ren/2017/03/04/Java%E4%B8%AD%E8%A7%A3%E6%9E%90XML%E7%9A%84%E6%96%B9%E6%B3%95/#more) +3. [Java多线程下载 2017-03-12](http://tennyson.ren/2017/03/12/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E4%B8%8B%E8%BD%BD/) \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coderising/LinkList/LinkedList.java b/group14/296933284/coding/src/com/coderising/LinkList/LinkedList.java new file mode 100644 index 0000000000..a07de29308 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/LinkList/LinkedList.java @@ -0,0 +1,355 @@ +package com.coderising.LinkList; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +import java.util.Collection; + + +/** + * LinkedList (单链表) 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + super(); + this.head = new Node(null); + } + + public Node getHead() { + return head; + } + + @Override + public boolean add(T element) { + addLast(element); + return true; + } + + @Override + public void add(int index, T element) { + + if (index == size) { + addLast(element); + } else { + Node r = getPreNode(index); + Node node = new Node<>(element); + node.next = r.next; + r.next = node; + size++; + + } + } + + public void addFirst(T element) { + Node node = new Node<>(element); + node.next = head.next; + head.next = node; + size++; + } + + public void addLast(T element) { + + Node node = new Node<>(element); + + Node r = head; + while (r.next != null) r = r.next; + + r.next = node; + + size++; + + } + + public void addAll(Collection c) { + + Iterator iter = (Iterator) c.iterator(); + + while (iter.hasNext()) { + addLast(iter.next()); + } + } + + @Override + public T get(int index) { + + rangCheck(index); + + return (T) getPreNode(index).next.data; + } + + @Override + public T remove(int index) { + + rangCheck(index); + + Node r = getPreNode(index); + + T result = (T) r.next.data; + + r.next = r.next.next; + size--; + + return result; + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + private Node getPreNode(int index) { + + rangCheck(index); + + if (index == 0) { + return head; + } else { + Node r = head; + + for (int i = 0; i < index; i++) + r = r.next; + + return r; + } + + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public Iterator iterator() { + return new Iter<>(); + } + + private class Iter implements Iterator { + int current = 0; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public T next() { + int i = current; + + rangCheck(i); + + current++; + + return (T) get(i); + } + + } + + private void rangCheck(int index) { + if ( index < 0 || index >= size) + throw new IndexOutOfBoundsException(); + } + + private static class Node { + T data; + Node next; + + Node(T data) { + super(); + this.data = data; + this.next = null; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node r = head.next; + Node p = null; + head.next = null; + + while (r != null) { + p = r; + r = r.next; + p.next = head.next; + head.next = p; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + int len = (int) Math.ceil(size / 2.0); + + remove(0, len); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length) { + + rangCheck(i); + + if (i + length - 1 > size - i) { + throw new IndexOutOfBoundsException(); + } + + Node preFirst = getPreNode(i); + Node preLast = getPreNode(i + length - 1).next; + + preFirst.next = preLast.next; + preLast = null; + size -= length; + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list) { + int[] elements = new int[list.size()]; + + for (int i = 0; i < list.size(); i++) { + elements[i] = (Integer) get((int) list.get(i)); + } + + return elements; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + int len; + for (int i = 0; i < list.size(); i++) { + Node p = head; + Node r = null; + + T value = list.get(i); + + while (p.next != null) { + + if (p.next.data.equals(value)) { + r = p.next; + p.next = r.next; + r.next = null; + size--; + } else { + p = p.next; + } + + + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node p = head; + Node r = head.next; + + while (p.next != null && r.next != null) { + if (p.next.data.compareTo(r.next.data) == 0) { + p.next = r.next; + r.next = p.next.next; + size--; + } else { + p = p.next; + r = r.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Node p = head; + + while (p.next!= null) { + if (p.next.data.compareTo(min) > 0 && p.next.data.compareTo(max) < 0) { + Node r = p.next; + p.next = r.next; + r.next = null; + size--; + } else { + p = p.next; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + LinkedList newList = new LinkedList(); + + Node p1 = head; + + while (p1.next != null) { + Node p2 = list.getHead(); + while (p2.next != null && p1.next.data.compareTo(p2.next.data) != 0) { + p2 = p2.next; + } + + if (p2.next != null) { + newList.add(p2.next.data); + } + p1 = p1.next; + } + + return newList; + } +} + + + + + + + + + + + + + diff --git a/group14/296933284/coding/src/com/coderising/LinkList/LinkedListTest.java b/group14/296933284/coding/src/com/coderising/LinkList/LinkedListTest.java new file mode 100644 index 0000000000..2cef12b030 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/LinkList/LinkedListTest.java @@ -0,0 +1,139 @@ +package com.coderising.LinkList; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by damocles on 2017/3/7. + */ +public class LinkedListTest { + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList<>(); + + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void reverse() throws Exception { + linkedList.reverse(); + + Assert.assertEquals(701, (int) linkedList.get(0)); + Assert.assertEquals(601, (int) linkedList.get(1)); + Assert.assertEquals(501, (int) linkedList.get(2)); + Assert.assertEquals(401, (int) linkedList.get(3)); + Assert.assertEquals(301, (int) linkedList.get(4)); + Assert.assertEquals(201, (int) linkedList.get(5)); + Assert.assertEquals(101, (int) linkedList.get(6)); + + } + + @Test + public void removeFirstHalf() throws Exception { + linkedList.removeFirstHalf(); + + Assert.assertEquals(501, (int) linkedList.get(0)); + Assert.assertEquals(3, linkedList.size()); + } + + @Test + public void remove() throws Exception { + linkedList.remove(0, 3); + + Assert.assertEquals(4, linkedList.size()); + Assert.assertEquals(401, (int) linkedList.get(0)); + + linkedList.remove(1, 3); + + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals(401, (int) linkedList.get(0)); + } + + @Test + public void getElements() throws Exception { + LinkedList list = new LinkedList<>(); + list.add(0); + list.add(2); + list.add(4); + list.add(6); + int[] ints = new int[]{101, 301, 501, 701}; + + Assert.assertArrayEquals(ints, linkedList.getElements(list)); + } + + @Test + public void subtract() throws Exception { + LinkedList list = new LinkedList<>(); + list.add(101); + list.add(301); + list.add(401); + list.add(601); + + linkedList.subtract(list); + + Assert.assertEquals(3, linkedList.size()); + Assert.assertEquals(201, (int) linkedList.get(0)); + Assert.assertEquals(701, (int) linkedList.get(2)); + } + + @Test + public void removeDuplicateValues() throws Exception { + linkedList.removeDuplicateValues(); + + Assert.assertEquals(7, linkedList.size()); + + linkedList.add(701); + linkedList.add(801); + linkedList.add(901); + linkedList.add(901); + linkedList.add(901); + linkedList.removeDuplicateValues(); + + Assert.assertEquals(9, linkedList.size()); + Assert.assertEquals(901, (int) linkedList.get(8)); + Assert.assertEquals(801, (int) linkedList.get(7)); + Assert.assertEquals(701, (int) linkedList.get(6)); + Assert.assertEquals(301, (int) linkedList.get(2)); + } + + @Test + public void removeRange() throws Exception { + linkedList.removeRange(101, 601); + + Assert.assertEquals(3, linkedList.size()); + Assert.assertEquals(101, (int) linkedList.get(0)); + Assert.assertEquals(601, (int) linkedList.get(1)); + Assert.assertEquals(701, (int) linkedList.get(2)); + } + + @Test + public void intersection() throws Exception { + LinkedList linkedList2 = new LinkedList<>(); + linkedList2.add(301); + linkedList2.add(401); + + LinkedList newList = linkedList.intersection(linkedList2); + + Assert.assertEquals(2, newList.size()); + Assert.assertEquals(301, (int) newList.get(0)); + Assert.assertEquals(401, (int) newList.get(1)); + } + +} \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coderising/download/DownloadThread.java b/group14/296933284/coding/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..ecf3686ff8 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,43 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread { + + private RandomAccessFile randomAccessFile; + private Connection conn; + private int startPos; + private int endPos; + private static int id = 0; + + public DownloadThread(Connection conn, RandomAccessFile randomAccessFile,int startPos, int endPos) { + this.conn = conn; + this.randomAccessFile = randomAccessFile; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run() { + try { + + randomAccessFile.seek(startPos); + byte[] bytes = conn.read(startPos, endPos); + + randomAccessFile.write(bytes, 0, bytes.length); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (randomAccessFile != null) { + try { + randomAccessFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/group14/296933284/coding/src/com/coderising/download/FileDownloader.java b/group14/296933284/coding/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..e4ef24ef17 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,97 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +import java.io.IOException; +import java.io.RandomAccessFile; + + +public class FileDownloader { + + private String url; + private DownloadListener listener; + private ConnectionManager cm; + private String localPath; + private int threadCount; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + Connection conn = null; + RandomAccessFile randomAccessFile = null; + + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + int blockSize = length % threadCount == 0 ? length / threadCount : length / threadCount + 1; + for (int i = 0; i < threadCount; i++) { + int startPos = i * blockSize; + int endPos = startPos + blockSize - 1; + + if (i == threadCount - 1) { + endPos = length - 1; + } + + randomAccessFile = new RandomAccessFile(localPath, "rwd"); + randomAccessFile.setLength(length); + + Connection connection = cm.open(this.url); + + DownloadThread downloadThread = new DownloadThread(connection, randomAccessFile, startPos, endPos); + downloadThread.start(); + + } + getListener().notifyFinished(); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setLocalPath(String localPath) { + this.localPath = localPath; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public void setThreadCount(int threadCount) { + this.threadCount = threadCount; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group14/296933284/coding/src/com/coderising/download/FileDownloaderTest.java b/group14/296933284/coding/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..cab6385a21 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,61 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:80/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + downloader.setLocalPath("D:\\Tennyson\\test.jpg"); + downloader.setThreadCount(3); + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group14/296933284/coding/src/com/coderising/download/api/Connection.java b/group14/296933284/coding/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..76be0a9be2 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/api/Connection.java @@ -0,0 +1,25 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + byte[] read(int startPos,int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * @return + */ + int getContentLength(); + + /** + * 关闭连接 + */ + void close(); +} diff --git a/group14/296933284/coding/src/com/coderising/download/api/ConnectionException.java b/group14/296933284/coding/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group14/296933284/coding/src/com/coderising/download/api/ConnectionManager.java b/group14/296933284/coding/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..76a26a37fa --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + Connection open(String url) throws ConnectionException; + +} diff --git a/group14/296933284/coding/src/com/coderising/download/api/DownloadListener.java b/group14/296933284/coding/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..de81b7607d --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + void notifyFinished(); +} diff --git a/group14/296933284/coding/src/com/coderising/download/impl/ConnectionImpl.java b/group14/296933284/coding/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..d5a6105648 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,89 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + private HttpURLConnection httpURLConnection = null; + private int contentLength = 0; + private int responsecode = 0; + + public ConnectionImpl(HttpURLConnection urlConnection) { + this.httpURLConnection = urlConnection; + } + + public int getResponsecode() { + try { + responsecode = httpURLConnection.getResponseCode(); + } catch (IOException e) { + e.printStackTrace(); + } + + return responsecode; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + httpURLConnection.setConnectTimeout(5000); + + byte[] bytes = null; + if (getResponsecode() == 206) { + + InputStream inputStream = httpURLConnection.getInputStream(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(endPos - startPos); + + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, len); + } + + bytes = outputStream.toByteArray(); + + outputStream.close(); + inputStream.close(); + } + + return bytes; + } + + @Override + public int getContentLength() { + InputStream inputStream = null; + + try { + inputStream = httpURLConnection.getInputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = inputStream.read(buffer)) != -1) { + contentLength += len; + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return contentLength; + } + + @Override + public void close() { + + } + +} diff --git a/group14/296933284/coding/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group14/296933284/coding/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..d8197123b2 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,31 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection connection = null; + HttpURLConnection urlConnection = null; + try { + URL newUrl = new URL(url); + urlConnection = (HttpURLConnection) newUrl.openConnection(); + connection = new ConnectionImpl(urlConnection); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return connection; + } + +} diff --git a/group14/296933284/coding/src/com/coding/basic/ArrayList.java b/group14/296933284/coding/src/com/coding/basic/ArrayList.java index 116020466d..39b6b68268 100644 --- a/group14/296933284/coding/src/com/coding/basic/ArrayList.java +++ b/group14/296933284/coding/src/com/coding/basic/ArrayList.java @@ -8,7 +8,7 @@ * @author Tonnyson * */ -public class ArrayList implements List { +public class ArrayList implements List { private int size; private static final int DEFAULT_CAPACITY = 10; @@ -19,110 +19,78 @@ public ArrayList() { elementData = new Object[DEFAULT_CAPACITY]; } - public ArrayList(int initCapacity) { - elementData = new Object[initCapacity]; + @Override + public boolean add(T element) { + ensureCapacity(size + 1); + elementData[size++] = element; + return true; } - /** - * ĩβָԪأԶչΪԭȵ - */ - public void add(Object obj) { - - ensureCapacityInternal(size); - - elementData[size] = obj; - size++; - } - - - /** - * ָλòԪ - */ - public void add(int index, Object obj) { - + @Override + public void add(int index, T element) { rangCheckForAdd(index); - ensureCapacityInternal(size + 1); + + ensureCapacity(size + 1); - for (int i = size - 1; i >= index; i--) - elementData[i + 1] = elementData[i]; + System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = obj; + elementData[index] = element; size++; } - /** - * - */ - private void ensureCapacityInternal(int minCapacity) { + private void ensureCapacity(int minCapacity) { if (minCapacity - elementData.length > 0) { int newCapacity = elementData.length * 2; elementData = Arrays.copyOf(elementData, newCapacity); - // elementData = tempElementData; } } - - /** - * add() м±ǷԽ - */ + private void rangCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(); } - /** - * ָλõԪֵ - */ - public Object get(int index) { - + @Override + public T get(int index) { rangCheck(index); - return elementData[index]; + return (T) elementData[index]; } - /** - * ɾָλõԪأظֵ - */ - public Object remove(int index) { + @Override + public T remove(int index) { rangCheck(index); - Object obj = elementData[index]; - - for (int i = index; i < size; i++) - elementData[i] = elementData[i + 1]; + T element = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index,size - index - 1); + elementData[size - 1] = null; size--; - return obj; + return element; } - /** - * ±ǷԽ - * - * @param index - */ private void rangCheck(int index) { - if (index >= size) + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); } - /** - * 鳤 - */ + @Override public int size() { return size; } - - /** - * - * - * @return - */ - public Iterator iterator() { + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public Iterator iterator() { return new Iter(); } - //ڲ - private class Iter implements Iterator { + private class Iter implements Iterator { int current; @Override @@ -131,13 +99,13 @@ public boolean hasNext() { } @Override - public Object next() { + public T next() { int i = current; rangCheck(i); current++; - return elementData[i]; + return (T) elementData[i]; } } diff --git a/group14/296933284/coding/src/com/coding/basic/ArrayListTest.java b/group14/296933284/coding/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..40e2279ae4 --- /dev/null +++ b/group14/296933284/coding/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,72 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by damocles on 2017/3/6. + */ +public class ArrayListTest { + private ArrayList bookList; + + @Before + public void setUp() throws Exception { + bookList = new ArrayList<>(); + + bookList.add("java"); + bookList.add("javascript"); + bookList.add("c++"); + } + + @After + public void tearDown() throws Exception { + bookList = null; + } + + @Test + public void add() throws Exception { + Assert.assertTrue(bookList.add("javaScript")); + } + + @Test + public void add1() throws Exception { + Assert.assertEquals("java", bookList.get(0)); + Assert.assertEquals("c++", bookList.get(2)); + } + + @Test + public void get() throws Exception { + Assert.assertEquals("java", bookList.get(0)); + } + + @Test + public void remove() throws Exception { + Assert.assertEquals("javascript", bookList.remove(1)); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(3, bookList.size()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertFalse(bookList.isEmpty()); + } + + @Test + public void iterator() throws Exception { + + Iterator it = bookList.iterator(); + Assert.assertTrue(it.hasNext()); + int count = 0; + while (it.hasNext()) { + Assert.assertEquals(bookList.get(count++), it.next()); + } + } + +} \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java b/group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java index b74dbe85a2..5284c5e978 100644 --- a/group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java +++ b/group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java @@ -1,16 +1,10 @@ package com.coding.basic; -/** - * BST ʵ 14С 296933284 - * - * @author Tonnyson - * - */ -public class BinarySearchTree implements Comparable { - - private Object data; - private BinarySearchTree leftChild; - private BinarySearchTree rightChild; +public class BinarySearchTree { + + private T data; + private BinarySearchTree leftChild; + private BinarySearchTree rightChild; public BinarySearchTree() { super(); @@ -19,79 +13,50 @@ public BinarySearchTree() { this.rightChild = null; } - public BinarySearchTree(Object data) { + public BinarySearchTree(T data) { this(); this.data = data; } - public Object getData() { + public T getData() { return data; } - public void setData(Object data) { - this.data = data; + public BinarySearchTree getLeftChild() { + return leftChild; } - public BinarySearchTree getLeftChild() { - return leftChild; + public BinarySearchTree getRightChild() { + return rightChild; } - public void setLeftChild(BinarySearchTree leftChild) { - this.leftChild = leftChild; + public void setData(T data) { + this.data = data; } - public BinarySearchTree getRightChild() { - return rightChild; + public void setLeftChild(BinarySearchTree leftChild) { + this.leftChild = leftChild; } - public void setRightChild(BinarySearchTree rightChild) { + public void setRightChild(BinarySearchTree rightChild) { this.rightChild = rightChild; } - /** - * вڵ - * - * @param obj - * ڵֵ - */ - public void insert(Object obj) { - insert(obj, this); + public void insert(T element) { + insert(element, this); } - private boolean insert(Object obj, BinarySearchTree node) { - - BinarySearchTree bstNode = new BinarySearchTree(obj); + private boolean insert(T element, BinarySearchTree node) { - if (node == null) { - node = bstNode; - return true; - } else if (node.compareTo(obj) == 0) { - return true; - } else if (node.compareTo(obj) > 0) { + BinarySearchTree bstNode = new BinarySearchTree(element); - if (node.getLeftChild() != null) { - return insert(obj, node.getLeftChild()); - } - - node.leftChild = bstNode; - - } else if (node.compareTo(obj) < 0) { - - if (node.getRightChild() != null) { - return insert(obj, node.getRightChild()); - } - node.rightChild = bstNode; - } return false; } - /** - * BST Ľڵ㣬ʹ֮ - */ - public void inOrder(BinarySearchTree node) { + public void inOrder(BinarySearchTree node) { if (node != null) { inOrder(node.getLeftChild()); @@ -101,12 +66,9 @@ public void inOrder(BinarySearchTree node) { } - /** - * BST Ľڵֵ - */ - public void levelOrder(BinarySearchTree node) { + public void levelOrder(BinarySearchTree node) { Queue queue = new Queue(); - BinarySearchTree bstNode = null; + BinarySearchTree bstNode = null; queue.enQueue(node); while (!queue.isEmpty()) { @@ -123,32 +85,8 @@ public void levelOrder(BinarySearchTree node) { } } - /** - * ָڵֵ - * - * @param node - */ - public void visit(BinarySearchTree node) { + public void visit(BinarySearchTree node) { System.out.println(node.getData()); } - /** - * Ƚ BST ڵֵС - */ - @Override - public int compareTo(Object obj) { - int result = 0; - - if (obj instanceof Integer) { - Integer value = (Integer) obj; - Integer thisValue = (Integer) this.data; - result = thisValue.compareTo(value); - } else { - String value = obj.toString(); - result = this.data.toString().compareTo(value); - } - - return result; - } - } diff --git a/group14/296933284/coding/src/com/coding/basic/Iterator.java b/group14/296933284/coding/src/com/coding/basic/Iterator.java index e7cbd474ec..c10771d52c 100644 --- a/group14/296933284/coding/src/com/coding/basic/Iterator.java +++ b/group14/296933284/coding/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ package com.coding.basic; -public interface Iterator { - public boolean hasNext(); - public Object next(); +public interface Iterator { + boolean hasNext(); + T next(); } diff --git a/group14/296933284/coding/src/com/coding/basic/JavaTest.java b/group14/296933284/coding/src/com/coding/basic/JavaTest.java deleted file mode 100644 index 8c4cab01b6..0000000000 --- a/group14/296933284/coding/src/com/coding/basic/JavaTest.java +++ /dev/null @@ -1,176 +0,0 @@ -package com.coding.basic; - -import org.junit.Test; - -public class JavaTest { - - @Test - public void binarySearchTreeTest() { - BinarySearchTree bSTree = new BinarySearchTree(5); - - System.out.println(bSTree.getData()); - - // insert - bSTree.insert(1); - bSTree.insert(2); - bSTree.insert(4); - bSTree.insert(6); - bSTree.insert(7); - bSTree.insert(8); - System.out.println("-----------------"); - // inOrder - bSTree.inOrder(bSTree); - - System.out.println("-----------------"); - // levelOrder - bSTree.levelOrder(bSTree); - } - - @Test - public void queueTest() { - Queue queue = new Queue(); - - // enQueue() - for (int i = 0; i < 10; i++) { - queue.enQueue("hello: " + i); - } - - // size() - System.out.println(queue.size()); // 10 - // isEmpty - System.out.println(queue.isEmpty()); - - // deQueue() - for (int i = 0; i < 10; i++) { - System.out.println(queue.deQueue()); - } - - // size() - System.out.println(queue.size()); // 0 - - // isEmpty - System.out.println(queue.isEmpty()); - } - - @Test - public void stackTest() { - Stack stack = new Stack(); - - // push() - for (int i = 0; i < 10; i++) { - stack.push("hello: " + i); - } - - // size() - System.out.println(stack.size()); - - // pop() - for (int i = 0; i < 10; i++) { - System.out.println(stack.pop()); - } - - // isEmpty() - System.out.println(stack.isEmpty()); - System.out.println(stack.size()); - - stack.push("hello world 1"); - stack.push("hello world 2"); - stack.push("hello world 3"); - stack.push("hello world 4"); - - // peek() - System.out.println(stack.peek()); - - // isEmpty() - System.out.println(stack.isEmpty()); - } - - @Test - public void linkedListTest() { - LinkedList linkedList = new LinkedList(); - - // add() addLast() - for (int i = 0; i < 5; i++) { - linkedList.add("hello: " + i); - } - - // iterator() get() getPreNode() - Iterator iter = linkedList.iterator(); - - while (iter.hasNext()) { - System.out.println(iter.next()); - } - System.out.println("-----------------------"); - LinkedList linkedList1 = new LinkedList(); - - // addFirst() - for (int i = 0; i < 5; i++) { - linkedList1.addFirst("hello: " + i); - } - - Iterator iter1 = linkedList1.iterator(); - - while (iter1.hasNext()) { - System.out.println(iter1.next()); - } - - System.out.println("-----------------------"); - // remove() - System.out.println(linkedList1.remove(0)); // hello: 4 - - System.out.println("-----------------------"); - // removeFirst() removeLast() - System.out.println(linkedList1.removeFirst()); // hello: 3 - System.out.println(linkedList1.removeLast()); // hello: 0 - - System.out.println("-----------------------"); - // size() - System.out.println(linkedList.size()); // 5 - } - - @Test - public void arrayListTest() { - ArrayList arrayList = new ArrayList(); - - // add(obj) - for (int i = 0; i < 10; i++) { - arrayList.add("hello: " + i); - } - - // get(index) - for (int i = 0; i < arrayList.size(); i++) { - System.out.println("-->" + arrayList.get(i)); - } - - // add(index, obj) - arrayList.add(5, "Tennyson"); - - for (int i = 0; i < arrayList.size(); i++) { - System.out.println("++>" + arrayList.get(i)); - } - - // size() - System.out.println("size: " + arrayList.size()); - System.out.println("index 5: " + arrayList.get(5)); - - // remove() - Object value = arrayList.remove(5); - System.out.println("index 5: " + value); - System.out.println("size: " + arrayList.size()); - System.out.println("index5: " + arrayList.get(5)); - - for (int i = 0; i < arrayList.size(); i++) { - System.out.println("index " + i + " : " + arrayList.get(i)); - } - - System.out.println("-------------------------------"); - // iterator - Iterator iter = arrayList.iterator(); - while (iter.hasNext()) { - System.out.println(iter.next()); - } - - System.out.println("-------------------------------"); - - } -} diff --git a/group14/296933284/coding/src/com/coding/basic/LinkedList.java b/group14/296933284/coding/src/com/coding/basic/LinkedList.java index 3c37904532..edd0819fb8 100644 --- a/group14/296933284/coding/src/com/coding/basic/LinkedList.java +++ b/group14/296933284/coding/src/com/coding/basic/LinkedList.java @@ -4,145 +4,109 @@ /** - * LinkedList (ͷĵ) ʵ 14С 296933284 + * LinkedList (单链表) 第14小组 296933284 * * @author Tonnyson * */ -public class LinkedList implements List { +public class LinkedList implements List { - private Node head; + private Node head; private int size; public LinkedList() { super(); - this.head = new Node(); - this.size = 0; + this.head = new Node(null); } - public void add(Object obj) { - addLast(obj); + @Override + public boolean add(T element) { + addLast(element); + return true; } - public void add(int index, Object obj) { + @Override + public void add(int index, T element) { - if (index == size + 1) { - addLast(obj); + if (index == size) { + addLast(element); } else { - Node r = getPreNode(index); - Node node = new Node(); - node.data = obj; + Node r = getPreNode(index); + Node node = new Node<>(element); node.next = r.next; r.next = node; size++; } } - - - /** - * ײڵ - * - * @param obj ڵĽڵֵ - * - */ - public void addFirst(Object obj) { - Node node = new Node(); - node.data = obj; - + public void addFirst(T element) { + Node node = new Node<>(element); node.next = head.next; head.next = node; size++; } - /** - * βڵ - * - * @param obj ڵĽڵֵ - * - */ - public void addLast(Object obj) { - - Node node = new Node(); - node.data = obj; - node.next = null; + public void addLast(T element) { - Node r = head; + Node node = new Node<>(element); + + Node r = head; while (r.next != null) r = r.next; - + r.next = node; size++; } - /** - * Ԫذ˳뵥 - * - * @param c Ҫ뵥Ԫصļ - * - */ - public void addAll(Collection c) { + public void addAll(Collection c) { - Iterator iter = (Iterator) c.iterator(); + Iterator iter = (Iterator) c.iterator(); while (iter.hasNext()) { addLast(iter.next()); } } - /** - * ȡָλõĽڵֵ - */ - public Object get(int index) { - // rangCheck(index); + @Override + public T get(int index) { + + rangCheck(index); - return getPreNode(index).next.data; + return (T) getPreNode(index).next.data; } - /** - * ɾָλýڵ㣬ؽڵֵ - */ - public Object remove(int index) { + @Override + public T remove(int index) { rangCheck(index); - Node r = getPreNode(index); + Node r = getPreNode(index); - Object result = r.next.data; + T result = (T) r.next.data; r.next = r.next.next; size--; - return result; - } - - /** - * ɾһڵ㣬ؽڵֵ - * - * @return һڵֵ - */ - public Object removeFirst() { + + return result; + } + + public T removeFirst() { return remove(0); } - - /** - * ɾһڵ㣬ؽڵֵ - * - * @return һڵֵ - */ - public Object removeLast() { + + public T removeLast() { return remove(size - 1); } - - // ȡָλõǰ㲢 - private Node getPreNode(int index) { + + private Node getPreNode(int index) { rangCheck(index); if (index == 0) { return head; } else { - Node r = head; + Node r = head; for (int i = 0; i < index; i++) r = r.next; @@ -151,25 +115,23 @@ private Node getPreNode(int index) { } } - - /** - * صij - */ + + @Override public int size() { return size; } - /** - * - * - * @return һ - */ - public Iterator iterator() { - return new Iter(); + @Override + public boolean isEmpty() { + return size == 0; } - // ڲ - private class Iter implements Iterator { + @Override + public Iterator iterator() { + return new Iter<>(); + } + + private class Iter implements Iterator { int current = 0; @Override @@ -178,35 +140,30 @@ public boolean hasNext() { } @Override - public Object next() { + public T next() { int i = current; rangCheck(i); current++; - return get(i); + return (T) get(i); } } - - /** - * ǷԽ - * - * @param index - */ + private void rangCheck(int index) { - if (index > size || index < 0) + if ( index < 0 || index >= size) throw new IndexOutOfBoundsException(); } - private static class Node { - Object data; - Node next; - - public Node() { + private static class Node { + T data; + Node next; + + Node(T data) { super(); - this.data = null; + this.data = data; this.next = null; } diff --git a/group14/296933284/coding/src/com/coding/basic/LinkedListTest.java b/group14/296933284/coding/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2ca8543e53 --- /dev/null +++ b/group14/296933284/coding/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,104 @@ +package com.coding.basic; + +import org.intellij.lang.annotations.Flow; +import org.jetbrains.annotations.NotNull; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; + +import static org.junit.Assert.*; + +/** + * Created by damocles on 2017/3/6. + */ +public class LinkedListTest { + private LinkedList bookList; + + @Before + public void setUp() throws Exception { + bookList = new LinkedList<>(); + + bookList.add("javascript"); + bookList.add("java"); + bookList.add("c++"); + bookList.add("c"); + + } + + @After + public void tearDown() throws Exception { + bookList = null; + } + + @Test + public void add() throws Exception { + Assert.assertTrue(bookList.add("python")); + } + + @Test + public void add1() throws Exception { + bookList.add("python"); + Assert.assertEquals("python", bookList.removeLast()); + } + + @Test + public void addAll() throws Exception { + + } + + @Test + public void addFirst() throws Exception { + bookList.addFirst("python"); + Assert.assertEquals("python", bookList.removeFirst()); + } + + @Test + public void addLast() throws Exception { + bookList.addLast("python"); + Assert.assertEquals("python", bookList.removeLast()); + } + + @Test + public void get() throws Exception { + Assert.assertEquals("javascript", bookList.get(0)); + } + + @Test + public void remove() throws Exception { + Assert.assertEquals("javascript", bookList.remove(0)); + } + + @Test + public void removeFirst() throws Exception { + Assert.assertEquals("javascript", bookList.removeFirst()); + } + + @Test + public void removeLast() throws Exception { + Assert.assertEquals("c", bookList.removeLast()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(4, bookList.size()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertFalse(bookList.isEmpty()); + } + + @Test + public void iterator() throws Exception { + Iterator it = bookList.iterator(); + Assert.assertTrue(it.hasNext()); + int count = 0; + while (it.hasNext()) { + Assert.assertEquals(bookList.get(count++), it.next()); + } + } + +} \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coding/basic/List.java b/group14/296933284/coding/src/com/coding/basic/List.java index 969e6dd82b..019fe42c0f 100644 --- a/group14/296933284/coding/src/com/coding/basic/List.java +++ b/group14/296933284/coding/src/com/coding/basic/List.java @@ -1,9 +1,18 @@ package com.coding.basic; -public interface List { - public void add(Object obj); - public void add(int index, Object obj); - public Object get(int index); - public Object remove(int index); - public int size(); +public interface List { + + boolean add(T element); + + void add(int index, T element); + + T get(int index); + + T remove(int index); + + int size(); + + boolean isEmpty(); + + Iterator iterator(); } diff --git a/group14/296933284/coding/src/com/coding/basic/Queue.java b/group14/296933284/coding/src/com/coding/basic/Queue.java index 9257eb04ca..2759c25b5f 100644 --- a/group14/296933284/coding/src/com/coding/basic/Queue.java +++ b/group14/296933284/coding/src/com/coding/basic/Queue.java @@ -1,49 +1,27 @@ package com.coding.basic; /** - * Queue ʵ - * First In First Out - * 14С 296933284 - * + * Queue 实现 第14小组 296933284 + * * @author Tonnyson * */ -public class Queue { - - private LinkedList elementData = new LinkedList(); +public class Queue { - /** - * вԪ - * - * @param obj - */ - public void enQueue(Object obj){ - elementData.addLast(obj); + private LinkedList elementData = new LinkedList<>(); + + public void enQueue(T element){ + elementData.addLast(element); } - - /** - * ɾԪ - * - * @return - */ - public Object deQueue(){ + + public T deQueue(){ return elementData.removeFirst(); } - /** - * ж϶ǷΪ - * - * @return - */ public boolean isEmpty(){ return elementData.size() == 0; } - - /** - * ضеԪظ - * - * @return - */ + public int size(){ return elementData.size(); } diff --git a/group14/296933284/coding/src/com/coding/basic/QueueTest.java b/group14/296933284/coding/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..3a32d0f0c9 --- /dev/null +++ b/group14/296933284/coding/src/com/coding/basic/QueueTest.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by damocles on 2017/3/7. + */ +public class QueueTest { + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue<>(); + + queue.enQueue("javascript"); + queue.enQueue("java"); + queue.enQueue("c++"); + queue.enQueue("c"); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("php"); + + Assert.assertEquals(5, queue.size()); + } + + @Test + public void deQueue() throws Exception { + Assert.assertEquals("javascript",queue.deQueue()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertFalse(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(4, queue.size()); + } + +} \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coding/basic/Stack.java b/group14/296933284/coding/src/com/coding/basic/Stack.java index e28a9e3760..f3a1d2e681 100644 --- a/group14/296933284/coding/src/com/coding/basic/Stack.java +++ b/group14/296933284/coding/src/com/coding/basic/Stack.java @@ -1,60 +1,33 @@ package com.coding.basic; /** - * Stack ʵ - * Last In First Out - * 14С 296933284 - * + * Stack 实现 第14小组 296933284 + * * @author Tonnyson * */ -public class Stack { +public class Stack { - private ArrayList elementData = new ArrayList(); + private ArrayList elementData = new ArrayList<>(); private int top = 0; - /** - * ջвԪ - * - * @param obj - */ - public void push(Object obj) { - elementData.add(obj); + public void push(T element) { + elementData.add(element); top++; } - - /** - * ջȡԪ - * - * @return - */ - public Object pop() { + + public T pop() { return elementData.remove(--top); } - /** - * ȡջԪ - * - * @return - */ - public Object peek() { + public T peek() { return elementData.get(top - 1); } - /** - * жջǷΪ - * - * @return - */ public boolean isEmpty() { return top == 0; } - - /** - * ȡջԪظ - * - * @return - */ + public int size() { return top; } diff --git a/group14/296933284/coding/src/com/coding/basic/StackTest.java b/group14/296933284/coding/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..e67a4ec361 --- /dev/null +++ b/group14/296933284/coding/src/com/coding/basic/StackTest.java @@ -0,0 +1,55 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by damocles on 2017/3/7. + */ +public class StackTest { + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack<>(); + + stack.push("javascript"); + stack.push("java"); + stack.push("c++"); + stack.push("c"); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("php"); + Assert.assertEquals("php", stack.pop()); + } + + @Test + public void pop() throws Exception { + Assert.assertEquals("c", stack.pop()); + } + + @Test + public void peek() throws Exception { + Assert.assertEquals("c", stack.peek()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertFalse(stack.isEmpty()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(4, stack.size()); + } + +} \ No newline at end of file