small changes

This commit is contained in:
2018-03-07 21:48:28 +01:00
parent 1136a8ddf9
commit e0ac1d1958
2 changed files with 28 additions and 17 deletions

View File

@@ -12,6 +12,9 @@ public class Main {
System.out.println(input); System.out.println(input);
System.out.println((int)input);*/ System.out.println((int)input);*/
System.out.println(1/2);
System.out.println(1.0/2.0);
System.out.println(8 + 11 % 2); System.out.println(8 + 11 % 2);
System.out.println((8 + 11) % 2); System.out.println((8 + 11) % 2);
} }

View File

@@ -1,3 +1,5 @@
package worms.model;
import be.kuleuven.cs.som.annotate.*; import be.kuleuven.cs.som.annotate.*;
public class Worm { public class Worm {
@@ -37,7 +39,7 @@ public class Worm {
/** /**
* this variable contains the minimum value of the radius * this variable contains the minimum value of the radius
*/ */
private final double minRadius; private final double minimumRadius;
/** /**
* this variable contains the mass of the worm * this variable contains the mass of the worm
@@ -84,21 +86,21 @@ public class Worm {
* @param orientation ... * @param orientation ...
* @param name ... * @param name ...
* @param radius ... * @param radius ...
* @param minRadius ... * @param minimumRadius ...
* @throws IllegalArgumentException ... * @throws IllegalArgumentException ...
*/ */
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minRadius) public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minimumRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
setLocation(location); setLocation(location);
this.orientation = orientation; setOrientation(orientation);
if (minRadius < 0) if (!isValidMininmumRadius(minimumRadius))
throw new IllegalArgumentException("minRadius must be larger than 0"); // TODO add decent exception msg throw new IllegalArgumentException("Invalid minimum radius"); // TODO add decent exception msg
setRadius(radius); setRadius(radius);
this.minRadius = minRadius; this.minimumRadius = minimumRadius;
this.points = this.maxPoints; this.points = this.maxPoints;
int validName = isValidName(name); int validName = isValidName(name);
@@ -137,7 +139,7 @@ public class Worm {
*/ */
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException { private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException {
if (location.equals(null)) if (location == null || Double.isNaN(location.item1) || Double.isNaN(location.item2))
throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
this.location = location; this.location = location;
} }
@@ -159,8 +161,8 @@ public class Worm {
* @post the new orientation of the worm must be equal to the given orientation * @post the new orientation of the worm must be equal to the given orientation
* |new.getOrientation() == orientation * |new.getOrientation() == orientation
*/ */
public void setOrientation(double orientation) { private void setOrientation(double orientation) {
// TODO nominal checking
this.orientation = orientation; this.orientation = orientation;
} }
@@ -205,8 +207,8 @@ public class Worm {
* |! isValidRadius(radius) * |! isValidRadius(radius)
*/ */
public void setRadius(double radius) throws IllegalArgumentException { public void setRadius(double radius) throws IllegalArgumentException {
if (radius < this.minRadius) if (radius < this.minimumRadius || Double.isNaN(radius))
throw new IllegalArgumentException("Radius is smaller than " + this.minRadius); throw new IllegalArgumentException("Invalid radius");
this.radius = radius; this.radius = radius;
setMass(radius); setMass(radius);
@@ -217,8 +219,12 @@ public class Worm {
* the minimum radius of the worm expresses the minimum length * the minimum radius of the worm expresses the minimum length
* of half of the width of the worm * of half of the width of the worm
*/ */
public double getMinRadius() { public double getMinimumRadius() {
return this.minRadius; return this.minimumRadius;
}
private boolean isValidMininmumRadius(double radius) {
return !Double.isNaN(radius) && radius > 0;
} }
/** /**
@@ -303,7 +309,7 @@ public class Worm {
* the new name for the worm * the new name for the worm
* @post the new name of the worm is equal to the given name * @post the new name of the worm is equal to the given name
* |new.GetName() == name * |new.GetName() == name
* @throws IllegalNameException(validName, name) * @throws IllegalNameException
* the given name is not a valid name for any worm * the given name is not a valid name for any worm
* |! isValidName(name) * |! isValidName(name)
*/ */
@@ -495,10 +501,12 @@ public class Worm {
* @return * @return
*/ */
public Tuple<Double, Double> jumpStep(double deltaTime) { public Tuple<Double, Double> jumpStep(double deltaTime) {
if (Double.isNaN(deltaTime)) {
throw new IllegalArgumentException();
}
return Tuple.create(oldLocation.item1 + this.jumpVelocity * Math.cos(this.orientation) * deltaTime, return Tuple.create(oldLocation.item1 + this.jumpVelocity * Math.cos(this.orientation) * deltaTime,
oldLocation.item2 + this.jumpVelocity * Math.sin(this.orientation) * deltaTime - (1/2) * G * Math.pow(deltaTime, 2)); oldLocation.item2 + this.jumpVelocity * Math.sin(this.orientation) * deltaTime - 0.5 * G * Math.pow(deltaTime, 2));
} }
/** /**