Explorar o código

commit labs 14 to 16

Lara Luu %!s(int64=10) %!d(string=hai) anos
pai
achega
75faacceb1

+ 6 - 0
Lab14/.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.7"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab14/.project

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

@@ -0,0 +1,12 @@
+#Mon Nov 09 11:07:40 PST 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

+ 42 - 0
Lab14/src/Algorithms.java

@@ -0,0 +1,42 @@
+
+public class Algorithms {
+
+	public static void linearAlgorithm(long N)
+	{
+		for(long x = 1; x <= N; x++)
+		{
+			long i = 1;
+		}
+	}
+	
+	public static void quadraticAlgorithm(long N)
+	{
+		for(long i = 1; i <= N; i++)
+		{
+			for(long j = 1; j <= N; j++)
+			{
+				long x = 1;
+			}
+		}
+	}
+	
+	public static void logarithmicAlgorithm(long N)
+	{
+		for (long i = N; i >= 1; i/=2)
+		{
+			long x = 1;
+		}
+	}
+	
+	public static void NlogNAlgorithm(long N)
+	{
+		for (long i = 1; i <= N; i++)
+		{
+			for(long j = N; j >= 1; j/=2)
+			{
+				long x = 1;
+			}
+		}
+	}
+	
+}

+ 61 - 0
Lab14/src/RunTimes.java

@@ -0,0 +1,61 @@
+
+public class RunTimes {
+
+	
+	public static void main(String args[])
+	{
+		long systemTime;
+		System.out.println("Logarithmic algorithm\'s running times:");
+		
+		for(long N = 10000; N < 100000000; N *= 2)
+		{
+			systemTime = System.nanoTime()/100000;
+			Algorithms.logarithmicAlgorithm(N);
+			long systemUpdate = System.nanoTime()/100000;
+			long runningTime = systemUpdate - systemTime;
+			
+			System.out.println("T(" + N + ") = " + runningTime);
+		}
+		System.out.println();
+		
+		System.out.println("Linear algorithm\'s running times:");
+		
+		for(long N = 10000; N < 100000000; N *= 2)
+		{
+			systemTime = System.nanoTime()/100000;
+			Algorithms.linearAlgorithm(N);
+			long systemUpdate = System.nanoTime()/100000;
+			long runningTime = systemUpdate - systemTime;
+			
+			System.out.println("T(" + N + ") = " + runningTime);
+		}
+		System.out.println();
+		
+		System.out.println("NlogN algorithm\'s running times:");
+		
+		for(long N = 10000; N < 100000000; N *= 2)
+		{
+			systemTime = System.nanoTime()/100000;
+			Algorithms.NlogNAlgorithm(N);
+			long systemUpdate = System.nanoTime()/100000;
+			long runningTime = systemUpdate - systemTime;
+			
+			System.out.println("T(" + N + ") = " + runningTime);
+		}
+		System.out.println();
+		
+		System.out.println("Quadratic algorithm\'s running times:");
+		
+		for(long N = 10000; N < 800000; N *= 2)
+		{
+			systemTime = System.nanoTime()/100000;
+			Algorithms.quadraticAlgorithm(N);
+			long systemUpdate = System.nanoTime()/100000;
+			long runningTime = systemUpdate - systemTime;
+			
+			System.out.println("T(" + N + ") = " + runningTime);
+		}
+	
+	}
+	
+}

+ 6 - 0
Lab15/.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.7"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab15/.project

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

@@ -0,0 +1,12 @@
+#Mon Nov 16 11:04:28 PST 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

+ 43 - 0
Lab15/src/ArrayMaxFinder.java

@@ -0,0 +1,43 @@
+import java.util.Scanner;
+
+
+public class ArrayMaxFinder 
+{
+	public static int arrayMax(int[] arr, int first)
+	{
+		if(first == arr.length - 1)
+		{
+			return first;
+		}
+		
+		else 
+		{
+			int index =  arrayMax(arr, first + 1);
+			if(arr[first] > arr[index])
+			{
+				return first;
+			}
+			else
+			{
+				return index;
+			}
+		}
+	}
+	
+	public static void main(String[] args)
+	{
+		Scanner in = new Scanner(System.in);
+				
+		System.out.println("Please enter 10 integers! (:");
+		int[] arr = new int[10];
+		int n = 0;
+		while(in.hasNextInt())
+		{
+			arr[n] = in.nextInt();
+			n++;
+		}
+		
+		System.out.println("The maximum value is:" + arr[arrayMax(arr, 0)]);
+	}
+
+}

+ 35 - 0
Lab15/src/ArraySummer.java

@@ -0,0 +1,35 @@
+import java.util.Scanner;
+
+
+public class ArraySummer 
+{
+	
+	public static int arraySum(int[] arr, int first)
+	{
+		if(first > arr.length - 1)
+		{
+			return 0;
+		}
+		return(arr[first] + arraySum(arr, ++first));
+	}
+
+	public static void main(String[] args)
+	{
+		Scanner in = new Scanner(System.in);
+				
+		System.out.println("Please enter 10 integers! (:");
+		int[] arr = new int[10];
+		int n = 0;
+		while(in.hasNextInt())
+		{
+			arr[n] = in.nextInt();
+			n++;
+		}
+		
+		System.out.println("The sum of the integers is:" + arraySum(arr, 0));
+		
+		
+	}
+	
+}
+

+ 38 - 0
Lab15/src/GCDCalculator.java

@@ -0,0 +1,38 @@
+import java.util.Scanner;
+
+
+public class GCDCalculator 
+{
+	public static int gcd(int x, int y)
+	{
+		if(x == y)
+		{
+			return x;
+		}
+		
+		if(x > y)
+		{
+			return (gcd(x - y, y));
+		}
+		else
+		{
+			return(gcd(x, y-x));
+		}
+	}
+	
+	public static void main(String[] args)
+	{
+		Scanner in = new Scanner(System.in);
+		String running = "y";
+		do
+		{
+			System.out.println("Please enter two inputs!");
+			System.out.println("The gcd is:" + gcd(in.nextInt(), in.nextInt()));
+			in.nextLine();
+			System.out.println("Would you like to continue?(y/n)");
+			running = in.next();
+			in.nextLine();
+		}
+		while (running.equals("y"));
+	}
+}

+ 6 - 0
Lab16/.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.7"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab16/.project

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

@@ -0,0 +1,12 @@
+#Wed Nov 18 11:08:27 PST 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

+ 122 - 0
Lab16/src/BasicAVL.java

@@ -0,0 +1,122 @@
+public class BasicAVL 
+{
+	private class AVLNode
+	{
+		int element;
+		int height;
+		
+		AVLNode leftChild;
+		AVLNode rightChild;
+	
+		public AVLNode(int element)
+		{
+			this.element = element;
+			this.height = 0;
+		}
+	}
+	
+	private AVLNode root;
+	
+	public BasicAVL()
+	{
+		root = null;
+	}
+	
+	public void insert(int item)
+	{
+		root = insert(root, item);
+	}
+	
+	private AVLNode insert(AVLNode root, int item)
+	{
+		if(root == null)
+		{
+			return new AVLNode(item);
+		}
+		else if(item < root.element)
+		{
+			root.leftChild = insert(root.leftChild, item);
+			if(root.leftChild.height >= height(root.rightChild) + 2)
+			{
+				if(item < root.leftChild.element)
+				{
+					root = rotateWithLeft(root);
+				}
+				else
+				{
+					root = doubleRightLeft(root);
+				}
+			}
+		
+		}
+		else
+		{
+			root.rightChild = insert(root.rightChild, item);
+			if(root.rightChild.height >= height(root.leftChild) + 2)
+			{
+				if(item < root.rightChild.element)
+				{
+					root = doubleLeftRight(root);
+				}
+				else
+				{
+					root = rotateWithRight(root);
+				}
+			}
+		}
+		root.height = Math.max(height(root.leftChild), height(root.rightChild)) + 1;
+		return root;
+	}
+	
+	private AVLNode rotateWithRight(AVLNode oldRoot)
+	{
+		AVLNode newRoot = oldRoot.rightChild;
+		oldRoot.rightChild = newRoot.leftChild;
+		newRoot.leftChild = oldRoot;
+		oldRoot.height = 1 + Math.max(height(oldRoot.leftChild), height(oldRoot.rightChild));
+		newRoot.height = 1 + Math.max(height(newRoot.rightChild), newRoot.leftChild.height);
+		return newRoot;
+	}
+	
+	private AVLNode rotateWithLeft(AVLNode oldRoot)
+	{
+		AVLNode newRoot = oldRoot.leftChild;
+		oldRoot.leftChild = newRoot.rightChild;
+		newRoot.rightChild = oldRoot;
+		oldRoot.height = 1 + Math.max(height(oldRoot.leftChild), height(oldRoot.rightChild));
+		newRoot.height = 1 + Math.max(height(newRoot.leftChild), newRoot.rightChild.height);
+		return newRoot;
+	}
+	
+	private AVLNode doubleLeftRight(AVLNode oldRoot)
+	{
+		oldRoot.leftChild = rotateWithRight(oldRoot.leftChild);
+		return rotateWithLeft(oldRoot);
+	}
+	
+	private AVLNode doubleRightLeft(AVLNode oldRoot)
+	{
+		oldRoot.rightChild = rotateWithLeft(oldRoot.rightChild);
+		return rotateWithRight(oldRoot);
+	}
+	private int height(AVLNode node)
+	{
+		return node == null? -1:node.height;
+	}
+	
+	public void print()
+	{
+		printHelper(root, "");
+	}
+	
+	private void printHelper(AVLNode chkNode, String indent)
+	{
+		if (chkNode != null)
+		{
+			System.out.println(indent + chkNode.element);
+			indent += "    ";
+			printHelper(chkNode.leftChild, indent);
+			printHelper(chkNode.rightChild, indent);
+		}
+	}
+}