many changes

This commit is contained in:
2018-03-07 05:21:19 +01:00
parent d594d7981b
commit 50f8424f1f
3 changed files with 143 additions and 66 deletions

View File

@@ -11,6 +11,9 @@ public class Main {
double input = Double.parseDouble(console.readLine("Enter your %d (th) passport number: ", 2)); double input = Double.parseDouble(console.readLine("Enter your %d (th) passport number: ", 2));
System.out.println(input); System.out.println(input);
System.out.println((int)input);*/ System.out.println((int)input);*/
System.out.println(8 + 11 % 2);
System.out.println((8 + 11) % 2);
} }
private static int minRadius = 50; private static int minRadius = 50;

View File

@@ -1,34 +1,61 @@
public class Tuple2<T1, T2> { import java.util.Objects;
import be.kuleuven.cs.som.annotate.*;
/**
* A tuple implementation
*
* @param <T1> first element type
* @param <T2> second element type
*
*
* @version 1.0
* @author Arthur Bols & Leen Dereu
*/
@Value
public class Tuple<T1, T2> {
public final T1 item1; public final T1 item1;
public final T2 item2; 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 <T1, T2> Tuple<T1, T2> create(T1 item1, T2 item2){
return new Tuple<>(item1, item2);
}
@Override @Override
public boolean equals(final Object obj) { public boolean equals(Object o) {
if (this == obj) if (this == o) return true;
return true; if (!(o instanceof Tuple)) return false;
if (obj == null) Tuple<?, ?> tuple = (Tuple<?, ?>) o;
return false; return Objects.equals(item1, tuple.item1) &&
if (!(obj instanceof Tuple2)) Objects.equals(item2, tuple.item2);
return false; }
final Tuple2<?, ?> other = (Tuple2<?, ?>) obj;
if (_1 == null) { @Override
if (other._1 != null) public int hashCode() {
return false;
} else if (!_1.equals(other._1)) return Objects.hash(item1, item2);
return false;
if (_2 == null) {
if (other._2 != null)
return false;
} else if (!_2.equals(other._2))
return false;
return true;
} }
} }

View File

@@ -30,29 +30,48 @@ public class Worm {
// region constructor // 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); 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) public Worm(double[] location, double orientation, String name, double radius, double minRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
if ( location.length != 2) { setLocation(location);
throw new IllegalArgumentException();
}
this.location = location;
this.orientation = orientation; this.orientation = orientation;
this.radius = radius;
this.minRadius = minRadius;
int validValue = isValidName(name); if (minRadius < 0)
if (validValue != -1) { throw new IllegalArgumentException("minRadius must be larger than 0"); // TODO add decent exception msg
throw new IllegalNameException(validValue, name);
} setRadius(radius);
this.minRadius = minRadius;
this.points = this.maxPoints;
int validName = isValidName(name);
if (validName != -1)
throw new IllegalNameException(validName, name);
this.name = name; this.name = name;
} }
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -66,10 +85,13 @@ public class Worm {
return this.location; return this.location;
} }
private void setLocation(double[] orientation) { private void setLocation(double[] location) throws IllegalArgumentException {
// TODO checking
this.location = orientation; if (location == null || location.length != 2)
throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
this.location = location;
} }
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -94,18 +116,19 @@ public class Worm {
// endregion // endregion
// region Shape mass/radius // region Shape mass/radius
//=================================================================================== //===================================================================================
public double getRadius() { public double getRadius() {
return this.radius; return this.radius;
} }
public void setRadius(double radius) throws IllegalArgumentException { public void setRadius(double radius) throws IllegalArgumentException {
if (radius < this.minRadius) { if (radius < this.minRadius)
throw new IllegalArgumentException("Radius is smaller than " + this.minRadius); throw new IllegalArgumentException("Radius is smaller than " + this.minRadius);
}
this.radius = radius; this.radius = radius;
setMass(radius); setMass(radius);
} }
@@ -114,7 +137,6 @@ public class Worm {
return this.minRadius; return this.minRadius;
} }
public double getMass() { public double getMass() {
return this.mass; return this.mass;
} }
@@ -136,26 +158,29 @@ public class Worm {
// region Points // region Points
//=================================================================================== //===================================================================================
public int getPoints() { public int getPoints() {
return this.points; return this.points;
} }
public void setPoints(int points) { public void setPoints(int points) {
if (points > this.maxPoints) { if (points > this.maxPoints)
points = this.maxPoints; points = this.maxPoints;
}
else if (points < 0) else if (points < 0)
points = 0; points = 0;
this.points = points; this.points = points;
} }
private void setMaxPoints(double maxPoints) { private void setMaxPoints(double maxPoints) {
this.maxPoints = (int) Math.round(maxPoints); this.maxPoints = (int) Math.ceil(maxPoints);
setPoints(this.points); 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) { public void move(int numberSteps) {
if (numberSteps < 0) 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 distanceX = this.radius * Math.cos(this.orientation);
double distanceY = this.radius * Math.sin(this.orientation); double distanceY = this.radius * Math.sin(this.orientation);
@@ -222,29 +251,47 @@ public class Worm {
this.location[0] + numberSteps * distanceX, this.location[0] + numberSteps * distanceX,
this.location[1] + numberSteps * distanceY 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 // 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
} }