changed constructor location and points

This commit is contained in:
2018-03-01 17:14:01 +01:00
parent a5d8b12cdd
commit 41128b67a4
2 changed files with 47 additions and 59 deletions

View File

@@ -3,11 +3,14 @@ import java.io.Console;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(4/3);
System.out.println(4.0/3.0);
/*
System.out.println("Hello World!"); System.out.println("Hello World!");
Console console = System.console(); Console console = System.console();
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);*/
} }
private static int minRadius = 50; private static int minRadius = 50;

View File

@@ -3,11 +3,10 @@ import be.kuleuven.cs.som.annotate.*;
public class Worm { public class Worm {
// Location // Location
private double coordX; private double[] location;
private double coordY;
// Orientation // Orientation
private double orientation; private double direction;
// shape // shape
private double radius; private double radius;
@@ -22,26 +21,19 @@ public class Worm {
// name // name
private final String name; private final String name;
public Worm(double coordX, double coordY, double orientation, String name, double radius) throws IllegalArgumentException { public Worm(double[] location, double direction, String name, double radius) throws IllegalArgumentException {
this.coordX = coordX; this(location, direction, name, radius, 0.25);
this.coordY = coordY;
this.orientation = orientation;
this.radius = radius;
this.minRadius = 0.25;
int validValue = isValidName(name);
if (validValue != -1) {
throw new IllegalNameException(validValue, name);
}
this.name = name;
} }
public Worm(double coordX, double coordY, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { public Worm(double[] location, double direction, String name, double radius, double minRadius) throws IllegalArgumentException {
this.coordX = coordX; if ( location.length != 2) {
this.coordY = coordY; throw new IllegalArgumentException();
this.orientation = orientation; }
this.location = location;
this.direction = direction;
this.radius = radius; this.radius = radius;
this.minRadius = minRadius; this.minRadius = minRadius;
@@ -54,8 +46,8 @@ public class Worm {
/** /**
* *
* @param name * @param name ...
* @return * @return ...
*/ */
private int isValidName (String name) { private int isValidName (String name) {
@@ -74,42 +66,29 @@ public class Worm {
return -1; return -1;
} }
public double[] getLocation() {
public double getCoordX() { return this.location;
return this.coordX;
}
public void setCoordX(double coordX) {
this.coordX = coordX;
}
public double getCoordY() {
return this.coordY;
}
public void setCoordY(double coordY) {
this.coordY = coordY;
}
public double getOrientation() {
return this.orientation;
} }
/** /**
* @param orientation * @param direction
* | ...
* *
*/ */
public void setOrientation(double orientation) { public void setdirection(double direction) {
this.orientation = orientation; this.direction = direction;
}
private void setLocation(double[] direction) {
this.location = direction;
} }
public double getRadius() { public double getRadius() {
return this.radius; return this.radius;
} }
public void setRadius(double 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); throw new IllegalArgumentException("Radius is smaller than " + this.minRadius);
} }
@@ -160,10 +139,10 @@ public class Worm {
* the number of steps the worm should take * the number of steps the worm should take
* *
* @post the new x-coordinate schould be the old one plus the number of steps multiplied * @post the new x-coordinate schould be the old one plus the number of steps multiplied
* with the distance of a step in the x-orientation * with the distance of a step in the x-direction
* |new.CoordX = CoordX + NumberSteps * distanceX * |new.CoordX = CoordX + NumberSteps * distanceX
* @post the new y-coordinate schould be the old one plus the number of steps multiplied * @post the new y-coordinate schould be the old one plus the number of steps multiplied
* with the distance of a step in the y-orientation * with the distance of a step in the y-direction
* |new.CoordY = CoordY + NumberSteps * distanceY * |new.CoordY = CoordY + NumberSteps * distanceY
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
@@ -174,28 +153,34 @@ public class Worm {
if (numberSteps < 0) if (numberSteps < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
double distanceX = this.radius * Math.cos(this.orientation); double distanceX = this.radius * Math.cos(this.direction);
double distanceY = this.radius * Math.sin(this.orientation); double distanceY = this.radius * Math.sin(this.direction);
setCoordX(this.coordX + numberSteps * distanceX); this.location = new double[] {this.location[0] + numberSteps * distanceX, this.location[1] + numberSteps * distanceY};
setCoordY(this.coordY + numberSteps * distanceY);
} }
/** /**
* @param angleToAdd * @param angleToAdd
* the angle that must be added to the orientation * the angle that must be added to the direction
* *
* @pre the angle to add must be between 0 and 2pi (including 0) * @pre the angle to add must be between 0 and 2pi (including 0)
* |0 <= angleToAdd < 2pi * |0 <= angleToAdd < 2pi
* *
* @post the new orientation is the old orientation plus the given angle * @post the new direction is the old direction plus the given angle
* |new.orientation = orientation + angleToAdd * |new.direction = direction + angleToAdd
* @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) * @post the resulting angle (= the new direction) must be between 0 and 2pi (including 0)
* |0 <= new.orientation < 2pi * |0 <= new.direction < 2pi
*/ */
public void turn(double angleToAdd) { public void turn(double angleToAdd) {
assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI); assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI);
if (0 <= (this.orientation + angleToAdd) && (this.orientation + angleToAdd) < (2 * Math.PI)) if (0 <= (this.direction + angleToAdd) && (this.direction + angleToAdd) < (2 * Math.PI))
setOrientation(this.orientation + angleToAdd); setdirection(this.direction + angleToAdd);
setOrientation((this.orientation + angleToAdd)%(2 * Math.PI)); setdirection((this.direction + angleToAdd)%(2 * Math.PI));
}
private void updatePoints (double value) {
this.points -= (int) Math.round(Math.toDegrees(value) / 2);
} }
} }