Tom Flucke %!s(int64=10) %!d(string=hai) anos
pai
achega
9ff11b6423

+ 6 - 0
Lab5/.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
Lab5/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab5</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
Lab5/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Mon Oct 05 11:08:17 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

+ 81 - 0
Lab5/src/AQueue.java

@@ -0,0 +1,81 @@
+
+public class AQueue <T>
+{
+	private T[] arr;
+	private int front;
+	private int end;
+	private int count;
+	
+	public static class MyException extends RuntimeException
+	{
+		private static final long serialVersionUID = -244806549310243756L;
+		public MyException()
+		{
+			super();
+		}
+		public MyException(String message)
+		{
+			super(message);
+		}
+	}
+	
+	public AQueue(int arrSize)
+	{
+		arr = (T[]) new Object[arrSize];
+		front = 0;
+		end = -1;
+		count = 0;
+	}
+	
+	public boolean isEmpty()
+	{
+		return (count < 1);
+	}
+	
+	public void enqueue(T item)
+	{
+		if(count == arr.length)
+		{
+			T[] tmpArr = (T[]) new Object[arr.length * 2];
+			for (int i = 0; i < count; i++)
+			{
+				tmpArr[i] = arr[(front + i) % arr.length];
+				
+			}
+			arr = tmpArr;
+			front = 0;
+			end = count - 1;
+		}
+		end++;
+		arr[end] = item;
+		count ++;
+	}
+	
+	public T dequeue()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		T tmp = arr[front];
+		arr[front] = null;
+		front = (front + 1) % arr.length;
+		return tmp;
+	}
+	
+	public void printArray()
+	{
+		for (int i = 0; i < arr.length; i++)
+		{
+			System.out.print(arr[i] + " ");
+		}
+		
+		System.out.println();
+	}
+	
+	
+	
+	
+	
+	
+}

+ 18 - 0
Lab5/src/AQueueClient.java

@@ -0,0 +1,18 @@
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
+	
+public class AQueueClient 
+{
+	Scanner input = new Scanner(System.in);
+	new floatArr = AQueue
+	
+	while(input.hasNext())
+	{
+		if(input.hasNextFloat())
+		{
+			
+		}
+	}
+}
+		

+ 77 - 0
Lab5/src/AQueueDriver.java

@@ -0,0 +1,77 @@
+import java.util.Scanner;
+
+public class AQueueDriver {
+	public static void main(String[] args) {
+		Scanner in = new Scanner(System.in);
+		AQueue<Integer> queue = new AQueue<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();
+						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 'p':
+					queue.printArray();
+					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("\"p\" -\tPrint");
+		System.out.println("\"q\" -\tQuit");
+	}
+
+}