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 --- .../src/main/java/com/mycompany/app/App.java | 9 ++++++++ .../src/main/java/com/mycompany/app/Beers.java | 17 +++++++++++++++ .../main/java/com/mycompany/app/IsLeapYear.java | 14 +++++++++++++ .../src/main/java/com/mycompany/app/Linear.java | 12 +++++++++++ .../src/main/java/com/mycompany/app/MeanSTD.java | 17 +++++++++++++++ .../src/main/java/com/mycompany/app/Quadratic.java | 24 ++++++++++++++++++++++ .../src/main/java/com/mycompany/app/RandRange.java | 13 ++++++++++++ .../src/main/java/com/mycompany/app/TenHellos.java | 10 +++++++++ .../src/main/java/com/mycompany/app/UseThree.java | 11 ++++++++++ .../src/test/java/com/mycompany/app/AppTest.java | 16 +++++++++++++++ 10 files changed, 143 insertions(+) create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/UseThree.java create mode 100644 usth/ICT2.2/labwork/2/my-app/src/test/java/com/mycompany/app/AppTest.java (limited to 'usth/ICT2.2/labwork/2/my-app/src') diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..0f661cb --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,9 @@ +package com.mycompany.app; + +public class App +{ + public static void main(String... args) + { + System.out.println("Hello World!"); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java new file mode 100644 index 0000000..b74a93b --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java @@ -0,0 +1,17 @@ +package com.mycompany.app; + +// Exercise 4 +public class Beers +{ + public static void main(String... args) + { + for (int i = 9; i > 1; --i) + System.out.printf( + "%d bottles of beer we are going to drink, %d bottles of beer.\n" + + "Now try to drink one, drink one,\n", i, i); + System.out.print( + "1 bottle of beer we are going to drink, 1 bottle of beer.\n" + + "Now try to drink one, drink one,\n" + + "Oh no, no bottles of beer to drink now.\n"); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java new file mode 100644 index 0000000..f21b4b3 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java @@ -0,0 +1,14 @@ +package com.mycompany.app; + +// Exercise 7 +public class IsLeapYear +{ + public static void main(String... args) + { + int n = Integer.parseInt(args[0]); + if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) + System.out.printf("%d is a leap year\n", n); + else + System.out.printf("%d is not a leap year\n", n); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java new file mode 100644 index 0000000..e7a8413 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java @@ -0,0 +1,12 @@ +package com.mycompany.app; + +// Exercise 5 +public class Linear +{ + public static void main(String... args) + { + double a = Double.parseDouble(args[0]); + double b = Double.parseDouble(args[1]); + System.out.println(-b / a); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java new file mode 100644 index 0000000..eb47914 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java @@ -0,0 +1,17 @@ +package com.mycompany.app; + +import java.util.Arrays; + +// Exercise 8 +public class MeanSTD +{ + public static void main(String... args) + { + double n = args.length; + double m = Arrays.stream(args).mapToDouble(Double::parseDouble).sum() / n; + double s = Math.sqrt( + Arrays.stream(args) + .mapToDouble(x -> Math.pow(Double.parseDouble(x) - m, 2)).sum() / n); + System.out.printf("Mean: %f\nStandard deviation: %f\n", m, s); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java new file mode 100644 index 0000000..84d3ecd --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java @@ -0,0 +1,24 @@ +package com.mycompany.app; + +// Exercise 6 +public class Quadratic +{ + public static void main(String... args) + { + double a = Double.parseDouble(args[0]); + double b = Double.parseDouble(args[1]); + double c = Double.parseDouble(args[2]); + // assume ax^2 + bx + c = 0 is a valid quadratic equation + double d = b*b - a*c*4; + if (d < 0) + { + System.out.printf("%f + %fj\n", -b/a/2, Math.sqrt(-d)/a/2); + System.out.printf("%f + %fj\n", -b/a/2, -Math.sqrt(-d)/a/2); + } + else + { + System.out.println(-b/a/2 + Math.sqrt(d)/a/2); + System.out.println(-b/a/2 - Math.sqrt(d)/a/2); + } + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java new file mode 100644 index 0000000..f5c5ffd --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java @@ -0,0 +1,13 @@ +package com.mycompany.app; + +import java.util.concurrent.ThreadLocalRandom; + +// Exercise 3 +class RandRange +{ + public static void main(String... args) + { + System.out.println( + ThreadLocalRandom.current().nextInt(0, Integer.parseInt(args[0]))); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java new file mode 100644 index 0000000..a517d39 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java @@ -0,0 +1,10 @@ +package com.mycompany.app; + +public class TenHellos +{ + 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/2/my-app/src/main/java/com/mycompany/app/UseThree.java b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/UseThree.java new file mode 100644 index 0000000..a139fc8 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/UseThree.java @@ -0,0 +1,11 @@ +package com.mycompany.app; + +// Exercise 2 +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/2/my-app/src/test/java/com/mycompany/app/AppTest.java b/usth/ICT2.2/labwork/2/my-app/src/test/java/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..4291826 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/src/test/java/com/mycompany/app/AppTest.java @@ -0,0 +1,16 @@ +package com.mycompany.app; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +// Unit test for simple App. +public class AppTest +{ + // Rigorous Test :-) + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} -- cgit 1.4.1