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/final/Member.java | 54 ++ usth/ICT2.2/final/Point.java | 43 + usth/ICT2.2/final/Problem1.java | 15 + usth/ICT2.2/final/Problem2.java | 22 + usth/ICT2.2/final/Problem3.java | 40 + usth/ICT2.2/final/Report.java | 18 + 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 + usth/ICT2.2/labwork/2/1_labwork1-extra.pdf | Bin 0 -> 1284474 bytes usth/ICT2.2/labwork/2/2_labwork2.pdf | Bin 0 -> 1788103 bytes usth/ICT2.2/labwork/2/README.md | 18 + usth/ICT2.2/labwork/2/my-app/pom.xml | 75 ++ .../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 + .../target/classes/com/mycompany/app/App.class | Bin 0 -> 549 bytes .../target/classes/com/mycompany/app/Beers.class | Bin 0 -> 1033 bytes .../classes/com/mycompany/app/IsLeapYear.class | Bin 0 -> 879 bytes .../target/classes/com/mycompany/app/Linear.class | Bin 0 -> 649 bytes .../target/classes/com/mycompany/app/MeanSTD.class | Bin 0 -> 1981 bytes .../classes/com/mycompany/app/Quadratic.class | Bin 0 -> 1120 bytes .../classes/com/mycompany/app/RandRange.class | Bin 0 -> 756 bytes .../classes/com/mycompany/app/TenHellos.class | Bin 0 -> 639 bytes .../classes/com/mycompany/app/UseThree.class | Bin 0 -> 645 bytes .../2/my-app/target/maven-archiver/pom.properties | 4 + .../compile/default-compile/createdFiles.lst | 9 + .../compile/default-compile/inputFiles.lst | 9 + .../default-testCompile/createdFiles.lst | 1 + .../testCompile/default-testCompile/inputFiles.lst | 1 + .../2/my-app/target/my-app-1.0-SNAPSHOT.jar | Bin 0 -> 8391 bytes .../TEST-com.mycompany.app.AppTest.xml | 60 ++ .../surefire-reports/com.mycompany.app.AppTest.txt | 4 + .../test-classes/com/mycompany/app/AppTest.class | Bin 0 -> 481 bytes usth/ICT2.2/labwork/3/3_labwork3.pdf | Bin 0 -> 1520939 bytes usth/ICT2.2/labwork/3/C++/README.md | 10 + usth/ICT2.2/labwork/3/C++/automobile.cc | 56 ++ usth/ICT2.2/labwork/3/C++/automobile.h | 20 + usth/ICT2.2/labwork/3/C++/button-test.cc | 14 + usth/ICT2.2/labwork/3/C++/button.h | 14 + usth/ICT2.2/labwork/3/C++/cow-test.cc | 9 + usth/ICT2.2/labwork/3/C++/cow.cc | 11 + usth/ICT2.2/labwork/3/C++/cow.h | 17 + usth/ICT2.2/labwork/3/C++/name-card-test.cc | 14 + usth/ICT2.2/labwork/3/C++/name-card.cc | 47 ++ usth/ICT2.2/labwork/3/C++/name-card.h | 18 + usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc | 19 + usth/ICT2.2/labwork/3/C++/shopping-cart.h | 16 + usth/ICT2.2/labwork/3/C++/vector-test.cc | 20 + usth/ICT2.2/labwork/3/C++/vector.cc | 19 + usth/ICT2.2/labwork/3/C++/vector.h | 17 + usth/ICT2.2/labwork/3/Java/Automobile.java | 68 ++ usth/ICT2.2/labwork/3/Java/Button.java | 50 ++ usth/ICT2.2/labwork/3/Java/ButtonTest.java | 12 + usth/ICT2.2/labwork/3/Java/Cow.java | 36 + usth/ICT2.2/labwork/3/Java/CowTest.java | 9 + usth/ICT2.2/labwork/3/Java/NameCard.java | 67 ++ usth/ICT2.2/labwork/3/Java/NameCardTest.java | 9 + usth/ICT2.2/labwork/3/Java/README.md | 10 + usth/ICT2.2/labwork/3/Java/ShoppingCart.java | 27 + usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java | 16 + usth/ICT2.2/labwork/3/Java/Vector.java | 39 + usth/ICT2.2/labwork/3/Java/VectorTest.java | 10 + usth/ICT2.2/labwork/4/4_labwork4.pdf | Bin 0 -> 1305629 bytes usth/ICT2.2/labwork/4/BubbleSort.java | 21 + usth/ICT2.2/labwork/4/Centroid.java | 26 + usth/ICT2.2/labwork/4/Closest.java | 45 + usth/ICT2.2/labwork/4/Deal.java | 27 + usth/ICT2.2/labwork/4/DiscreteDistro.java | 23 + usth/ICT2.2/labwork/4/Employ.java | 30 + usth/ICT2.2/labwork/4/Employee.class | Bin 0 -> 1052 bytes usth/ICT2.2/labwork/4/Employee.java | 27 + usth/ICT2.2/labwork/4/HowMany.java | 7 + usth/ICT2.2/labwork/4/LongestRun.java | 30 + usth/ICT2.2/labwork/4/MaxMin.java | 18 + usth/ICT2.2/labwork/4/Stats.java | 19 + usth/ICT2.2/labwork/4/Transpose.java | 24 + usth/ICT2.2/labwork/4/WordCount.java | 16 + usth/ICT2.2/labwork/4/employees.txt | 8 + usth/ICT2.2/labwork/5/Java/abstract/Circle.java | 13 + usth/ICT2.2/labwork/5/Java/abstract/Cylinder.java | 14 + usth/ICT2.2/labwork/5/Java/abstract/Point.java | 13 + usth/ICT2.2/labwork/5/Java/abstract/Shape.java | 12 + usth/ICT2.2/labwork/5/Java/abstract/Test.java | 11 + usth/ICT2.2/labwork/5/Java/interface/Circle.java | 13 + usth/ICT2.2/labwork/5/Java/interface/Cylinder.java | 14 + usth/ICT2.2/labwork/5/Java/interface/Point.java | 21 + usth/ICT2.2/labwork/5/Java/interface/Shape.java | 6 + usth/ICT2.2/labwork/5/Java/interface/Test.java | 11 + usth/ICT2.2/labwork/5/virtual.cc | 61 ++ usth/ICT2.2/midterm/report/bubbleplot.jpg | Bin 0 -> 20549 bytes usth/ICT2.2/midterm/report/report.pdf | Bin 0 -> 278214 bytes usth/ICT2.2/midterm/report/report.tex | 928 +++++++++++++++++++++ usth/ICT2.2/midterm/slides/USTH.jpg | Bin 0 -> 375099 bytes usth/ICT2.2/midterm/slides/bubbleplot.jpg | Bin 0 -> 20549 bytes usth/ICT2.2/midterm/slides/slides.pdf | Bin 0 -> 980519 bytes usth/ICT2.2/midterm/slides/slides.tex | 734 ++++++++++++++++ usth/ICT2.2/midterm/slides/squint.jpg | Bin 0 -> 70262 bytes usth/ICT2.2/midterm/src/Compare.java | 9 + usth/ICT2.2/midterm/src/Heap.java | 61 ++ usth/ICT2.2/midterm/src/Person.java | 38 + usth/ICT2.2/midterm/src/Search.java | 57 ++ usth/ICT2.2/midterm/src/Sort.java | 49 ++ 124 files changed, 3776 insertions(+) create mode 100644 usth/ICT2.2/final/Member.java create mode 100644 usth/ICT2.2/final/Point.java create mode 100644 usth/ICT2.2/final/Problem1.java create mode 100644 usth/ICT2.2/final/Problem2.java create mode 100644 usth/ICT2.2/final/Problem3.java create mode 100644 usth/ICT2.2/final/Report.java 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 create mode 100644 usth/ICT2.2/labwork/2/1_labwork1-extra.pdf create mode 100644 usth/ICT2.2/labwork/2/2_labwork2.pdf create mode 100644 usth/ICT2.2/labwork/2/README.md create mode 100644 usth/ICT2.2/labwork/2/my-app/pom.xml 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 create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/App.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Beers.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/IsLeapYear.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Linear.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/MeanSTD.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Quadratic.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/RandRange.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/TenHellos.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/UseThree.class create mode 100644 usth/ICT2.2/labwork/2/my-app/target/maven-archiver/pom.properties create mode 100644 usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 usth/ICT2.2/labwork/2/my-app/target/my-app-1.0-SNAPSHOT.jar create mode 100644 usth/ICT2.2/labwork/2/my-app/target/surefire-reports/TEST-com.mycompany.app.AppTest.xml create mode 100644 usth/ICT2.2/labwork/2/my-app/target/surefire-reports/com.mycompany.app.AppTest.txt create mode 100644 usth/ICT2.2/labwork/2/my-app/target/test-classes/com/mycompany/app/AppTest.class create mode 100644 usth/ICT2.2/labwork/3/3_labwork3.pdf create mode 100644 usth/ICT2.2/labwork/3/C++/README.md create mode 100644 usth/ICT2.2/labwork/3/C++/automobile.cc create mode 100644 usth/ICT2.2/labwork/3/C++/automobile.h create mode 100644 usth/ICT2.2/labwork/3/C++/button-test.cc create mode 100644 usth/ICT2.2/labwork/3/C++/button.h create mode 100644 usth/ICT2.2/labwork/3/C++/cow-test.cc create mode 100644 usth/ICT2.2/labwork/3/C++/cow.cc create mode 100644 usth/ICT2.2/labwork/3/C++/cow.h create mode 100644 usth/ICT2.2/labwork/3/C++/name-card-test.cc create mode 100644 usth/ICT2.2/labwork/3/C++/name-card.cc create mode 100644 usth/ICT2.2/labwork/3/C++/name-card.h create mode 100644 usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc create mode 100644 usth/ICT2.2/labwork/3/C++/shopping-cart.h create mode 100644 usth/ICT2.2/labwork/3/C++/vector-test.cc create mode 100644 usth/ICT2.2/labwork/3/C++/vector.cc create mode 100644 usth/ICT2.2/labwork/3/C++/vector.h create mode 100644 usth/ICT2.2/labwork/3/Java/Automobile.java create mode 100644 usth/ICT2.2/labwork/3/Java/Button.java create mode 100644 usth/ICT2.2/labwork/3/Java/ButtonTest.java create mode 100644 usth/ICT2.2/labwork/3/Java/Cow.java create mode 100644 usth/ICT2.2/labwork/3/Java/CowTest.java create mode 100644 usth/ICT2.2/labwork/3/Java/NameCard.java create mode 100644 usth/ICT2.2/labwork/3/Java/NameCardTest.java create mode 100644 usth/ICT2.2/labwork/3/Java/README.md create mode 100644 usth/ICT2.2/labwork/3/Java/ShoppingCart.java create mode 100644 usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java create mode 100644 usth/ICT2.2/labwork/3/Java/Vector.java create mode 100644 usth/ICT2.2/labwork/3/Java/VectorTest.java create mode 100644 usth/ICT2.2/labwork/4/4_labwork4.pdf create mode 100644 usth/ICT2.2/labwork/4/BubbleSort.java create mode 100644 usth/ICT2.2/labwork/4/Centroid.java create mode 100644 usth/ICT2.2/labwork/4/Closest.java create mode 100644 usth/ICT2.2/labwork/4/Deal.java create mode 100644 usth/ICT2.2/labwork/4/DiscreteDistro.java create mode 100644 usth/ICT2.2/labwork/4/Employ.java create mode 100644 usth/ICT2.2/labwork/4/Employee.class create mode 100644 usth/ICT2.2/labwork/4/Employee.java create mode 100644 usth/ICT2.2/labwork/4/HowMany.java create mode 100644 usth/ICT2.2/labwork/4/LongestRun.java create mode 100644 usth/ICT2.2/labwork/4/MaxMin.java create mode 100644 usth/ICT2.2/labwork/4/Stats.java create mode 100644 usth/ICT2.2/labwork/4/Transpose.java create mode 100644 usth/ICT2.2/labwork/4/WordCount.java create mode 100644 usth/ICT2.2/labwork/4/employees.txt create mode 100644 usth/ICT2.2/labwork/5/Java/abstract/Circle.java create mode 100644 usth/ICT2.2/labwork/5/Java/abstract/Cylinder.java create mode 100644 usth/ICT2.2/labwork/5/Java/abstract/Point.java create mode 100644 usth/ICT2.2/labwork/5/Java/abstract/Shape.java create mode 100644 usth/ICT2.2/labwork/5/Java/abstract/Test.java create mode 100644 usth/ICT2.2/labwork/5/Java/interface/Circle.java create mode 100644 usth/ICT2.2/labwork/5/Java/interface/Cylinder.java create mode 100644 usth/ICT2.2/labwork/5/Java/interface/Point.java create mode 100644 usth/ICT2.2/labwork/5/Java/interface/Shape.java create mode 100644 usth/ICT2.2/labwork/5/Java/interface/Test.java create mode 100644 usth/ICT2.2/labwork/5/virtual.cc create mode 100644 usth/ICT2.2/midterm/report/bubbleplot.jpg create mode 100644 usth/ICT2.2/midterm/report/report.pdf create mode 100644 usth/ICT2.2/midterm/report/report.tex create mode 100644 usth/ICT2.2/midterm/slides/USTH.jpg create mode 100644 usth/ICT2.2/midterm/slides/bubbleplot.jpg create mode 100644 usth/ICT2.2/midterm/slides/slides.pdf create mode 100644 usth/ICT2.2/midterm/slides/slides.tex create mode 100644 usth/ICT2.2/midterm/slides/squint.jpg create mode 100644 usth/ICT2.2/midterm/src/Compare.java create mode 100644 usth/ICT2.2/midterm/src/Heap.java create mode 100644 usth/ICT2.2/midterm/src/Person.java create mode 100644 usth/ICT2.2/midterm/src/Search.java create mode 100644 usth/ICT2.2/midterm/src/Sort.java (limited to 'usth') diff --git a/usth/ICT2.2/final/Member.java b/usth/ICT2.2/final/Member.java new file mode 100644 index 0000000..a3e2092 --- /dev/null +++ b/usth/ICT2.2/final/Member.java @@ -0,0 +1,54 @@ +public class Member +{ + public static final String[] ROLES = {"leader", "club coordinator", + "keynote speaker of code sharing", + "product programmer"}; + + private String name; + private String time; + private int role; + + public String getName() + { + return name; + } + + public String getTime() + { + return time; + } + + public int getRole() + { + return role; + } + + public void setName(String name) + { + this.name = name; + } + + public void setTime(String time) + { + this.time = time; + } + + public void setRole(int role) throws IllegalArgumentException + { + if (role < 0 || role > 3) + throw new IllegalArgumentException("invalid role number: " + role); + this.role = role; + } + + public Member(String name, String time, int role) + { + setName(name); + setTime(time); + setRole(role); + } + + public String toString() + { + return String.format("%s (%s), from %s", name, ROLES[role], time); + } +} diff --git a/usth/ICT2.2/final/Point.java b/usth/ICT2.2/final/Point.java new file mode 100644 index 0000000..5e79428 --- /dev/null +++ b/usth/ICT2.2/final/Point.java @@ -0,0 +1,43 @@ +// Immutable Point +public class Point +{ + private double x; + private double y; + + public Point(double x, double y) + { + this.x = x; + this.y = y; + } + + public double getX() + { + return x; + } + + public double getY() + { + return y; + } + + public String toString() + { + return String.format("(%g, %g)", x, y); + } + + public static Point add(Point a, Point b) + { + return new Point(a.getX() + b.getX(), a.getY() + b.getY()); + } + + public static Point subtract(Point a, Point b) + { + return new Point(a.getX() - b.getX(), a.getY() - b.getY()); + } + + public static double calDisEuclid(Point a, Point b) + { + var trans = Point.subtract(a, b); + return Math.sqrt(trans.getX()*trans.getX() + trans.getY()*trans.getY()); + } +} diff --git a/usth/ICT2.2/final/Problem1.java b/usth/ICT2.2/final/Problem1.java new file mode 100644 index 0000000..a9c5e32 --- /dev/null +++ b/usth/ICT2.2/final/Problem1.java @@ -0,0 +1,15 @@ +import java.util.Scanner; + +class Problem1 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + int n = scanner.nextInt(); + for (int i = 1; i <= n; i += 2) + System.out.println(i); + if (n % 2 == 0) + n--; + System.out.println((n + 1) * (n + 1) / 4); + } +} diff --git a/usth/ICT2.2/final/Problem2.java b/usth/ICT2.2/final/Problem2.java new file mode 100644 index 0000000..dcad796 --- /dev/null +++ b/usth/ICT2.2/final/Problem2.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +class Problem2 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + System.out.println( + "Please enter two numbers which are the coordinate of one point:"); + var a = new Point(scanner.nextDouble(), scanner.nextDouble()); + System.out.println( + "Please enter two numbers which are the coordinate of another point:"); + var b = new Point(scanner.nextDouble(), scanner.nextDouble()); + + System.out.printf("First point: %s\nSecond point: %s\n", a, b); + // Vector would make a better name than Point + System.out.printf("Their sum: %s\nTheir difference: %s\n", + Point.add(a, b), Point.subtract(a, b)); + System.out.printf("The Euclidean distance between the two points: %s\n", + Point.calDisEuclid(a, b)); + } +} diff --git a/usth/ICT2.2/final/Problem3.java b/usth/ICT2.2/final/Problem3.java new file mode 100644 index 0000000..11be143 --- /dev/null +++ b/usth/ICT2.2/final/Problem3.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Problem3 +{ + public static void main(String... args) + { + var scanner = new Scanner(System.in); + System.out.println("Number of members:"); + int n = scanner.nextInt(); + + var members = new ArrayList(); + for (int i = 0; i < n; ++i) + { + System.out.println("Member name: "); + String name = scanner.next(); + System.out.println("Member joining time: "); + String time = scanner.next(); + System.out.println("Member role number: "); + int role = scanner.nextInt(); + members.add(new Member(name, time, role)); + } + + var reports = new ArrayList(); + for (var m : members) + { + System.out.println(m); + System.out.println("Activity: "); + String act = scanner.next(); + System.out.println("Rate: "); + int rate = scanner.nextInt(); + reports.add(new Report(m.getName(), act, rate)); + } + + System.out.println("Members:"); + members.forEach(System.out::println); + System.out.println("Activity reports:"); + reports.forEach(System.out::println); + } +} diff --git a/usth/ICT2.2/final/Report.java b/usth/ICT2.2/final/Report.java new file mode 100644 index 0000000..ec4ee23 --- /dev/null +++ b/usth/ICT2.2/final/Report.java @@ -0,0 +1,18 @@ +public class Report +{ + String name; + String activity; + int rate; + + public Report(String name, String act, int rate) + { + this.name = name; + this.activity = act; + this.rate = rate; + } + + public String toString() + { + return String.format("%s: %s: %s", name, activity, rate); + } +} 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" diff --git a/usth/ICT2.2/labwork/2/1_labwork1-extra.pdf b/usth/ICT2.2/labwork/2/1_labwork1-extra.pdf new file mode 100644 index 0000000..278400a Binary files /dev/null and b/usth/ICT2.2/labwork/2/1_labwork1-extra.pdf differ diff --git a/usth/ICT2.2/labwork/2/2_labwork2.pdf b/usth/ICT2.2/labwork/2/2_labwork2.pdf new file mode 100644 index 0000000..3cd1908 Binary files /dev/null and b/usth/ICT2.2/labwork/2/2_labwork2.pdf differ diff --git a/usth/ICT2.2/labwork/2/README.md b/usth/ICT2.2/labwork/2/README.md new file mode 100644 index 0000000..a81631e --- /dev/null +++ b/usth/ICT2.2/labwork/2/README.md @@ -0,0 +1,18 @@ +# Build with Maven + +Following the official guide [Maven in 5 Minutes](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html): + +1. Create a project named `my-app`: + + mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false + +2. Change into the project directory: `cd my-app` +3. Build the project: `mvn package` + +Extra exercises for labwork 1 are written in `src/main/java/com/mycompany/app/` +inside `my-app`. To launch a class named `App` for example, run + + java -cp my-app/target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App + +I find this build method to be overcomplicated for 5-minute throw-away programs +and decide not to use it in the upcoming labworks. diff --git a/usth/ICT2.2/labwork/2/my-app/pom.xml b/usth/ICT2.2/labwork/2/my-app/pom.xml new file mode 100644 index 0000000..07012c1 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.mycompany.app + my-app + 1.0-SNAPSHOT + + my-app + + http://www.example.com + + + UTF-8 + 1.8 + 1.8 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + 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 ); + } +} diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/App.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/App.class new file mode 100644 index 0000000..740d561 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/App.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Beers.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Beers.class new file mode 100644 index 0000000..ff4573d Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Beers.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/IsLeapYear.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/IsLeapYear.class new file mode 100644 index 0000000..f8464c6 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/IsLeapYear.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Linear.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Linear.class new file mode 100644 index 0000000..6797c5e Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Linear.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/MeanSTD.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/MeanSTD.class new file mode 100644 index 0000000..8da82c5 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/MeanSTD.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Quadratic.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Quadratic.class new file mode 100644 index 0000000..15b1c7b Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Quadratic.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/RandRange.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/RandRange.class new file mode 100644 index 0000000..9fe4ac3 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/RandRange.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/TenHellos.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/TenHellos.class new file mode 100644 index 0000000..ce0b894 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/TenHellos.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/UseThree.class b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/UseThree.class new file mode 100644 index 0000000..b66069e Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/UseThree.class differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/maven-archiver/pom.properties b/usth/ICT2.2/labwork/2/my-app/target/maven-archiver/pom.properties new file mode 100644 index 0000000..248310d --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/maven-archiver/pom.properties @@ -0,0 +1,4 @@ +#Created by Apache Maven 3.6.2 +groupId=com.mycompany.app +artifactId=my-app +version=1.0-SNAPSHOT diff --git a/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..5a3a47c --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,9 @@ +com/mycompany/app/App.class +com/mycompany/app/UseThree.class +com/mycompany/app/Linear.class +com/mycompany/app/TenHellos.class +com/mycompany/app/MeanSTD.class +com/mycompany/app/RandRange.class +com/mycompany/app/Beers.class +com/mycompany/app/IsLeapYear.class +com/mycompany/app/Quadratic.class diff --git a/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..7c51ee3 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,9 @@ +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Quadratic.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/IsLeapYear.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/App.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/RandRange.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Beers.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/Linear.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/TenHellos.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/MeanSTD.java +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/main/java/com/mycompany/app/UseThree.java diff --git a/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000..6348184 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst @@ -0,0 +1 @@ +com/mycompany/app/AppTest.class diff --git a/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..543454e --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/src/test/java/com/mycompany/app/AppTest.java diff --git a/usth/ICT2.2/labwork/2/my-app/target/my-app-1.0-SNAPSHOT.jar b/usth/ICT2.2/labwork/2/my-app/target/my-app-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..30b9fdc Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/my-app-1.0-SNAPSHOT.jar differ diff --git a/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/TEST-com.mycompany.app.AppTest.xml b/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/TEST-com.mycompany.app.AppTest.xml new file mode 100644 index 0000000..4041d13 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/TEST-com.mycompany.app.AppTest.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/com.mycompany.app.AppTest.txt b/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/com.mycompany.app.AppTest.txt new file mode 100644 index 0000000..1c0c529 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/surefire-reports/com.mycompany.app.AppTest.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: com.mycompany.app.AppTest +------------------------------------------------------------------------------- +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in com.mycompany.app.AppTest diff --git a/usth/ICT2.2/labwork/2/my-app/target/test-classes/com/mycompany/app/AppTest.class b/usth/ICT2.2/labwork/2/my-app/target/test-classes/com/mycompany/app/AppTest.class new file mode 100644 index 0000000..88a6c58 Binary files /dev/null and b/usth/ICT2.2/labwork/2/my-app/target/test-classes/com/mycompany/app/AppTest.class differ diff --git a/usth/ICT2.2/labwork/3/3_labwork3.pdf b/usth/ICT2.2/labwork/3/3_labwork3.pdf new file mode 100644 index 0000000..0c9c533 Binary files /dev/null and b/usth/ICT2.2/labwork/3/3_labwork3.pdf differ diff --git a/usth/ICT2.2/labwork/3/C++/README.md b/usth/ICT2.2/labwork/3/C++/README.md new file mode 100644 index 0000000..8cd208f --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/README.md @@ -0,0 +1,10 @@ +# Labwork 3: Implementations in C++ + +At this point I've come to realized that like Thanos, C++ is inevitable. +Despite my disgust toward the language (TBH I don't like Java any better), +I need to know it enough, at least at the read-only level. + +Compilation of tests could be done as follows (on Unix-like system of course, +since poor Windows users don't even have a standard C/C++ compiler): + + c++ *.cc diff --git a/usth/ICT2.2/labwork/3/C++/automobile.cc b/usth/ICT2.2/labwork/3/C++/automobile.cc new file mode 100644 index 0000000..d274a60 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/automobile.cc @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "automobile.h" + +using namespace std; + +const regex license_pattern ("[0-9A-Z]+"); + +void +Automobile::set_license (string s) +{ + smatch m; + if (!regex_match (s, m, license_pattern)) + throw invalid_argument{"invalid license plate"}; + license = s; +} + +void +Automobile::set_fuel (double x) +{ + if (x < 0) + throw invalid_argument{"negative fuel"}; + fuel = x; +} + +void +Automobile::set_speed (double x) +{ + speed = (x < 0) ? 0 : x; +} + +Automobile::Automobile (string l, double f, double s) +{ + set_license (l); + set_fuel (f); + set_speed (s); +} + +void +Automobile::accelerate (double v) +{ + if (v < 0) + throw invalid_argument{"negative acceleration"}; + if (fuel) + set_speed (speed + v); +} + +void +Automobile::decelerate (double v) +{ + if (v < 0) + throw invalid_argument{"negative deceleration"}; + set_speed (speed - v); +} diff --git a/usth/ICT2.2/labwork/3/C++/automobile.h b/usth/ICT2.2/labwork/3/C++/automobile.h new file mode 100644 index 0000000..0e65865 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/automobile.h @@ -0,0 +1,20 @@ +#include + +using namespace std; + +class Automobile +{ + double fuel; + double speed; + string license; +public: + Automobile (string, double, double); + string get_license () { return license; } + double get_fuel () { return fuel; } + double get_speed () { return speed; } + void set_license (string); + void set_fuel (double); + void set_speed (double); + void accelerate (double); + void decelerate (double); +}; diff --git a/usth/ICT2.2/labwork/3/C++/button-test.cc b/usth/ICT2.2/labwork/3/C++/button-test.cc new file mode 100644 index 0000000..7164b2f --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/button-test.cc @@ -0,0 +1,14 @@ +#include + +#include "button.h" + +using namespace std; + +int +main () +{ + Button butt ("foo", "bar"); + butt.depress (); + butt.undepress (); + cout << "button " << butt.label << " color " << butt.color << endl; +} diff --git a/usth/ICT2.2/labwork/3/C++/button.h b/usth/ICT2.2/labwork/3/C++/button.h new file mode 100644 index 0000000..92fa685 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/button.h @@ -0,0 +1,14 @@ +#include + +using namespace std; + +class Button +{ + bool state; +public: + string label; + string color; + Button (string l, string c) : label {l}, color {c}, state {false} {} + void depress () { state = true; } + void undepress () { state = false; } +}; diff --git a/usth/ICT2.2/labwork/3/C++/cow-test.cc b/usth/ICT2.2/labwork/3/C++/cow-test.cc new file mode 100644 index 0000000..ea7b86d --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/cow-test.cc @@ -0,0 +1,9 @@ +#include "cow.h" + +int +main () +{ + Cow cow ("foo", "bar", 7); + cow.age = -4; // some casting happens here + cow.moo (); +} diff --git a/usth/ICT2.2/labwork/3/C++/cow.cc b/usth/ICT2.2/labwork/3/C++/cow.cc new file mode 100644 index 0000000..84fabc8 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/cow.cc @@ -0,0 +1,11 @@ +#include + +#include "cow.h" + +using namespace std; + +void +Cow::moo () +{ + cout << "Moo...!" << endl; +} diff --git a/usth/ICT2.2/labwork/3/C++/cow.h b/usth/ICT2.2/labwork/3/C++/cow.h new file mode 100644 index 0000000..df7ffd9 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/cow.h @@ -0,0 +1,17 @@ +#include + +using namespace std; + +class Cow +{ + // The reason these have private access + // is that their no way a cow's name and breed can be changed. + string name; + string breed; +public: + unsigned age; + + Cow (string n, string b, unsigned a) : name {n}, breed {b}, age {a} {} + Cow (string n, string b) : name {n}, breed {b}, age {0} {} + void moo (); +}; diff --git a/usth/ICT2.2/labwork/3/C++/name-card-test.cc b/usth/ICT2.2/labwork/3/C++/name-card-test.cc new file mode 100644 index 0000000..b459d0d --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/name-card-test.cc @@ -0,0 +1,14 @@ +#include + +#include "name-card.h" + +using namespace std; + +int +main () +{ + NameCard card ("Foobar Baz", "420-69", "foo@bar.baz"); + cout << "Name: " << card.get_name () << endl + << "Phone: " << card.get_phone () << endl + << "Email: " << card.get_email () << endl; +} diff --git a/usth/ICT2.2/labwork/3/C++/name-card.cc b/usth/ICT2.2/labwork/3/C++/name-card.cc new file mode 100644 index 0000000..b6c0751 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/name-card.cc @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "name-card.h" + +using namespace std; + +const regex name_pattern ("[ A-Za-z]+"); +const regex phone_pattern ("[-0-9]+"); +// This should be good enough I guess? +const regex email_pattern ("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"); + +void +NameCard::set_name (string s) +{ + // I miss Raku so much + smatch m; + if (!regex_match (s, m, name_pattern)) + throw invalid_argument{"invalid name"}; + name = s; +} + +void +NameCard::set_phone (string s) +{ + smatch m; + if (!regex_match (s, m, phone_pattern)) + throw invalid_argument{"invalid number"}; + phone = s; +} + +void +NameCard::set_email (string s) +{ + smatch m; + if (!regex_match (s, m, email_pattern)) + throw invalid_argument{"invalid name"}; + email = s; +} + +NameCard::NameCard (string n, string p, string e) +{ + set_name (n); + set_phone (p); + set_email (e); +} diff --git a/usth/ICT2.2/labwork/3/C++/name-card.h b/usth/ICT2.2/labwork/3/C++/name-card.h new file mode 100644 index 0000000..08e76a7 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/name-card.h @@ -0,0 +1,18 @@ +#include + +using namespace std; + +class NameCard +{ + string name; + string phone; + string email; +public: + string get_name () { return name; } + string get_phone () { return phone; } + string get_email () { return email; } + void set_name (string); + void set_phone (string); + void set_email (string); + NameCard (string, string, string); +}; diff --git a/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc b/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc new file mode 100644 index 0000000..4833e70 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc @@ -0,0 +1,19 @@ +#include + +#include "shopping-cart.h" + +using namespace std; + +int +main () +{ + ShoppingCart cart; + cart.add_to_cart ("foo"); + cart.add_to_cart ("foo"); + cart.add_to_cart ("bar"); + cart.remove_from_cart ("baz"); + cout << cart.count ("foo") << ' ' + << cart.count ("bar") << ' ' + << cart.count ("baz") << endl; + cart.check_out (); +} diff --git a/usth/ICT2.2/labwork/3/C++/shopping-cart.h b/usth/ICT2.2/labwork/3/C++/shopping-cart.h new file mode 100644 index 0000000..c1450e7 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/shopping-cart.h @@ -0,0 +1,16 @@ +#include +#include + +using namespace std; + +class ShoppingCart +{ + // quite of an improvement over the Java version + unordered_map contents; +public: + void add_to_cart (string item) { contents[item]++; } + void remove_from_cart (string item) { contents[item] = 0; } + // to make this class useful anyhow + int count (string item) { return contents[item]; } + void check_out () { contents.clear (); } +}; diff --git a/usth/ICT2.2/labwork/3/C++/vector-test.cc b/usth/ICT2.2/labwork/3/C++/vector-test.cc new file mode 100644 index 0000000..647c8a6 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/vector-test.cc @@ -0,0 +1,20 @@ +#include + +#include "vector.h" + +using namespace std; + +int +main () +{ + Vector u (4, 20); + Vector v (6, 9); + cout << "u = (" << u.x << ", " << u.y << ")" << endl; + cout << "v = (" << v.x << ", " << v.y << ")" << endl; + Vector w {u + v}; + cout << "u + v = (" << w.x << ", " << w.y << ")" << endl; + w = u - v; + cout << "u - v = (" << w.x << ", " << w.y << ")" << endl; + w = u * v; + cout << "u * v = (" << w.x << ", " << w.y << ")" << endl; +} diff --git a/usth/ICT2.2/labwork/3/C++/vector.cc b/usth/ICT2.2/labwork/3/C++/vector.cc new file mode 100644 index 0000000..5ac56de --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/vector.cc @@ -0,0 +1,19 @@ +#include "vector.h" + +Vector +operator+ (Vector u, Vector v) +{ + return u += v; +} + +Vector +operator- (Vector u, Vector v) +{ + return u -= v; +} + +Vector +operator* (Vector u, Vector v) +{ + return u *= v; +} diff --git a/usth/ICT2.2/labwork/3/C++/vector.h b/usth/ICT2.2/labwork/3/C++/vector.h new file mode 100644 index 0000000..8433e79 --- /dev/null +++ b/usth/ICT2.2/labwork/3/C++/vector.h @@ -0,0 +1,17 @@ +class Vector +{ +public: + int x; + int y; + + Vector (int ex, int why) : x {ex}, y {why} {} + Vector () : x {0}, y{0} {} + + Vector& operator+= (Vector v) { x += v.x, y += v.y; return *this; } + Vector& operator-= (Vector v) { x -= v.x, y -= v.y; return *this; } + Vector& operator*= (Vector v) { x *= v.x, y *= v.y; return *this; } +}; + +Vector operator+ (Vector, Vector); +Vector operator- (Vector, Vector); +Vector operator* (Vector, Vector); diff --git a/usth/ICT2.2/labwork/3/Java/Automobile.java b/usth/ICT2.2/labwork/3/Java/Automobile.java new file mode 100644 index 0000000..14a82ef --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/Automobile.java @@ -0,0 +1,68 @@ +import java.util.regex.Pattern; + +class Automobile +{ + private static final Pattern licensePattern = Pattern.compile("[0-9A-Z]+"); + private double fuel; + private double speed; + private String license; + + public double getFuel() + { + return fuel; + } + + public double getSpeed() + { + return speed; + } + + public String getLicense() + { + return license; + } + + public void setFuel(double fuel) + { + if (fuel < 0) + throw new IllegalArgumentException( + "fuel must be nonnegative, instead got " + fuel); + this.fuel = fuel; + } + + public void setSpeed(double speed) + { + this.speed = Math.max(0, speed); + } + + public void setLicense(String license) + { + if (!licensePattern.matcher(license).matches()) + throw new IllegalArgumentException("invalid license: " + license); + this.license = license; + } + + public Automobile(double f, double s, String l) + { + setFuel(f); + setSpeed(s); + setLicense(l); + } + + public void accelerate(double v) + { + if (v < 0) + throw new IllegalArgumentException( + "acceleration must be nonnegative, instead got " + v); + if (fuel > 0) + setSpeed(speed + v); + } + + public void decelerate(double v) + { + if (v < 0) + throw new IllegalArgumentException( + "deceleration must be nonnegative, instead got " + v); + setSpeed(speed - v); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/Button.java b/usth/ICT2.2/labwork/3/Java/Button.java new file mode 100644 index 0000000..8a26b3f --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/Button.java @@ -0,0 +1,50 @@ +public class Button +{ + private String label; + private String color; + private boolean state; + + public Button(String label, String color) + { + this.label = label; + this.color = color; + this.state = false; + } + + public String toString() + { + return String.format("