diff --git a/group03/345943980/download-0335/pom.xml b/group03/345943980/download-0335/pom.xml index b62ba3fec1..5193da926b 100644 --- a/group03/345943980/download-0335/pom.xml +++ b/group03/345943980/download-0335/pom.xml @@ -28,8 +28,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/group03/345943980/mini-jvm-0330/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/group03/345943980/mini-jvm-0330/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..17abd10b7e --- /dev/null +++ b/group03/345943980/mini-jvm-0330/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,68 @@ +package com.coderising.jvm.loader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + className = className.replace(".", File.separator) + ".class"; + for (String path : clzPaths) { + String clzFileName = path + File.separator + className; + byte[] codes = this.loadClassFile(clzFileName); + if (codes != null) { + return codes; + } + } + return null; + } + + private byte[] loadClassFile(String clzFileName){ + try { + return IOUtils.toByteArray(new FileInputStream(clzFileName)); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath() { + return StringUtils.join(clzPaths, ";"); + } + + public String getClassPath_V1() { + StringBuffer sb = new StringBuffer(); + /* + * for (int i = 0; i < clzPaths.size(); i++) + { + if (i == clzPaths.size() - 1) { + sb.append(clzPaths.get(i)); + break; + } + sb.append(clzPaths.get(i)); + sb.append(";"); + * + * } + */ + for (int i = 0; i < clzPaths.size(); i++) { + sb.append(clzPaths.get(i)); + if (i < clzPaths.size() - 1) { + sb.append(";"); + } + } + return sb.toString(); + } + +} diff --git a/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/CmLRUPageFrame.java b/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/CmLRUPageFrame.java new file mode 100644 index 0000000000..890db81fb5 --- /dev/null +++ b/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/CmLRUPageFrame.java @@ -0,0 +1,69 @@ +package com.coding.basic.linklist; + +/** + * + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年4月10日 上午12:35:03 + */ +public class CmLRUPageFrame { + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + + public CmLRUPageFrame(int capacity) { + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum + */ + public void access(int pageNum) { + Node node = first; + Node foundNode = null; + while(node!=null){ + if(node.pageNum==pageNum){ + foundNode = node; + } + node = node.next; + } + + //在该队列中存在, 则提到队列头 + if(foundNode!=null){ + + }else{ + + } + + + + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + node = node.next; + if (node != null) { + buffer.append(";"); + } + } + return buffer.toString(); + } + +} diff --git a/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..3dba1b554b --- /dev/null +++ b/group03/345943980/mini-jvm-0330/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,163 @@ +package com.coding.basic.linklist; + + +/** + * 实现一个简单的LRU算法,即是 Least Recently Used 近期最少使用算法 + * 作用:需要维护一个页面中的栈,并且需要把某一个页面从栈中提到栈顶,用硬件实现的话,开销比较大,注意:实际上并不是一个真正的栈 + * @author Administrator + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group03/345943980/mini-jvm-0330/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java b/group03/345943980/mini-jvm-0330/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..cf4108c0d9 --- /dev/null +++ b/group03/345943980/mini-jvm-0330/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,87 @@ +package com.coderising.jvm.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + + +public class ClassFileloaderTest { + + + static String path1 = "E:\\github\\coding2017\\group03\\345943980\\mini-jvm-0330\\target\\test-classes"; + static String path2 = "D:\\wordtest"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i constantInfos = new ArrayList(); + + + public ConstantPool(){ + + } + public void addConstantInfo(ConstantInfo info){ + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/FieldRefInfo.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..7ff9d5fb77 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.coderising.jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/MethodRefInfo.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..0feffa65b5 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.coderising.jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + + + +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NameAndTypeInfo.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..dcac7f97c4 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.coderising.jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NullConstantInfo.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..fa90d110fe --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.coderising.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/StringInfo.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..d01065fd53 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.coderising.jvm.constant; + +public class StringInfo extends ConstantInfo{ + private int type = ConstantInfo.STRING_INFO; + private int index; + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/UTF8Info.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..b7407d146f --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.coderising.jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private int type = ConstantInfo.UTF8_INFO; + private int length ; + private String value; + public UTF8Info(ConstantPool pool) { + super(pool); + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getType() { + return type; + } + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + + + +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ByteCodeIterator.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..2dff6746da --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,5 @@ +package com.coderising.jvm.loader; + +public class ByteCodeIterator { + +} diff --git a/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..b9d2a3ba0c --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,131 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.coderising.jvm.clz.ClassFile; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + className = className.replace('.', File.separatorChar) +".class"; + + for(String path : this.clzPaths){ + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + return null; + } + + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + + } + + + + public String getClassPath(){ + return StringUtils.join(this.clzPaths,";"); + } + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } + + + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack<>(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + /** + * QQ号为:247565311的解法 + * @param s + */ + public static void reverse_247565311(Stack s){ + if(s == null || s.isEmpty()) { + return; + } + + int size = s.size(); + Stack tmpStack = new Stack(); + + for(int i=0;ii){ + tmpStack.push(s.pop()); + } + s.push(top); + while(tmpStack.size()>0){ + s.push(tmpStack.pop()); + } + } + } + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + + Stack tmp = new Stack(); + while(!s.isEmpty()){ + tmp.push(s.pop()); + } + while(!tmp.isEmpty()){ + Integer top = tmp.pop(); + addToBottom(s,top); + } + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() tmpStack = new Stack<>(); + int i = 0; + Object[] result = new Object[len]; + while(!s.isEmpty()){ + Object value = s.pop(); + tmpStack.push(value); + result[i++] = value; + if(i == len){ + break; + } + } + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + return result; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + + Stack stack = new Stack<>(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + + +} diff --git a/group03/345943980/mini-jvm-0405/src/test/java/com/coderising/jvm/test/EmployeeV1.java b/group03/345943980/mini-jvm-0405/src/test/java/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..9a36573dd3 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/test/java/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group03/345943980/mini-jvm-0405/src/test/java/com/coding/basic/stack/StackUtilTest.java b/group03/345943980/mini-jvm-0405/src/test/java/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..6c8db075d0 --- /dev/null +++ b/group03/345943980/mini-jvm-0405/src/test/java/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,77 @@ +package com.coding.basic.stack; + +import java.util.Stack; + +import org.junit.Assert; +import org.junit.Test; +public class StackUtilTest { + + @Test + public void testAddToBottom() { + + Stack s = new Stack<>(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.addToBottom(s, 0); + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + + @Test + public void testReverse() { + Stack s = new Stack<>(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testReverse_247565311() { + Stack s = new Stack<>(); + s.push(1); + s.push(2); + s.push(3); + + Assert.assertEquals("[1, 2, 3]", s.toString()); + StackUtil.reverse_247565311(s); + Assert.assertEquals("[3, 2, 1]", s.toString()); + } + @Test + public void testRemove() { + Stack s = new Stack<>(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack<>(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..498aeb3e59 --- /dev/null +++ b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,74 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +/** + * 用两个栈实现中序表达式求值 + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年5月7日 下午4:17:24 + */ +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack opStack = new Stack<>(); + Stack numStack = new Stack<>(); + + for(Token token : tokens){ + + if (token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + Token prevOperator = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + Float result = calculate(prevOperator.toString(), f1,f2); + numStack.push(result); + + } + opStack.push(token); + } + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } + } + + while(!opStack.isEmpty()){ + Token token = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(), f1,f2)); + } + + + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/Token.java b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..72720b0da1 --- /dev/null +++ b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Token { + + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } +} diff --git a/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d1b586bce0 --- /dev/null +++ b/group03/345943980/mini-jvm-0409/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + public List parse(String expr) { + List tokens = new ArrayList<>(); + int i = 0; + while (i < expr.length()) { + char c = expr.charAt(i); + if (isOperator(c)) { + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + } else if (Character.isDigit(c)) { + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + } else { + System.out.println("char :[" + c + "] is not number or operator,ignore"); + i++; + } + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/InfixExprTest.java b/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..1d1913e0ad --- /dev/null +++ b/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,38 @@ +package com.coding.basic.stack.expr; + +import org.junit.Assert; +import org.junit.Test; + +public class InfixExprTest { + @Test + public void testEvaluate() { + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } +} diff --git a/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/TokenParserTest.java b/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..b8bbd6bb5f --- /dev/null +++ b/group03/345943980/mini-jvm-0409/src/test/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class TokenParserTest { + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } +} diff --git a/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..ca21f19849 --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,35 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * 中序表达式转后缀表达式 + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年5月7日 下午4:29:06 + */ +public class InfixToPostfix { + + public static List convert(String expr) { + List inFixTokens = new TokenParser().parse(expr); + List postFixTokens = new ArrayList<>(); + Stack opStack = new Stack(); + for(Token token : inFixTokens){ + if(token.isOperator()){ + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + postFixTokens.add(opStack.pop()); + } + opStack.push(token); + } + if(token.isNumber()){ + postFixTokens.add(token); + } + } + while(!opStack.isEmpty()){ + postFixTokens.add(opStack.pop()); + } + return postFixTokens; + } +} diff --git a/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..f656ee91bc --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,49 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +/** + * 用栈实现后缀表达式 + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年5月7日 下午4:42:26 + */ +public class PostfixExpr { + String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + Stack numStack = new Stack<>(); + for(Token token : tokens){ + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } else{ + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(),f1,f2)); + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..a9541c9006 --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,58 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +/** + * 用两个栈实现前缀表达式 + * + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年5月7日 下午4:46:49 + */ +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + Stack exprStack = new Stack<>(); + Stack numStack = new Stack<>(); + for (Token token : tokens) { + exprStack.push(token); + } + + while (!exprStack.isEmpty()) { + Token t = exprStack.pop(); + if (t.isNumber()) { + numStack.push(new Float(t.getIntValue())); + } else { + Float f1 = numStack.pop(); + Float f2 = numStack.pop(); + numStack.push(calculate(t.toString(), f1, f2)); + + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2) { + if (op.equals("+")) { + return f1 + f2; + } + if (op.equals("-")) { + return f1 - f2; + } + if (op.equals("*")) { + return f1 * f2; + } + if (op.equals("/")) { + return f1 / f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/Token.java b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..72720b0da1 --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Token { + + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } +} diff --git a/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d1b586bce0 --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + public List parse(String expr) { + List tokens = new ArrayList<>(); + int i = 0; + while (i < expr.length()) { + char c = expr.charAt(i); + if (isOperator(c)) { + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + } else if (Character.isDigit(c)) { + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + } else { + System.out.println("char :[" + c + "] is not number or operator,ignore"); + i++; + } + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/InfixToPostfixTest.java new file mode 100644 index 0000000000..b3e2f97837 --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/InfixToPostfixTest.java @@ -0,0 +1,26 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class InfixToPostfixTest { + + @Test + public void testConvert() { + { + List tokens = InfixToPostfix.convert("2+3"); + Assert.assertEquals("[2, 3, +]", tokens.toString()); + } + { + List tokens = InfixToPostfix.convert("2+3*4"); + Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); + } + + { + List tokens = InfixToPostfix.convert("2-3*4+5"); + Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); + } + } +} diff --git a/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/PostfixExprTest.java b/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..3f1729b16e --- /dev/null +++ b/group03/345943980/mini-jvm-0416/src/test/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,26 @@ +package com.coding.basic.stack.expr; + +import org.junit.Assert; +import org.junit.Test; + +public class PostfixExprTest { + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } +} diff --git a/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/CircleQueue.java b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..da6bf9fd58 --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,49 @@ +package com.coding.basic.queue; + +public class CircleQueue { + + // 用数组来保存循环队列的元素 + private Object[] elementData; + int size = 0; + // 队头 + private int front = 0; + // 队尾 + private int rear = 0; + + public CircleQueue(int capacity) { + elementData = new Object[capacity]; + } + + public boolean isEmpty() { + return front == rear; + + } + + public boolean isFull() { + return size == elementData.length; + } + + public int size() { + return size; + } + + public void enQueue(E data) { + if (isFull()) { + throw new RuntimeException("The queue is full"); + } + elementData[rear++] = data; + size++; + } + + @SuppressWarnings("unchecked") + public E deQueue() { + if (isEmpty()) { + throw new RuntimeException("The queue is empty"); + } + E data = (E) elementData[front]; + elementData[front] = null; + front = (front + 1) % elementData.length; + size--; + return data; + } +} diff --git a/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Josephus.java b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..1c0c4bcce0 --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,35 @@ +package com.coding.basic.queue; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author chenming E-mail:cm_20094020@163.com + * @version 创建时间:2017年5月7日 下午10:16:22 + * + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: + * N个人围成一圈(位置记为0到N-1),并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + */ +public class Josephus { + +public static List execute(int n, int m){ + + Queue queue = new Queue(); + for (int i = 0; i < n; i++){ + queue.enQueue(i); + } + List result = new ArrayList(); + int i = 0; + while (!queue.isEmpty()) { + int x = queue.deQueue(); + if (++i % m == 0){ + result.add(x); + } else{ + queue.enQueue(x); + } + } + return result; + } +} diff --git a/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Queue.java b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..5b1191d59e --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,55 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + + private Node first; + private Node last; + private int size; + + private static class Node { + private E item; + private Node next; + } + + public Queue() { + first = null; + last = null; + size = 0; + } + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } else { + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } +} diff --git a/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..8d25563420 --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,37 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; +import java.util.Stack; + +public class QueueWithTwoStacks { + + private Stack stack1; + private Stack stack2; + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + private void moveStack1ToStack2() { + while (!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + } + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + public int size() { + return stack1.size() + stack2.size(); + } + public void enQueue(E item) { + stack1.push(item); + } + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue is empty"); + } + if (stack2.isEmpty()) { + moveStack1ToStack2(); + } + return stack2.pop(); + } +} diff --git a/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/CircleQueueTest.java b/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/CircleQueueTest.java new file mode 100644 index 0000000000..87f976728e --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/CircleQueueTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.queue; + +import org.junit.Assert; +import org.junit.Test; + +public class CircleQueueTest { + + @Test + public void test() { + CircleQueue queue = new CircleQueue(5); + Assert.assertTrue(queue.isEmpty()); + Assert.assertFalse(queue.isFull()); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + Assert.assertTrue(queue.isFull()); + Assert.assertFalse(queue.isEmpty()); + Assert.assertEquals(5, queue.size()); + + Assert.assertEquals("a", queue.deQueue()); + Assert.assertEquals("b", queue.deQueue()); + Assert.assertEquals("c", queue.deQueue()); + Assert.assertEquals("d", queue.deQueue()); + Assert.assertEquals("e", queue.deQueue()); + + } +} diff --git a/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/JosephusTest.java b/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..d2c0456a05 --- /dev/null +++ b/group03/345943980/mini-jvm-0423/src/test/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,12 @@ +package com.coding.basic.queue; + +import org.junit.Assert; +import org.junit.Test; + +public class JosephusTest { + + @Test + public void testExecute() { + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + } +}