diff --git a/src/Main.java b/src/Main.java index 168d2c7..e7ddd65 100644 --- a/src/Main.java +++ b/src/Main.java @@ -11,6 +11,9 @@ public class Main { double input = Double.parseDouble(console.readLine("Enter your %d (th) passport number: ", 2)); System.out.println(input); System.out.println((int)input);*/ + + System.out.println(8 + 11 % 2); + System.out.println((8 + 11) % 2); } private static int minRadius = 50; diff --git a/src/Tuple.java b/src/Tuple.java index 16cb8f0..8d29dfe 100644 --- a/src/Tuple.java +++ b/src/Tuple.java @@ -1,34 +1,61 @@ -public class Tuple2 { +import java.util.Objects; +import be.kuleuven.cs.som.annotate.*; + + +/** + * A tuple implementation + * + * @param first element type + * @param second element type + * + * + * @version 1.0 + * @author Arthur Bols & Leen Dereu + */ +@Value +public class Tuple { + public final T1 item1; public final T2 item2; - public Tuple2(T1 x, T2 y) { - this.item1 = x; - this.item2 = y; + + /** + * + * @param item1 + * The first element for this tuple + * @param item2 + * The second element for this tuple + */ + private Tuple(T1 item1, T2 item2) { + + if (item1 == null) + throw new IllegalArgumentException("item1 may not be null"); + if (item2 == null) + throw new IllegalArgumentException("item2 may not be null"); + + this.item1 = item1; + this.item2 = item2; } + @Immutable + public static Tuple create(T1 item1, T2 item2){ + return new Tuple<>(item1, item2); + } @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Tuple2)) - return false; - final Tuple2 other = (Tuple2) obj; - if (_1 == null) { - if (other._1 != null) - return false; - } else if (!_1.equals(other._1)) - return false; - if (_2 == null) { - if (other._2 != null) - return false; - } else if (!_2.equals(other._2)) - return false; - return true; + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Tuple)) return false; + Tuple tuple = (Tuple) o; + return Objects.equals(item1, tuple.item1) && + Objects.equals(item2, tuple.item2); + } + + @Override + public int hashCode() { + + return Objects.hash(item1, item2); } } diff --git a/src/Worm.java b/src/Worm.java index 8b3c858..1b43e7d 100644 --- a/src/Worm.java +++ b/src/Worm.java @@ -30,29 +30,48 @@ public class Worm { // region constructor //=================================================================================== - public Worm(double[] location, double orientation, String name, double radius) throws IllegalArgumentException { + + /** + * + * @param location ... + * @param orientation ... + * @param name ... + * @param radius ... + */ + public Worm(double[] location, double orientation, String name, double radius) { this(location, orientation, name, radius, 0.25); } + /** + * + * @param location ... + * @param orientation ... + * @param name ... + * @param radius ... + * @param minRadius ... + * @throws IllegalArgumentException ... + */ public Worm(double[] location, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { - if ( location.length != 2) { - throw new IllegalArgumentException(); - } + setLocation(location); - this.location = location; this.orientation = orientation; - this.radius = radius; - this.minRadius = minRadius; - int validValue = isValidName(name); - if (validValue != -1) { - throw new IllegalNameException(validValue, name); - } + if (minRadius < 0) + throw new IllegalArgumentException("minRadius must be larger than 0"); // TODO add decent exception msg + + setRadius(radius); + this.minRadius = minRadius; + this.points = this.maxPoints; + + int validName = isValidName(name); + if (validName != -1) + throw new IllegalNameException(validName, name); this.name = name; } + //=================================================================================== // endregion @@ -66,10 +85,13 @@ public class Worm { return this.location; } - private void setLocation(double[] orientation) { - // TODO checking - this.location = orientation; + private void setLocation(double[] location) throws IllegalArgumentException { + + if (location == null || location.length != 2) + throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg + this.location = location; } + //=================================================================================== // endregion @@ -94,18 +116,19 @@ public class Worm { // endregion + + // region Shape mass/radius //=================================================================================== - public double getRadius() { return this.radius; } public void setRadius(double radius) throws IllegalArgumentException { - if (radius < this.minRadius) { + if (radius < this.minRadius) throw new IllegalArgumentException("Radius is smaller than " + this.minRadius); - } + this.radius = radius; setMass(radius); } @@ -114,7 +137,6 @@ public class Worm { return this.minRadius; } - public double getMass() { return this.mass; } @@ -136,26 +158,29 @@ public class Worm { // region Points //=================================================================================== - public int getPoints() { return this.points; } public void setPoints(int points) { - if (points > this.maxPoints) { + if (points > this.maxPoints) points = this.maxPoints; - } else if (points < 0) points = 0; + this.points = points; } private void setMaxPoints(double maxPoints) { - this.maxPoints = (int) Math.round(maxPoints); + this.maxPoints = (int) Math.ceil(maxPoints); setPoints(this.points); } - private void updatePoints (double value) { + private void subtractPoints (int value) { - this.points -= (int) Math.round(Math.toDegrees(value) / 2); + setPoints(this.points - value); + } + private void subtractPoints (double angle) { + + setPoints(this.points - (int) Math.ceil(Math.toDegrees(angle) / 6)); } //=================================================================================== @@ -214,7 +239,11 @@ public class Worm { */ public void move(int numberSteps) { if (numberSteps < 0) - throw new IllegalArgumentException(); + throw new IllegalArgumentException(); // TODO add decent exception msg + + int cost = (int) Math.ceil(Math.abs(Math.cos(this.orientation)) + Math.abs(4 * Math.sin(this.orientation))); + if (cost > this.points) + throw new IllegalArgumentException(); // TODO add decent exception msg double distanceX = this.radius * Math.cos(this.orientation); double distanceY = this.radius * Math.sin(this.orientation); @@ -222,29 +251,47 @@ public class Worm { this.location[0] + numberSteps * distanceX, this.location[1] + numberSteps * distanceY }; - } + subtractPoints(cost); - /** - * @param angleToAdd - * the angle that must be added to the orientation - * - * @pre the angle to add must be between 0 and 2pi (including 0) - * |0 <= angleToAdd < 2pi - * - * @post the new orientation is the old orientation plus the given angle - * |new.orientation = orientation + angleToAdd - * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) - * |0 <= new.orientation < 2pi - */ - public void turn(double angleToAdd) { - assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI); - if (0 <= (this.orientation + angleToAdd) && (this.orientation + angleToAdd) < (2 * Math.PI)) - setOrientation(this.orientation + angleToAdd); - setOrientation((this.orientation + angleToAdd)%(2 * Math.PI)); } //=================================================================================== // endregion + // region turn + //=================================================================================== + + /** + * @param angle + * the angle that must be added to the orientation + * + * @pre the angle to add must be between 0 and 2pi (including 0) + * |0 <= angle < 2pi + * + * @post the new orientation is the old orientation plus the given angle + * |new.orientation = orientation + angle + * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) + * |0 <= new.orientation < 2pi + */ + public void turn(double angle) { + assert 0 <= angle && angle < (2 * Math.PI); + + setOrientation((this.orientation + angle) % (2 * Math.PI)); + subtractPoints(angle); + } + + //=================================================================================== + // endregion + + + // region Jump + //=================================================================================== + + + + + + //=================================================================================== + // endregion }