diff options
Diffstat (limited to 'usth/ICT2.2/labwork')
105 files changed, 1708 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" 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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/1_labwork1-extra.pdf Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/2_labwork2.pdf Binary files differdiff --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 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.mycompany.app</groupId> + <artifactId>my-app</artifactId> + <version>1.0-SNAPSHOT</version> + + <name>my-app</name> + <!-- FIXME change it to the project's website --> + <url>http://www.example.com</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> + <plugins> + <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> + <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>3.1.0</version> + </plugin> + <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.0</version> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.22.1</version> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <version>2.5.2</version> + </plugin> + <plugin> + <artifactId>maven-deploy-plugin</artifactId> + <version>2.8.2</version> + </plugin> + <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <version>3.7.1</version> + </plugin> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <version>3.0.0</version> + </plugin> + </plugins> + </pluginManagement> + </build> +</project> 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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/App.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Beers.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/IsLeapYear.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Linear.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/MeanSTD.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/Quadratic.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/RandRange.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/TenHellos.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/classes/com/mycompany/app/UseThree.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/my-app-1.0-SNAPSHOT.jar Binary files differdiff --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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.mycompany.app.AppTest" time="0.019" tests="1" errors="0" skipped="0" failures="0"> + <properties> + <property name="awt.toolkit" value="sun.awt.X11.XToolkit"/> + <property name="java.specification.version" value="11"/> + <property name="sun.cpu.isalist" value=""/> + <property name="sun.jnu.encoding" value="UTF-8"/> + <property name="java.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/test-classes:/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/classes:/home/cnx/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/cnx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/> + <property name="java.vm.vendor" value="Debian"/> + <property name="sun.arch.data.model" value="64"/> + <property name="java.vendor.url" value="https://tracker.debian.org/openjdk-11"/> + <property name="user.timezone" value=""/> + <property name="java.vm.specification.version" value="11"/> + <property name="os.name" value="Linux"/> + <property name="sun.java.launcher" value="SUN_STANDARD"/> + <property name="user.country" value="US"/> + <property name="sun.boot.library.path" value="/usr/lib/jvm/java-11-openjdk-amd64/lib"/> + <property name="sun.java.command" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire/surefirebooter4171187558944627507.jar /home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire 2019-10-09T17-47-23_851-jvmRun1 surefire1053437032396327338tmp surefire_014762799941833427558tmp"/> + <property name="jdk.debug" value="release"/> + <property name="surefire.test.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/test-classes:/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/classes:/home/cnx/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/cnx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/> + <property name="sun.cpu.endian" value="little"/> + <property name="user.home" value="/home/cnx"/> + <property name="user.language" value="en"/> + <property name="java.specification.vendor" value="Oracle Corporation"/> + <property name="java.version.date" value="2019-10-15"/> + <property name="java.home" value="/usr/lib/jvm/java-11-openjdk-amd64"/> + <property name="file.separator" value="/"/> + <property name="basedir" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app"/> + <property name="java.vm.compressedOopsMode" value="32-bit"/> + <property name="line.separator" value=" "/> + <property name="java.specification.name" value="Java Platform API Specification"/> + <property name="java.vm.specification.vendor" value="Oracle Corporation"/> + <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/> + <property name="surefire.real.class.path" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app/target/surefire/surefirebooter4171187558944627507.jar"/> + <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/> + <property name="java.runtime.version" value="11.0.5-ea+6-post-Debian-2"/> + <property name="user.name" value="cnx"/> + <property name="path.separator" value=":"/> + <property name="os.version" value="4.19.0-5-amd64"/> + <property name="java.runtime.name" value="OpenJDK Runtime Environment"/> + <property name="file.encoding" value="UTF-8"/> + <property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/> + <property name="localRepository" value="/home/cnx/.m2/repository"/> + <property name="java.vendor.url.bug" value="https://bugs.debian.org/openjdk-11"/> + <property name="java.io.tmpdir" value="/tmp"/> + <property name="java.version" value="11.0.5-ea"/> + <property name="user.dir" value="/home/cnx/Documents/B2/ICT2.2/labwork/2/my-app"/> + <property name="os.arch" value="amd64"/> + <property name="java.vm.specification.name" value="Java Virtual Machine Specification"/> + <property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/> + <property name="sun.os.patch.level" value="unknown"/> + <property name="java.library.path" value="/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/> + <property name="java.vendor" value="Debian"/> + <property name="java.vm.info" value="mixed mode, sharing"/> + <property name="java.vm.version" value="11.0.5-ea+6-post-Debian-2"/> + <property name="sun.io.unicode.encoding" value="UnicodeLittle"/> + <property name="java.class.version" value="55.0"/> + </properties> + <testcase name="shouldAnswerWithTrue" classname="com.mycompany.app.AppTest" time="0.001"/> +</testsuite> \ 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 --- /dev/null +++ b/usth/ICT2.2/labwork/2/my-app/target/test-classes/com/mycompany/app/AppTest.class Binary files differdiff --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 --- /dev/null +++ b/usth/ICT2.2/labwork/3/3_labwork3.pdf Binary files differdiff --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++ <classname>*.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 <regex> +#include <stdexcept> +#include <string> + +#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 <string> + +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 <iostream> + +#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 <string> + +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 <iostream> + +#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 <string> + +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 <iostream> + +#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 <regex> +#include <stdexcept> +#include <string> + +#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 <string> + +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 <iostream> + +#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 <string> +#include <unordered_map> + +using namespace std; + +class ShoppingCart +{ + // quite of an improvement over the Java version + unordered_map<string, int> 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 <iostream> + +#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("<button %s with color %s and state %s>", + label, color, state); + } + + // To be honest these getters and setters are really redundant in this case + public String getLabel() + { + return label; + } + + public String getColor() + { + return color; + } + + public void setLabel(String label) + { + this.label = label; + } + + public void setColor(String color) + { + this.color = color; + } + + public void dePress() + { + this.state = true; + } + + public void unDepress() + { + this.state = false; + } +} diff --git a/usth/ICT2.2/labwork/3/Java/ButtonTest.java b/usth/ICT2.2/labwork/3/Java/ButtonTest.java new file mode 100644 index 0000000..51dd4d2 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/ButtonTest.java @@ -0,0 +1,12 @@ +class ButtonTest +{ + public static void main(String... args) + { + Button button = new Button("foo", "bar"); + System.out.println(button); + button.setLabel("fu"); + button.setColor("baz"); + button.dePress(); + System.out.println(button); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/Cow.java b/usth/ICT2.2/labwork/3/Java/Cow.java new file mode 100644 index 0000000..36a1051 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/Cow.java @@ -0,0 +1,36 @@ +public class Cow +{ + private String name; + private String breed; + private int age; + + public Cow(String name, String breed) + { + this(name, breed, 0); + } + + public Cow(String name, String breed, int age) + { + this.name = name; + this.breed = breed; + setAge(age); + } + + public static void moo() + { + System.out.println("Moo...!"); + } + + public int getAge() + { + return age; + } + + public void setAge(int age) + { + if (age < 0) + throw new IllegalArgumentException( + "age must be nonnegative, instead got " + age); + this.age = age; + } +} diff --git a/usth/ICT2.2/labwork/3/Java/CowTest.java b/usth/ICT2.2/labwork/3/Java/CowTest.java new file mode 100644 index 0000000..1a13f67 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/CowTest.java @@ -0,0 +1,9 @@ +class CowTest // there's no reason to import a test what-so-ever +{ + public static void main(String... args) + { + Cow cow = new Cow("foo", "bar", 7); + cow.setAge(-4); + cow.moo(); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/NameCard.java b/usth/ICT2.2/labwork/3/Java/NameCard.java new file mode 100644 index 0000000..165d5f9 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/NameCard.java @@ -0,0 +1,67 @@ +import java.util.regex.Pattern; + +public class NameCard +{ + private static final Pattern namePattern = Pattern.compile("[ A-Za-z]+"); + private static final Pattern phonePattern = Pattern.compile("[-0-9]*"); + // I have not learnt regex properly + private static final Pattern emailPattern = Pattern.compile( + "(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"); + + private String name; + private String phone; + private String email; + + public String getName() + { + return name; + } + + public String getPhone() + { + return phone; + } + + public String getEmail() + { + return email; + } + + public void setName(String name) + { + if (!namePattern.matcher(name).matches()) + throw new IllegalArgumentException("invalid name: " + name); + this.name = name; + } + + public void setPhone(String phone) + { + if (!phonePattern.matcher(phone).matches()) + throw new IllegalArgumentException("invalid phone number: " + phone); + this.phone = phone; + } + + public void setEmail(String email) + { + if (!emailPattern.matcher(email).matches()) + throw new IllegalArgumentException("invalid email: " + email); + this.email = email; + } + + public NameCard(String name) + { + this(name, "", ""); + } + + public NameCard(String name, String phone) + { + this(name, phone, ""); + } + + public NameCard(String name, String phone, String email) + { + setName(name); + setPhone(phone); + setEmail(email); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/NameCardTest.java b/usth/ICT2.2/labwork/3/Java/NameCardTest.java new file mode 100644 index 0000000..ed66343 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/NameCardTest.java @@ -0,0 +1,9 @@ +class NameCardTest +{ + public static void main(String... args) + { + NameCard card = new NameCard("Foobar Baz", "420-69", "foo@bar.baz"); + System.out.printf("Name: %s\nPhone: %s\nEmail: %s\n", + card.getName(), card.getPhone(), card.getEmail()); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/README.md b/usth/ICT2.2/labwork/3/Java/README.md new file mode 100644 index 0000000..f0c494a --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/README.md @@ -0,0 +1,10 @@ +# Labwork 3: Implementations in Java + +For the ease of typing, the test files are named `<classname>Test.java` +instead of `<classname>TestDrive.java`. +To rename them to the original requirement, use Perl `rename` and GNU `sed` + +```sh +rename s/Test/TestDrive/ *Test.java +sed -i s/Test/TestDrive/ *TestDrive.java +``` diff --git a/usth/ICT2.2/labwork/3/Java/ShoppingCart.java b/usth/ICT2.2/labwork/3/Java/ShoppingCart.java new file mode 100644 index 0000000..f5e23ad --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/ShoppingCart.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; + +public class ShoppingCart +{ + private List<String> cartContents = new ArrayList<String>(); + + public boolean addToCart(String content) + { + return cartContents.add(content); + } + + public boolean removeFromCart(String content) + { + return cartContents.remove(content); + } + + public void checkOut() + { + cartContents.clear(); + } + + public String toString() + { + return "Cart content(s): " + String.join(", ", cartContents); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java b/usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java new file mode 100644 index 0000000..315f9f4 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java @@ -0,0 +1,16 @@ +class ShoppingCartTest +{ + public static void main(String... args) + { + ShoppingCart cart = new ShoppingCart(); + System.out.println(cart); + cart.addToCart("foo"); + cart.addToCart("bar"); + cart.addToCart("baz"); + System.out.println(cart); + cart.removeFromCart("bar"); + System.out.println(cart); + cart.checkOut(); + System.out.println(cart); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/Vector.java b/usth/ICT2.2/labwork/3/Java/Vector.java new file mode 100644 index 0000000..3fc9137 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/Vector.java @@ -0,0 +1,39 @@ +public class Vector +{ + // There's nothing to validate + public int x; + public int y; + + public Vector() + { + this(0, 0); + } + + public Vector(int x, int y) + { + this.x = x; + this.y = y; + } + + public String toString() + { + // I feel bad writing this + return "(" + x + ", " + y + ")"; + } + + public Vector add(Vector other) + { + return new Vector(this.x + other.x, this.y + other.y); + } + + public Vector subtract(Vector other) + { + return new Vector(this.x - other.x, this.y - other.y); + } + + public Vector multiply(Vector other) + { + // instruction unclear, applying element-wise multiplication + return new Vector(this.x * other.x, this.y * other.y); + } +} diff --git a/usth/ICT2.2/labwork/3/Java/VectorTest.java b/usth/ICT2.2/labwork/3/Java/VectorTest.java new file mode 100644 index 0000000..4f0a4a3 --- /dev/null +++ b/usth/ICT2.2/labwork/3/Java/VectorTest.java @@ -0,0 +1,10 @@ +class VectorTest +{ + public static void main(String... args) + { + Vector u = new Vector(4, 20); + Vector v = new Vector(6, 9); + System.out.printf("u = %s\nv = %s\nu + v = %s\nu - v = %s\nu * v = %s\n", + u, v, u.add(v), u.subtract(v), u.multiply(v)); + } +} 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 diff --git a/usth/ICT2.2/labwork/5/Java/abstract/Circle.java b/usth/ICT2.2/labwork/5/Java/abstract/Circle.java new file mode 100644 index 0000000..3ee8c84 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/abstract/Circle.java @@ -0,0 +1,13 @@ +public class Circle extends Point +{ + public double r; + + public Circle(double x, double y, double r) + { + super(x, y); + this.r = r; + } + + public double calArea() { return r * r * Math.PI; } + public String getName() { return "Circle"; } +} diff --git a/usth/ICT2.2/labwork/5/Java/abstract/Cylinder.java b/usth/ICT2.2/labwork/5/Java/abstract/Cylinder.java new file mode 100644 index 0000000..8fa9e0c --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/abstract/Cylinder.java @@ -0,0 +1,14 @@ +public class Cylinder extends Circle +{ + public double h; + + public Cylinder(double x, double y, double r, double h) + { + super(x, y, r); + this.h = h; + } + + public double calArea() { return super.calArea()*2.0 + r*h*Math.PI*2.0; } + public double calVolume() { return super.calArea() * h; } + public String getName() { return "Circle"; } +} diff --git a/usth/ICT2.2/labwork/5/Java/abstract/Point.java b/usth/ICT2.2/labwork/5/Java/abstract/Point.java new file mode 100644 index 0000000..f6daf22 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/abstract/Point.java @@ -0,0 +1,13 @@ +public class Point extends Shape +{ + public double x; + public double y; + + public Point(double x, double y) + { + this.x = x; + this.y = y; + } + + public String getName() { return "Point"; } +} diff --git a/usth/ICT2.2/labwork/5/Java/abstract/Shape.java b/usth/ICT2.2/labwork/5/Java/abstract/Shape.java new file mode 100644 index 0000000..dd4124c --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/abstract/Shape.java @@ -0,0 +1,12 @@ +public abstract class Shape +{ + public double calArea() { return 0.0; } + public double calVolume() { return 0.0; } + public abstract String getName(); + + public String toString() + { + return String.format("%s of area %g and volume %g", + getName(), calArea(), calVolume()); + } +} diff --git a/usth/ICT2.2/labwork/5/Java/abstract/Test.java b/usth/ICT2.2/labwork/5/Java/abstract/Test.java new file mode 100644 index 0000000..b0502f0 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/abstract/Test.java @@ -0,0 +1,11 @@ +import static java.util.Arrays.asList; + +class Test +{ + public static void main(String... args) + { + var array = asList(new Point(1.0, 2.0), new Circle(3.0, 4.0, 5.0), + new Cylinder(6.0, 7.0, 8.0, 9.0)); + array.forEach(System.out::println); + } +} diff --git a/usth/ICT2.2/labwork/5/Java/interface/Circle.java b/usth/ICT2.2/labwork/5/Java/interface/Circle.java new file mode 100644 index 0000000..3ee8c84 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/interface/Circle.java @@ -0,0 +1,13 @@ +public class Circle extends Point +{ + public double r; + + public Circle(double x, double y, double r) + { + super(x, y); + this.r = r; + } + + public double calArea() { return r * r * Math.PI; } + public String getName() { return "Circle"; } +} diff --git a/usth/ICT2.2/labwork/5/Java/interface/Cylinder.java b/usth/ICT2.2/labwork/5/Java/interface/Cylinder.java new file mode 100644 index 0000000..8fa9e0c --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/interface/Cylinder.java @@ -0,0 +1,14 @@ +public class Cylinder extends Circle +{ + public double h; + + public Cylinder(double x, double y, double r, double h) + { + super(x, y, r); + this.h = h; + } + + public double calArea() { return super.calArea()*2.0 + r*h*Math.PI*2.0; } + public double calVolume() { return super.calArea() * h; } + public String getName() { return "Circle"; } +} diff --git a/usth/ICT2.2/labwork/5/Java/interface/Point.java b/usth/ICT2.2/labwork/5/Java/interface/Point.java new file mode 100644 index 0000000..90b2569 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/interface/Point.java @@ -0,0 +1,21 @@ +public class Point implements Shape +{ + public double x; + public double y; + + public Point(double x, double y) + { + this.x = x; + this.y = y; + } + + public double calArea() { return 0.0; } + public double calVolume() { return 0.0; } + public String getName() { return "Point"; } + + public String toString() + { + return String.format("%s of area %g and volume %g", + getName(), calArea(), calVolume()); + } +} diff --git a/usth/ICT2.2/labwork/5/Java/interface/Shape.java b/usth/ICT2.2/labwork/5/Java/interface/Shape.java new file mode 100644 index 0000000..1eb33fa --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/interface/Shape.java @@ -0,0 +1,6 @@ +interface Shape +{ + public double calArea(); + public double calVolume(); + public String getName(); +} diff --git a/usth/ICT2.2/labwork/5/Java/interface/Test.java b/usth/ICT2.2/labwork/5/Java/interface/Test.java new file mode 100644 index 0000000..b0502f0 --- /dev/null +++ b/usth/ICT2.2/labwork/5/Java/interface/Test.java @@ -0,0 +1,11 @@ +import static java.util.Arrays.asList; + +class Test +{ + public static void main(String... args) + { + var array = asList(new Point(1.0, 2.0), new Circle(3.0, 4.0, 5.0), + new Cylinder(6.0, 7.0, 8.0, 9.0)); + array.forEach(System.out::println); + } +} diff --git a/usth/ICT2.2/labwork/5/virtual.cc b/usth/ICT2.2/labwork/5/virtual.cc new file mode 100644 index 0000000..23ae78a --- /dev/null +++ b/usth/ICT2.2/labwork/5/virtual.cc @@ -0,0 +1,61 @@ +#include <cmath> +#include <iostream> +#include <memory> +#include <string> +#include <vector> + +using namespace std; + +static constexpr double PI = acos (-1.0); + +class Shape +{ +public: + virtual string get_name () const = 0; + + virtual double cal_area () const { return 0.0; } + virtual double cal_vol () const { return 0.0; } +}; + +class Point : public Shape +{ +public: + double x, y; + Point (double a, double b) : x {a}, y {b} {} + + string get_name () const override { return "Point"; } +}; + +class Circle : public Shape +{ +public: + double x, y, r; + Circle (double a, double b, double c) : x {a}, y {b}, r {c} {} + + string get_name () const override { return "Circle"; } + double cal_area () const override { return r * r * PI; } +}; + +class Cylinder : public Shape +{ +public: + double x, y, r, h; + Cylinder (double a, double b, double c, double d) + : x {a}, y {b}, r {c}, h {d} {} + + string get_name () const override { return "Cylinder"; } + double cal_area () const override { return (r + h) * r * PI * 2.0; } + double cal_vol () const override { return r * r * h * PI; } +}; + +int +main () +{ + vector<unique_ptr<Shape>> v; + v.push_back (make_unique<Point> (1.0, 2.0)); + v.push_back (make_unique<Circle> (3.0, 4.0, 5.0)); + v.push_back (make_unique<Cylinder> (6.0, 7.0, 8.0, 9.0)); + for (auto& p : v) + cout << p->get_name () << " of area " << p->cal_area () + << " and volume " << p->cal_vol () << endl; +} |