浏览代码

Merge branch 'master' of https://github.com/tflucke/CPE103
merge conflicting files
Conflicts:
Lab1/bin/ListWork.class

Lara Luu 10 年之前
父节点
当前提交
13b4ae237a

+ 0 - 2
.gitignore

@@ -1,2 +0,0 @@
-.metadata
-.recommenders

二进制
Lab1/bin/ListWork.class


二进制
Lab2/bin/Separator.class


+ 6 - 0
Lab3/.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab3/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab3</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 12 - 0
Lab3/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Mon Sep 28 11:05:08 PDT 2015
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

二进制
Lab3/bin/LQueue$MyException.class


二进制
Lab3/bin/LQueue$Node.class


二进制
Lab3/bin/LQueue.class


二进制
Lab3/bin/LQueueDriver.class


+ 70 - 0
Lab3/src/LQueue.java

@@ -0,0 +1,70 @@
+
+public class LQueue<T>
+{
+	public static class MyException extends RuntimeException
+	{
+		private static final long serialVersionUID = -244806549310243756L;
+		public MyException()
+		{
+			super();
+		}
+		public MyException(String message)
+		{
+			super(message);
+		}
+	}
+	
+	private Node front, end;
+	
+	public LQueue()
+	{
+		front = end = null;
+	}
+	
+	public void enqueue(T item)
+	{
+		Node newEnd = new Node(item);
+		if (isEmpty())
+		{
+			front = newEnd;
+		}
+		else
+		{
+			end.next = newEnd;	
+		}
+		end = newEnd;
+	}
+	
+	public T dequeue()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		try
+		{
+			return front.item;
+		}
+		finally
+		{
+			front = front.next;
+		}
+	}
+	
+	public boolean isEmpty()
+	{
+		return front == null;
+	}
+	
+	private class Node
+	{
+		public T item;
+		public Node next;
+		
+		public Node(T item)
+		{
+			this.item = item;
+			this.next = null;
+		}
+	}
+}

+ 73 - 0
Lab3/src/LQueueDriver.java

@@ -0,0 +1,73 @@
+import java.util.Scanner;
+
+public class LQueueDriver {
+	public static void main(String[] args) {
+		Scanner in = new Scanner(System.in);
+		LQueue<Integer> queue = new LQueue<Integer>();
+		printHelp();
+		boolean running = true;
+		while (running)
+		{
+			String line = in.nextLine();
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid Option.");
+				//printHelp();
+				continue;
+			}
+			char command = line.charAt(0);
+			switch (command)
+			{
+				case 'a':
+					System.out.println("Enter an integer.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						queue.enqueue(newVal);
+						System.out.println(newVal+" enqueued");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'd':
+					try
+					{
+						System.out.println(queue.dequeue() +" dequeued");
+					}
+					catch (LQueue.MyException me)
+					{
+						System.out.println("Invalid operation.  Queue is empty.");
+					}
+					break;
+				case 'e':
+					System.out.println(queue.isEmpty()? "Empty":"Not Empty");
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
+		while (!queue.isEmpty())
+		{
+			System.out.print(queue.dequeue()+" ");
+		}
+		System.out.println();
+	}
+	
+	public static void printHelp()
+	{
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd to Queue");
+		System.out.println("\"d\" -\tRemove from Queue");
+		System.out.println("\"e\" -\tCheck if Queue is empty");
+		System.out.println("\"q\" -\tQuit");
+	}
+
+}

+ 6 - 0
Lab4/.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab4/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab4</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 12 - 0
Lab4/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Wed Sep 30 11:27:02 PDT 2015
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

+ 61 - 0
Lab4/src/AStack.java

@@ -0,0 +1,61 @@
+public class AStack<T> {
+	public static class MyException extends RuntimeException
+	{
+		private static final long serialVersionUID = -244806549310243756L;
+		public MyException()
+		{
+			super();
+		}
+		public MyException(String message)
+		{
+			super(message);
+		}
+	}
+	
+	private T[] arr;
+	private int top;
+
+	@SuppressWarnings("unchecked")
+	public AStack(int size)
+	{
+		arr = (T[]) new Object[size];
+		top = -1;
+	}
+
+	@SuppressWarnings("unchecked")
+	public void push(T item)
+	{
+		if (top+1 == arr.length)
+		{
+			T[] newArr = (T[]) new Object[2*arr.length];
+			for (int i = 0; i <= top; i++)
+			{
+				newArr[i] = arr[i];
+			}
+			arr = newArr;
+		}
+		arr[++top] = item;
+	}
+
+	public T pop()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		return arr[top--];
+	}
+	public T peek()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		return arr[top];
+	}
+	
+	public boolean isEmpty()
+	{
+		return top < 0;
+	}
+}

+ 52 - 0
Lab4/src/AStackClient.java

@@ -0,0 +1,52 @@
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
+	
+public class AStackClient {
+		public static void main(String[] args) throws FileNotFoundException {
+			Scanner console = new Scanner(System.in);
+			System.out.println("Enter file name:");
+			String fileName = console.nextLine();
+			console.close();
+			Scanner in = new Scanner(new FileInputStream(fileName));
+			in.useDelimiter("\\s+");
+			final int n = 5;
+			AStack<Integer> intStack = new AStack<Integer>(n);
+			AStack<Float> fltStack = new AStack<Float>(n);
+			AStack<String> strStack = new AStack<String>(n);
+			while (in.hasNext())
+			{
+				if (in.hasNextFloat())
+				{
+					fltStack.push(in.nextFloat());
+				}
+				else if (in.hasNextInt())
+				{
+					intStack.push(in.nextInt());
+				}
+				else
+				{
+					strStack.push(in.next());
+				}
+			}
+			in.close();
+			System.out.println("Abort mission");
+			
+			System.out.print("Strings:");
+			while (!strStack.isEmpty())
+			{
+				System.out.print(" " + strStack.pop());
+			}
+			System.out.print("\nFloats:");
+			while (!fltStack.isEmpty())
+			{
+				System.out.print(" " + fltStack.pop());
+			}
+			System.out.print("\nIntegers:");
+			while (!intStack.isEmpty())
+			{
+				System.out.print(" " + intStack.pop());
+			}
+			System.out.println();
+		}
+}

+ 84 - 0
Lab4/src/AStackDriver.java

@@ -0,0 +1,84 @@
+import java.util.Scanner;
+
+public class AStackDriver {
+	public static void main(String[] args) {
+		Scanner in = new Scanner(System.in);
+		AStack<Integer> stack = new AStack<Integer>(5);
+		printHelp();
+		boolean running = true;
+		while (running)
+		{
+			String line = in.nextLine();
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid Option.");
+				//printHelp();
+				continue;
+			}
+			char command = line.charAt(0);
+			switch (command)
+			{
+				case 'a':
+					System.out.println("Enter an integer.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						stack.push(newVal);
+						System.out.println(newVal+" Pushed");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'd':
+					try
+					{
+						System.out.println(stack.pop() +" Poped");
+					}
+					catch (AStack.MyException me)
+					{
+						System.out.println("Invalid operation.  Stack is empty.");
+					}
+					break;
+				case 'p':
+					try
+					{
+						System.out.println(stack.peek() +" Peeked");
+					}
+					catch (AStack.MyException me)
+					{
+						System.out.println("Invalid operation.  Stack is empty.");
+					}
+					break;
+				case 'e':
+					System.out.println(stack.isEmpty()? "Empty":"Not Empty");
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
+		while (!stack.isEmpty())
+		{
+			System.out.print(stack.pop()+" ");
+		}
+		System.out.println();
+	}
+	
+	public static void printHelp()
+	{
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tPush to Stack");
+		System.out.println("\"d\" -\tPop from Stack");
+		System.out.println("\"p\" -\tPeek");
+		System.out.println("\"e\" -\tCheck if Stack is empty");
+		System.out.println("\"q\" -\tQuit");
+	}
+
+}