aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m2/opg2/src
diff options
context:
space:
mode:
Diffstat (limited to 'sem5/oop/m2/opg2/src')
-rw-r--r--sem5/oop/m2/opg2/src/Lines.java71
-rw-r--r--sem5/oop/m2/opg2/src/LoadException.java16
-rw-r--r--sem5/oop/m2/opg2/src/Main.java23
3 files changed, 110 insertions, 0 deletions
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);
+ }
+
+}