diff options
author | Julian T <julian@jtle.dk> | 2020-09-08 13:43:21 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-09-08 13:43:21 +0200 |
commit | c009693a07276f8d66cd68637431f0a3f0e9ef15 (patch) | |
tree | fc9bc85620813e7a179b043203d25879932267f6 | |
parent | ba5f68924664b081aab1abb62c68e2fb70bdff0b (diff) |
Added more java assignments
-rw-r--r-- | sem5/oop/m1/src/Main.java | 2 | ||||
-rw-r--r-- | sem5/oop/m2/Opg1/.classpath | 10 | ||||
-rw-r--r-- | sem5/oop/m2/Opg1/.gitignore | 1 | ||||
-rw-r--r-- | sem5/oop/m2/Opg1/.project | 17 | ||||
-rw-r--r-- | sem5/oop/m2/Opg1/hej.txt | 10 | ||||
-rw-r--r-- | sem5/oop/m2/Opg1/src/Main.java | 38 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/.classpath | 6 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/.gitignore | 1 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/.project | 17 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/hej.txt | 10 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/src/Lines.java | 71 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/src/LoadException.java | 16 | ||||
-rw-r--r-- | sem5/oop/m2/opg2/src/Main.java | 23 | ||||
-rw-r--r-- | sem5/oop/m2/primes/.classpath | 6 | ||||
-rw-r--r-- | sem5/oop/m2/primes/.gitignore | 1 | ||||
-rw-r--r-- | sem5/oop/m2/primes/.project | 17 | ||||
-rw-r--r-- | sem5/oop/m2/primes/src/Main.java | 32 | ||||
-rw-r--r-- | sem5/oop/m2/primes/src/PrimeGen.java | 40 |
18 files changed, 317 insertions, 1 deletions
diff --git a/sem5/oop/m1/src/Main.java b/sem5/oop/m1/src/Main.java index b7c1f42..3906cc6 100644 --- a/sem5/oop/m1/src/Main.java +++ b/sem5/oop/m1/src/Main.java @@ -4,7 +4,7 @@ public class Main { Student std = new Student("Julian", (short)21, "Male", (long)123213213, "COMTEK"); Uniperson hej = std; - hej.addGrade(0.32); + hej.addGrade(7); hej.addGrade(12); System.out.printf("deg: %s, sem: %d, avg: %f\n", hej.getDeg(), hej.getSem(), hej.getGPA()); diff --git a/sem5/oop/m2/Opg1/.classpath b/sem5/oop/m2/Opg1/.classpath new file mode 100644 index 0000000..adeb0a3 --- /dev/null +++ b/sem5/oop/m2/Opg1/.classpath @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> + <attributes> + <attribute name="module" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/sem5/oop/m2/Opg1/.gitignore b/sem5/oop/m2/Opg1/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/sem5/oop/m2/Opg1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/sem5/oop/m2/Opg1/.project b/sem5/oop/m2/Opg1/.project new file mode 100644 index 0000000..1b0d3fd --- /dev/null +++ b/sem5/oop/m2/Opg1/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>Opg1</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> diff --git a/sem5/oop/m2/Opg1/hej.txt b/sem5/oop/m2/Opg1/hej.txt new file mode 100644 index 0000000..d8c6033 --- /dev/null +++ b/sem5/oop/m2/Opg1/hej.txt @@ -0,0 +1,10 @@ +1 2 3 4 5 6 7 8 9 10 +11 12 13 14 15 16 17 18 19 20 +21 22 23 24 25 26 27 28 29 30 +31 32 33 34 35 36 37 38 39 40 +41 42 43 44 45 46 47 48 49 50 +51 52 53 54 55 56 57 58 59 60 +61 62 63 64 65 66 67 68 69 70 +71 72 73 74 75 76 77 78 79 80 +81 82 83 84 85 86 87 88 89 90 +91 92 93 94 95 96 97 98 99 100 diff --git a/sem5/oop/m2/Opg1/src/Main.java b/sem5/oop/m2/Opg1/src/Main.java new file mode 100644 index 0000000..179e678 --- /dev/null +++ b/sem5/oop/m2/Opg1/src/Main.java @@ -0,0 +1,38 @@ +import java.io.FileWriter; +import java.io.IOException; + +public class Main { + + static void writeLines(String name) throws IOException{ + FileWriter f = null; + try { + f = new FileWriter(name); + + for (int i = 1; i <= 100; i++) { + f.write(String.format("%-3d ", i)); + if (i % 10 == 0) { + f.write(System.lineSeparator()); + } + } + } finally { + if (f != null) { + f.close(); + } + } + } + + public static void main(String[] args) { + String name = "hej.txt"; + if (args.length > 0) { + name = args[0]; + } + + System.out.printf("Writing to %s%n", name); + try { + writeLines(name); + } catch(IOException e) { + System.err.printf("Failed with: %s%n", e); + } + } + +} diff --git a/sem5/oop/m2/opg2/.classpath b/sem5/oop/m2/opg2/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/sem5/oop/m2/opg2/.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"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/sem5/oop/m2/opg2/.gitignore b/sem5/oop/m2/opg2/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/sem5/oop/m2/opg2/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/sem5/oop/m2/opg2/.project b/sem5/oop/m2/opg2/.project new file mode 100644 index 0000000..291ee0d --- /dev/null +++ b/sem5/oop/m2/opg2/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>opg2</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> diff --git a/sem5/oop/m2/opg2/hej.txt b/sem5/oop/m2/opg2/hej.txt new file mode 100644 index 0000000..d8c6033 --- /dev/null +++ b/sem5/oop/m2/opg2/hej.txt @@ -0,0 +1,10 @@ +1 2 3 4 5 6 7 8 9 10 +11 12 13 14 15 16 17 18 19 20 +21 22 23 24 25 26 27 28 29 30 +31 32 33 34 35 36 37 38 39 40 +41 42 43 44 45 46 47 48 49 50 +51 52 53 54 55 56 57 58 59 60 +61 62 63 64 65 66 67 68 69 70 +71 72 73 74 75 76 77 78 79 80 +81 82 83 84 85 86 87 88 89 90 +91 92 93 94 95 96 97 98 99 100 diff --git a/sem5/oop/m2/opg2/src/Lines.java b/sem5/oop/m2/opg2/src/Lines.java new file mode 100644 index 0000000..ed1f420 --- /dev/null +++ b/sem5/oop/m2/opg2/src/Lines.java @@ -0,0 +1,71 @@ +import java.util.ArrayList; +import java.io.IOException; +import java.io.EOFException; +import java.io.FileReader; +import java.io.BufferedReader; +import java.util.Scanner; +import java.io.PrintStream; + +public class Lines { + + private ArrayList<int[]> lines; + + public Lines(String loadpath) throws IOException, LoadException { + this.lines = new ArrayList<int[]>(); + FileReader f = null; + BufferedReader buff = null; + try { + f = new FileReader(loadpath); + buff = new BufferedReader(f); + + String line; + while((line = buff.readLine()) != null) { + this.readRow(line); + } + + } finally { + if (f != null) { + f.close(); + } + } + } + + void readRow(String line) throws LoadException { + // you are a pirate + int[] arr = new int[10]; + + + int i; + Scanner sc = null; + try { + sc = new Scanner(line); + for (i = 0; i < 10; i++) { + if (!sc.hasNextInt()) { + break; + } + int n = sc.nextInt(); + // OPG 3 + if (n > 90) { + throw new LoadException(n, "is above 90"); + } + // OPG 4 + //a assert (n <= 90) : "is above 90"; + arr[i] = n; + } + } finally { + sc.close(); + } + if (i > 0) { + lines.add(arr); + } + } + + public void print(PrintStream out) { + for (int[] arr : this.lines) { + for (int i : arr) { + out.printf("%-3d ", i); + } + out.print(System.lineSeparator()); + } + } +} diff --git a/sem5/oop/m2/opg2/src/LoadException.java b/sem5/oop/m2/opg2/src/LoadException.java new file mode 100644 index 0000000..b6ea720 --- /dev/null +++ b/sem5/oop/m2/opg2/src/LoadException.java @@ -0,0 +1,16 @@ + +public class LoadException extends Exception { + + private int value; + private String msg; + + public LoadException(int value, String msg) { + this.value = value; + this.msg = msg; + } + + public String getMessage() { + return String.format("Invalid value %d (%s)", this.value, this.msg); + } + +} diff --git a/sem5/oop/m2/opg2/src/Main.java b/sem5/oop/m2/opg2/src/Main.java new file mode 100644 index 0000000..532bc72 --- /dev/null +++ b/sem5/oop/m2/opg2/src/Main.java @@ -0,0 +1,23 @@ + +public class Main { + + public static void main(String[] args) { + System.out.println("Get ready for cool stuff."); + + String fname = "hej.txt"; + if (args.length > 0) { + fname = args[0]; + } + + Lines l = null; + try { + l = new Lines(fname); + } catch (Exception e) { + System.err.printf("error: %s%n", e); + System.exit(1); + } + + l.print(System.out); + } + +} diff --git a/sem5/oop/m2/primes/.classpath b/sem5/oop/m2/primes/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/sem5/oop/m2/primes/.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"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/sem5/oop/m2/primes/.gitignore b/sem5/oop/m2/primes/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/sem5/oop/m2/primes/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/sem5/oop/m2/primes/.project b/sem5/oop/m2/primes/.project new file mode 100644 index 0000000..b2abcc1 --- /dev/null +++ b/sem5/oop/m2/primes/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>primes</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> diff --git a/sem5/oop/m2/primes/src/Main.java b/sem5/oop/m2/primes/src/Main.java new file mode 100644 index 0000000..e191835 --- /dev/null +++ b/sem5/oop/m2/primes/src/Main.java @@ -0,0 +1,32 @@ + +public class Main { + + public static void main(String[] args) { + System.out.println("Lets find some primes!!"); + + int runto = 10000; + if (args.length > 0) { + runto = Integer.parseInt(args[0]); + } + + // Calculate length of runto as string + int numlen = String.valueOf(runto).length(); + + PrimeGen pg = new PrimeGen(); + + int p; + int index = 1; + while ((p = pg.next(runto)) != -1) { + // Terrible two level format + System.out.printf(String.format("%%-%dd ", numlen), p); + if (index % 10 == 0) { + System.out.print(System.lineSeparator()); + } + index++; + } + System.out.print(System.lineSeparator()); + System.out.printf("Found %d primes%n", index-1); + + } + +} diff --git a/sem5/oop/m2/primes/src/PrimeGen.java b/sem5/oop/m2/primes/src/PrimeGen.java new file mode 100644 index 0000000..9c91645 --- /dev/null +++ b/sem5/oop/m2/primes/src/PrimeGen.java @@ -0,0 +1,40 @@ +import java.io.PrintStream; +import java.util.ArrayList; + +public class PrimeGen { + + private ArrayList<Integer> primes; + private int at; + + public PrimeGen() { + this.primes = new ArrayList<>(); + + this.at = 1; + } + + public int next(int limit) { + // Run until a prime + primeloop: + while (true) { + this.at++; + if (this.at >= limit) { + return -1; + } + + for (int prime : this.primes) { + if (this.at % prime == 0) { + continue primeloop; + } + } + break; + } + + this.primes.add(at); + return at; + } + + public int next() { + return next(-1); + } + +} |