/* Lösningsförslag till omtentamen 161220. */ // --------- 1 -------------- Se föreläsningsanteckningar eller bok. // --------- 2 -------------- int i = a(5); // 1 double d = a(5.0); // 2 Bad parameter type. d = a(5); // 3 d = c(1, 2.0, 3); // 4 Wrong number of params. d = a(1) + c(2, 3); // 5 int a(int n) { // 6 return n + 1; } double b(double n) { // 7 return n + 1; } double c(int n, double d) { // 8 return n + d; } // ----- 3 ---------- void plotCollatz(int n) { while (n > 1) { out.print(n + ", "); if (n % 2 == 0) { n = n / 2; } else { n = 3 * n + 1; } } out.print(n); } // --------- 4 ------------------- // Se länk till bild. // Before: // C[] ca1 = {new C(1), new C(2), new C(3)}; // C[] ca2 = {new C(3), new C(2), new C(5)}; void process(C[] cArr1, C[] cArr2) { for (int i = 0; i < cArr1.length; i++) { if (cArr1[i].n == cArr2[i].n) { cArr1[i] = cArr2[i]; } else { cArr1[i].n = cArr2[i].n; } } } class C { int n; C(int n) { this.n = n; } } // --------- 5 ------------------- int[] intersection(int[] a1, int[] a2) { int[] tmp; int len = a1.length > a2.length ? a1.length : a2.length; tmp = new int[len]; int k = 0; for (int i = 0; i < a1.length; i++) { for (int j = 0; j < a2.length; j++) { if (a1[i] == a2[j] && !contains(tmp, a2[j])) { tmp[k] = a1[i]; k++; } } } int[] result = new int[k]; for (int i = 0; i < result.length; i++) { result[i] = tmp[i]; } return result; } boolean contains( int[] arr, int n){ for( int i: arr){ if( i == n ){ return true; } } return false; } // -------- 6 --------- double[] d1 = {1.0, 1.0, 2.0, 2.0, 3.0, 3.0}; double[] d2 = {4.0, 4.0, 5.0, 5.0}; PolyLine p3 = new PolyLine(d1).concat(new PolyLine(d2)); public class PolyLine { private final double[] pts; public PolyLine(double[] pts) { this.pts = pts; } public void translate(double dx, double dy) { for (int i = 0; i < pts.length - 1; i += 2) { pts[i] += dx; pts[i + 1] += dy; } } public PolyLine concat(PolyLine other) { double[] tmp = new double[pts.length + other.pts.length]; int i = 0; while (i < pts.length) { tmp[i] = pts[i]; i++; } int j = 0; while (i < tmp.length) { tmp[i] = other.pts[j]; i++; j++; } return new PolyLine(tmp); } } // -------- 7 --------- public int sumFromString(String text) { StringBuilder sb = new StringBuilder(); int sum = 0; for (char ch : text.toCharArray()) { if (Character.isDigit(ch)) { sb.append(ch); } else { if (sb.length() > 0) { sum = sum + Integer.valueOf(sb.toString()); sb.setLength(0); // Alt. sb = new StringBuilder() } } } if (sb.length() > 0) { sum = sum + Integer.valueOf(sb.toString()); } return sum; } // --------- 8 ------------------ Se föreläsningsanteckningar eller bok. // ---------- 9 ---------------- Solution to a) test(5,2): 1 111 1 test(5,3): 1 111 11111 111 1 Solution to c) 1 212 11511 212 1 void setMark(int[][] a, int x, int y) { a[x][y] = 1; // a[x][y] = 1 + a[x][y]; // for part c) } void mark(int[][] a, int x, int y, int depth) { for (int i = 0; i < depth; i++) { for (int j = 0; j < depth; j++) { if (i + j < depth) { setMark(a, x + i, y + j); setMark(a, x - i, y + j); setMark(a, x + i, y - j); setMark(a, x - i, y - j); } } } }