about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/3/Java
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.2/labwork/3/Java')
-rw-r--r--usth/ICT2.2/labwork/3/Java/Automobile.java68
-rw-r--r--usth/ICT2.2/labwork/3/Java/Button.java50
-rw-r--r--usth/ICT2.2/labwork/3/Java/ButtonTest.java12
-rw-r--r--usth/ICT2.2/labwork/3/Java/Cow.java36
-rw-r--r--usth/ICT2.2/labwork/3/Java/CowTest.java9
-rw-r--r--usth/ICT2.2/labwork/3/Java/NameCard.java67
-rw-r--r--usth/ICT2.2/labwork/3/Java/NameCardTest.java9
-rw-r--r--usth/ICT2.2/labwork/3/Java/README.md10
-rw-r--r--usth/ICT2.2/labwork/3/Java/ShoppingCart.java27
-rw-r--r--usth/ICT2.2/labwork/3/Java/ShoppingCartTest.java16
-rw-r--r--usth/ICT2.2/labwork/3/Java/Vector.java39
-rw-r--r--usth/ICT2.2/labwork/3/Java/VectorTest.java10
12 files changed, 353 insertions, 0 deletions
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));
+  }
+}