about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/1
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.2/labwork/1')
-rw-r--r--usth/ICT2.2/labwork/1/AllEqual.java10
-rw-r--r--usth/ICT2.2/labwork/1/Binary.java14
-rw-r--r--usth/ICT2.2/labwork/1/Distance.java10
-rw-r--r--usth/ICT2.2/labwork/1/FivePerLine.java11
-rw-r--r--usth/ICT2.2/labwork/1/FunctionGrowth.java10
-rw-r--r--usth/ICT2.2/labwork/1/HelloWorld.java7
-rw-r--r--usth/ICT2.2/labwork/1/Hellos.java14
-rw-r--r--usth/ICT2.2/labwork/1/Kary.java22
-rw-r--r--usth/ICT2.2/labwork/1/RollLoadedDie.java10
-rw-r--r--usth/ICT2.2/labwork/1/SpringSeason.java25
-rw-r--r--usth/ICT2.2/labwork/1/SumOfSines.java8
-rw-r--r--usth/ICT2.2/labwork/1/SumOfTwoDice.java13
-rw-r--r--usth/ICT2.2/labwork/1/TenHelloWorld.java8
-rw-r--r--usth/ICT2.2/labwork/1/UseArguments.java7
-rw-r--r--usth/ICT2.2/labwork/1/UseThree.java8
-rw-r--r--usth/ICT2.2/labwork/1/logic-xor-table.md8
-rw-r--r--usth/ICT2.2/labwork/1/string-concat.md15
17 files changed, 200 insertions, 0 deletions
diff --git a/usth/ICT2.2/labwork/1/AllEqual.java b/usth/ICT2.2/labwork/1/AllEqual.java
new file mode 100644
index 0000000..b1d6929
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/AllEqual.java
@@ -0,0 +1,10 @@
+import java.util.Arrays;
+
+class AllEqual
+{
+  public static void main(String... args)
+  {
+    String first = args[0];
+    System.out.println(Arrays.stream(args).allMatch(a -> (a.equals(first))));
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/Binary.java b/usth/ICT2.2/labwork/1/Binary.java
new file mode 100644
index 0000000..3573a35
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/Binary.java
@@ -0,0 +1,14 @@
+class Binary
+{
+  public static void main(String... args)
+  {
+    int n = Integer.parseInt(args[0]);
+    String s = "";
+    while (n > 0)
+      {
+        s = (n % 2) + s;
+        n >>= 1;
+      }
+    System.out.println(s);
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/Distance.java b/usth/ICT2.2/labwork/1/Distance.java
new file mode 100644
index 0000000..16bcc2c
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/Distance.java
@@ -0,0 +1,10 @@
+class Distance
+{
+  public static void main(String... args)
+  {
+    double x = Double.parseDouble(args[0]);
+    double y = Double.parseDouble(args[1]);
+    System.out.printf("The distance from (%g, %g) to origin is %g\n",
+                      x, y, Math.sqrt(x*x + y*y));
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/FivePerLine.java b/usth/ICT2.2/labwork/1/FivePerLine.java
new file mode 100644
index 0000000..339e6cf
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/FivePerLine.java
@@ -0,0 +1,11 @@
+class FivePerLine
+{
+  public static void main(String... args)
+  {
+    for (int i = 1000; i < 2000; ++i)
+      if (i % 5 < 4)
+        System.out.print(i + " ");
+      else
+        System.out.println(i);
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/FunctionGrowth.java b/usth/ICT2.2/labwork/1/FunctionGrowth.java
new file mode 100644
index 0000000..e1bebfa
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/FunctionGrowth.java
@@ -0,0 +1,10 @@
+class FunctionGrowth
+{
+  public static void main(String... args)
+  {
+    System.out.println("ln n\tn\tn ln n\tn^2\tn^3\t2^n");
+    for (long n = 16; n < 3005; n <<= 1)
+      System.out.printf("%g\t%d\t%g\t%d\t%d\t%g\n", Math.log(n), n,
+                        n * Math.log(n), n*n, n*n*n, Math.pow(2, n));
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/HelloWorld.java b/usth/ICT2.2/labwork/1/HelloWorld.java
new file mode 100644
index 0000000..cf7313a
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/HelloWorld.java
@@ -0,0 +1,7 @@
+class HelloWorld
+{
+  public static void main(String... args)
+  {
+    System.out.println("Hello, World!");
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/Hellos.java b/usth/ICT2.2/labwork/1/Hellos.java
new file mode 100644
index 0000000..206ac58
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/Hellos.java
@@ -0,0 +1,14 @@
+class Hellos
+{
+  private static final String[] TH = {"th", "st", "nd", "rd", "th",
+                                      "th", "th", "th", "th", "th"};
+
+  public static void main(String... args)
+  {
+    int n = Integer.parseInt(args[0]);
+    for (int i = 1; i <= n; ++i)
+      // Before one says this defeat the purpose of the section,
+      // the tertiary operator IS conditional.
+      System.out.println(i + TH[i % 100 / 10 == 1 ? 7 : i % 10] + " Hello");
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/Kary.java b/usth/ICT2.2/labwork/1/Kary.java
new file mode 100644
index 0000000..51fd9af
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/Kary.java
@@ -0,0 +1,22 @@
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+class Kary
+{ 
+  private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7',
+                                        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+  public static void main(String... args)
+  { 
+    int n = Integer.parseInt(args[0]);
+    List<Character> result = new ArrayList<Character>();
+    for (int k = Integer.parseInt(args[1]); n > 0; n /= k)
+      result.add(DIGITS[n % k]);
+
+    Collections.reverse(result);
+    for (char d : result)
+      System.out.print(d);
+    System.out.println();
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/RollLoadedDie.java b/usth/ICT2.2/labwork/1/RollLoadedDie.java
new file mode 100644
index 0000000..161fd93
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/RollLoadedDie.java
@@ -0,0 +1,10 @@
+import java.util.concurrent.ThreadLocalRandom;
+
+class RollLoadedDie
+{
+  public static void main(String... args)
+  {
+    int x = ThreadLocalRandom.current().nextInt(1, 9);
+    System.out.println((x < 6) ? x : 6);
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/SpringSeason.java b/usth/ICT2.2/labwork/1/SpringSeason.java
new file mode 100644
index 0000000..2fda09b
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/SpringSeason.java
@@ -0,0 +1,25 @@
+import java.time.LocalDate;
+import java.time.DateTimeException;
+
+class SpringSeason
+{
+  private static final LocalDate start = LocalDate.of(42, 3, 19);
+  private static final LocalDate end = LocalDate.of(42, 6, 21);
+
+  public static void main(String... args)
+  {
+    int m = Integer.parseInt(args[0]);
+    int d = Integer.parseInt(args[1]);
+    try
+      {
+        LocalDate query = LocalDate.of(42, m, d);
+        // Yes, I'm sorry for leaving it here.
+        // If only Java has the try-except-else like Python!
+        System.out.println(query.isAfter(start) && query.isBefore(end));
+      }
+    catch (DateTimeException e)
+      {
+        System.out.println(false);
+      }
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/SumOfSines.java b/usth/ICT2.2/labwork/1/SumOfSines.java
new file mode 100644
index 0000000..2cf6e03
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/SumOfSines.java
@@ -0,0 +1,8 @@
+class SumOfSines
+{
+  public static void main(String... args)
+  {
+    double t = Double.parseDouble(args[0]);
+    System.out.printf("sin2t + sin3t = %g", Math.sin(t * 2) + Math.sin (t * 3));
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/SumOfTwoDice.java b/usth/ICT2.2/labwork/1/SumOfTwoDice.java
new file mode 100644
index 0000000..d955f28
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/SumOfTwoDice.java
@@ -0,0 +1,13 @@
+import java.util.concurrent.ThreadLocalRandom;
+
+class SumOfTwoDice
+{
+  public static void main(String... args)
+  {
+    int x = ThreadLocalRandom.current().nextInt(1, 7);
+    System.out.println("First roll: " + x);
+    int y = ThreadLocalRandom.current().nextInt(1, 7);
+    System.out.println("Second roll: " + y);
+    System.out.println("Sum: " + (x + y));
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/TenHelloWorld.java b/usth/ICT2.2/labwork/1/TenHelloWorld.java
new file mode 100644
index 0000000..d8a2cd9
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/TenHelloWorld.java
@@ -0,0 +1,8 @@
+class TenHelloWorld
+{
+  public static void main(String... args)
+  {
+    for (int i = 0; i < 10; ++i)
+      System.out.println("Hello, World!");
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/UseArguments.java b/usth/ICT2.2/labwork/1/UseArguments.java
new file mode 100644
index 0000000..d477f51
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/UseArguments.java
@@ -0,0 +1,7 @@
+class UseArguments
+{
+  public static void main(String... args)
+  {
+    System.out.printf("Hi %s.  How are you?\n", args[0]);
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/UseThree.java b/usth/ICT2.2/labwork/1/UseThree.java
new file mode 100644
index 0000000..9b5693b
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/UseThree.java
@@ -0,0 +1,8 @@
+class UseThree
+{
+  public static void main(String... args)
+  {
+    System.out.printf("Hi %s, %s and %s.  How are you?\n",
+                      args[2], args[1], args[0]);
+  }
+}
diff --git a/usth/ICT2.2/labwork/1/logic-xor-table.md b/usth/ICT2.2/labwork/1/logic-xor-table.md
new file mode 100644
index 0000000..e955d5b
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/logic-xor-table.md
@@ -0,0 +1,8 @@
+# Logical Exclusive Or Table
+
+|   a   |   b   | a ^ b |
+| ----- | ----- | ----- |
+| false | false | false |
+| false | true  | true  |
+| true  | false | true  |
+| true  | true  | false |
diff --git a/usth/ICT2.2/labwork/1/string-concat.md b/usth/ICT2.2/labwork/1/string-concat.md
new file mode 100644
index 0000000..624383f
--- /dev/null
+++ b/usth/ICT2.2/labwork/1/string-concat.md
@@ -0,0 +1,15 @@
+# String Concatenation
+
+To quote the [official Java documentation](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html):
+
+> The Java language provides special support for the string concatenation
+> operator (`+`), and for conversion of other objects to strings.  [...]
+> String conversions are implemented through the method `toString`, defined
+> by Object and inherited by all classes in Java.
+
+Thus the numbers (e.g. 2, 2 + 3 = 5) are converted to their strings
+representations ("2", "5") and concatenated to the string.  Since `+` is
+operated from left to right,
+
+    2 + 3 + "bc" = 5 + "bc" = "5" + "bc" = "5bc"
+    "bc" + 2 + 3 = "bc" + "2" + 3 = "bc2" + 3 = "bc2" + "3" = "bc23"