about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/4
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.2/labwork/4')
-rw-r--r--usth/ICT2.2/labwork/4/4_labwork4.pdfbin0 -> 1305629 bytes
-rw-r--r--usth/ICT2.2/labwork/4/BubbleSort.java21
-rw-r--r--usth/ICT2.2/labwork/4/Centroid.java26
-rw-r--r--usth/ICT2.2/labwork/4/Closest.java45
-rw-r--r--usth/ICT2.2/labwork/4/Deal.java27
-rw-r--r--usth/ICT2.2/labwork/4/DiscreteDistro.java23
-rw-r--r--usth/ICT2.2/labwork/4/Employ.java30
-rw-r--r--usth/ICT2.2/labwork/4/Employee.classbin0 -> 1052 bytes
-rw-r--r--usth/ICT2.2/labwork/4/Employee.java27
-rw-r--r--usth/ICT2.2/labwork/4/HowMany.java7
-rw-r--r--usth/ICT2.2/labwork/4/LongestRun.java30
-rw-r--r--usth/ICT2.2/labwork/4/MaxMin.java18
-rw-r--r--usth/ICT2.2/labwork/4/Stats.java19
-rw-r--r--usth/ICT2.2/labwork/4/Transpose.java24
-rw-r--r--usth/ICT2.2/labwork/4/WordCount.java16
-rw-r--r--usth/ICT2.2/labwork/4/employees.txt8
16 files changed, 321 insertions, 0 deletions
diff --git a/usth/ICT2.2/labwork/4/4_labwork4.pdf b/usth/ICT2.2/labwork/4/4_labwork4.pdf
new file mode 100644
index 0000000..4a4de56
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/4_labwork4.pdf
Binary files differdiff --git a/usth/ICT2.2/labwork/4/BubbleSort.java b/usth/ICT2.2/labwork/4/BubbleSort.java
new file mode 100644
index 0000000..8557c64
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/BubbleSort.java
@@ -0,0 +1,21 @@
+import java.util.ArrayList;
+import java.util.Scanner;
+
+import static java.util.Collections.swap;
+
+class Stats
+{
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    int n = scanner.nextInt();
+    var numbers = new ArrayList<Double>();
+    for (int i = 0; i < n; ++i)
+      numbers.add(scanner.nextDouble());
+    for (int m = 0; n > 1; n = m, m = 0)
+      for (int i = 1; i < n; ++i)
+        if (numbers.get(i).compareTo(numbers.get(i - 1)) < 0)
+          swap(numbers, m = i, i - 1);
+    System.out.println(numbers);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Centroid.java b/usth/ICT2.2/labwork/4/Centroid.java
new file mode 100644
index 0000000..7862cbb
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Centroid.java
@@ -0,0 +1,26 @@
+import java.util.Scanner;
+
+class Centriod
+{
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    double m = 0.0, x = 0.0, y = 0.0;
+
+    while (scanner.hasNextDouble())
+      {
+        double n = scanner.nextDouble();
+        if (!scanner.hasNextDouble())
+          break;
+        double s = scanner.nextDouble();
+        if (!scanner.hasNextDouble())
+          break;
+        double i = scanner.nextDouble();
+
+        m += n;
+        x += n * s;
+        y += n * i;
+      }
+    System.out.println(m + " " + x/m + " " + y/m);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Closest.java b/usth/ICT2.2/labwork/4/Closest.java
new file mode 100644
index 0000000..2f1e45d
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Closest.java
@@ -0,0 +1,45 @@
+import java.util.Scanner;
+
+class Closest
+{
+  private static double x, y, z;
+
+  private static double dist(double a, double b, double c)
+  {
+    double g = x - a;
+    double h = y - b;
+    double i = z - c;
+    return g*g + h*h + i*i;
+  }
+
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    Double length = null;
+    double a = 0.0, b = 0.0, c = 0.0;
+
+    x = Double.parseDouble(args[0]);
+    y = Double.parseDouble(args[1]);
+    z = Double.parseDouble(args[2]);
+    while (scanner.hasNextDouble())
+      {
+        double d = scanner.nextDouble();
+        if (!scanner.hasNextDouble())
+          break;
+        double e = scanner.nextDouble();
+        if (!scanner.hasNextDouble())
+          break;
+        double f = scanner.nextDouble();
+        double len = dist(d, e, f);
+
+        if (length == null || len < length)
+          {
+            length = len;
+            a = d;
+            b = e;
+            c = f;
+          }
+      }
+    System.out.println(a + " " + b + " " + c);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Deal.java b/usth/ICT2.2/labwork/4/Deal.java
new file mode 100644
index 0000000..b2d9ba9
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Deal.java
@@ -0,0 +1,27 @@
+import static java.util.Collections.shuffle;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.IntStream.range;
+
+class Deal
+{
+  static final String[] suits = {"clubs", "diamonds", "hearts", "spades"};
+  static final String[] ranks = {"Ace", "Two", "Three", "Four", "Five",
+                                 "Six", "Seven", "Eight", "Nine", "Ten",
+                                 "Jack", "Queen", "King"};
+
+  public static void main(String... args)
+  {
+    var deck = range(0, 52).boxed().collect(toList());
+    shuffle(deck);
+
+    // I have no time handling exceptions.
+    int n = Integer.parseInt(args[0]) % 52;
+    while (n-- > 0)
+      {
+        int card = deck.get(n);
+        int suit = card / 13;
+        int rank = card % 13;
+        System.out.printf("%s of %s\n", ranks[rank], suits[suit]);
+      }
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/DiscreteDistro.java b/usth/ICT2.2/labwork/4/DiscreteDistro.java
new file mode 100644
index 0000000..b603ed7
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/DiscreteDistro.java
@@ -0,0 +1,23 @@
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Stream;
+
+import static java.util.Collections.binarySearch;
+import static java.util.stream.Collectors.toList;
+
+class DiscreteDistro
+{
+  public static void main(String... args)
+  {
+    var numbers = Stream.of(args).mapToInt(Integer::parseInt)
+                                 .boxed().collect(toList());
+    int n = numbers.size();
+    for (int i = 1; i < n; ++i)
+      numbers.set(i, numbers.get(i) + numbers.get(i - 1));
+
+    int x = ThreadLocalRandom.current().nextInt(0, numbers.get(n - 1));
+    int i = binarySearch(numbers, x) + 1;
+    System.out.println(numbers);
+    System.out.println(x);
+    System.out.println((i < 0) ? -i : i);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Employ.java b/usth/ICT2.2/labwork/4/Employ.java
new file mode 100644
index 0000000..924e1d3
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Employ.java
@@ -0,0 +1,30 @@
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Scanner;
+
+class Employ
+{
+  private static Employee scan(Scanner s)
+  {
+    int ID = s.nextInt();
+    s.nextLine();
+    return new Employee(ID, s.nextLine(), s.nextLine(),
+                        s.nextDouble(), s.nextDouble());
+  }
+
+  public static void main(String... args) throws FileNotFoundException
+  {
+    var stdin = new Scanner(System.in);
+    var f = new File("employees.txt");
+    var file = new Scanner(f);
+    var employees = new ArrayList<Employee>();
+    int n = stdin.nextInt();
+    for (int i = 0; i < n; ++i)
+      {
+        employees.add(scan(stdin));
+        employees.add(scan(file));
+      }
+    employees.forEach(System.out::println);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Employee.class b/usth/ICT2.2/labwork/4/Employee.class
new file mode 100644
index 0000000..3176b0c
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Employee.class
Binary files differdiff --git a/usth/ICT2.2/labwork/4/Employee.java b/usth/ICT2.2/labwork/4/Employee.java
new file mode 100644
index 0000000..51647b5
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Employee.java
@@ -0,0 +1,27 @@
+public class Employee
+{
+  private int ID;
+  private String name;
+  private String dep;
+  private double basic;
+  private double extra;
+
+  public Employee(int ID, String name, String dep, double basic, double extra)
+  {
+    this.ID = ID;
+    this.name = name;
+    this.dep = dep;
+    this.basic = basic;
+    this.extra = extra;
+  }
+
+  public int getID() { return ID; }
+  public String getName() { return name; }
+  public String getDep() { return dep; }
+  public double getIncome() { return basic + extra*2.5; }
+
+  public String toString()
+  {
+    return String.format("#%d %s [%s]: %g", ID, name, dep, getIncome());
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/HowMany.java b/usth/ICT2.2/labwork/4/HowMany.java
new file mode 100644
index 0000000..16c07bd
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/HowMany.java
@@ -0,0 +1,7 @@
+class HowMany
+{
+  public static void main(String... args)
+  {
+    System.out.println(args.length);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/LongestRun.java b/usth/ICT2.2/labwork/4/LongestRun.java
new file mode 100644
index 0000000..74c18c7
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/LongestRun.java
@@ -0,0 +1,30 @@
+import java.util.Scanner;
+
+class LongestRun
+{
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    Integer number = null;
+    Integer length = null;
+    Integer n = null, len = null;
+
+    while (scanner.hasNextInt())
+      {
+        Integer i = scanner.nextInt();
+        if (i == n)
+          {
+            len++;
+            continue;
+          }
+        if (length == null || len > length)
+          {
+            number = n;
+            length = len;
+          }
+        n = i;
+        len = 1;
+      }
+    System.out.printf("Longest run: %d consecutive %d\n", number, length);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/MaxMin.java b/usth/ICT2.2/labwork/4/MaxMin.java
new file mode 100644
index 0000000..d34c01f
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/MaxMin.java
@@ -0,0 +1,18 @@
+import java.util.ArrayList;
+import java.util.Scanner;
+
+import static java.util.Collections.min;
+import static java.util.Collections.max;
+import static java.util.stream.Collectors.toList;
+
+class MaxMin
+{
+  public static void main(String... args)
+  {
+    var numbers = new ArrayList<Integer>();
+    var scanner = new Scanner(System.in);
+    while (scanner.hasNextInt())
+      numbers.add(scanner.nextInt());
+    System.out.printf("Min: %d\nMax: %d\n", min(numbers), max(numbers));
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Stats.java b/usth/ICT2.2/labwork/4/Stats.java
new file mode 100644
index 0000000..88d6832
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Stats.java
@@ -0,0 +1,19 @@
+import java.util.ArrayList;
+import java.util.Scanner;
+
+class Stats
+{
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    int n = scanner.nextInt();
+    var numbers = new ArrayList<Double>();
+    for (int i = 0; i < n; ++i)
+      numbers.add(scanner.nextDouble());
+
+    double m = numbers.stream().mapToDouble(Double::doubleValue).sum() / n;
+    double s = Math.sqrt(numbers.stream()
+                         .mapToDouble(x -> Math.pow(x - m, 2)).sum() / n);
+    System.out.printf("Mean: %f\nStandard deviation: %f\n", m, s);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/Transpose.java b/usth/ICT2.2/labwork/4/Transpose.java
new file mode 100644
index 0000000..597a783
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/Transpose.java
@@ -0,0 +1,24 @@
+class Transpose
+{
+  public static void main(String... args)
+  {
+    int[][] m = {{7, 8, 9},
+                 {4, 5, 6},
+                 {1, 2, 3}};
+    int n = 3;  // some sort of abstraction
+    System.out.println("Original matrix:");
+    for (int i = 0; i < n; ++i)
+      System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
+
+    for (int i = 1; i < n; ++i)
+      for (int j = 0; j < i; ++j)
+        {
+          m[i][j] ^= m[j][i];
+          m[j][i] ^= m[i][j];
+          m[i][j] ^= m[j][i];
+        }
+    System.out.println("Transposed matrix:");
+    for (int i = 0; i < n; ++i)
+      System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/WordCount.java b/usth/ICT2.2/labwork/4/WordCount.java
new file mode 100644
index 0000000..4d0bfc6
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/WordCount.java
@@ -0,0 +1,16 @@
+import java.util.Scanner;
+
+class WordCount
+{
+  public static void main(String... args)
+  {
+    var scanner = new Scanner(System.in);
+    int count = 0;
+    while (scanner.hasNext())
+      {
+        scanner.next();
+        count++;
+      }
+    System.out.println(count);
+  }
+}
diff --git a/usth/ICT2.2/labwork/4/employees.txt b/usth/ICT2.2/labwork/4/employees.txt
new file mode 100644
index 0000000..7dc4e8f
--- /dev/null
+++ b/usth/ICT2.2/labwork/4/employees.txt
@@ -0,0 +1,8 @@
+7
+Foo Bar
+lmao
+420 69
+3
+Baz Bac
+lol
+1 4