From 67393f42f41ab92219deb549f711121c4dab845b Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 15 Dec 2019 10:34:58 +0700 Subject: [usth/ICT2.2] Object Oriented Programming --- usth/ICT2.2/labwork/1/AllEqual.java | 10 ++++++++++ usth/ICT2.2/labwork/1/Binary.java | 14 ++++++++++++++ usth/ICT2.2/labwork/1/Distance.java | 10 ++++++++++ usth/ICT2.2/labwork/1/FivePerLine.java | 11 +++++++++++ usth/ICT2.2/labwork/1/FunctionGrowth.java | 10 ++++++++++ usth/ICT2.2/labwork/1/HelloWorld.java | 7 +++++++ usth/ICT2.2/labwork/1/Hellos.java | 14 ++++++++++++++ usth/ICT2.2/labwork/1/Kary.java | 22 ++++++++++++++++++++++ usth/ICT2.2/labwork/1/RollLoadedDie.java | 10 ++++++++++ usth/ICT2.2/labwork/1/SpringSeason.java | 25 +++++++++++++++++++++++++ usth/ICT2.2/labwork/1/SumOfSines.java | 8 ++++++++ usth/ICT2.2/labwork/1/SumOfTwoDice.java | 13 +++++++++++++ usth/ICT2.2/labwork/1/TenHelloWorld.java | 8 ++++++++ usth/ICT2.2/labwork/1/UseArguments.java | 7 +++++++ usth/ICT2.2/labwork/1/UseThree.java | 8 ++++++++ usth/ICT2.2/labwork/1/logic-xor-table.md | 8 ++++++++ usth/ICT2.2/labwork/1/string-concat.md | 15 +++++++++++++++ 17 files changed, 200 insertions(+) create mode 100644 usth/ICT2.2/labwork/1/AllEqual.java create mode 100644 usth/ICT2.2/labwork/1/Binary.java create mode 100644 usth/ICT2.2/labwork/1/Distance.java create mode 100644 usth/ICT2.2/labwork/1/FivePerLine.java create mode 100644 usth/ICT2.2/labwork/1/FunctionGrowth.java create mode 100644 usth/ICT2.2/labwork/1/HelloWorld.java create mode 100644 usth/ICT2.2/labwork/1/Hellos.java create mode 100644 usth/ICT2.2/labwork/1/Kary.java create mode 100644 usth/ICT2.2/labwork/1/RollLoadedDie.java create mode 100644 usth/ICT2.2/labwork/1/SpringSeason.java create mode 100644 usth/ICT2.2/labwork/1/SumOfSines.java create mode 100644 usth/ICT2.2/labwork/1/SumOfTwoDice.java create mode 100644 usth/ICT2.2/labwork/1/TenHelloWorld.java create mode 100644 usth/ICT2.2/labwork/1/UseArguments.java create mode 100644 usth/ICT2.2/labwork/1/UseThree.java create mode 100644 usth/ICT2.2/labwork/1/logic-xor-table.md create mode 100644 usth/ICT2.2/labwork/1/string-concat.md (limited to 'usth/ICT2.2/labwork/1') 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 result = new ArrayList(); + 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" -- cgit 1.4.1