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 static void main(String[] args) {
System.out.println(4/3);
System.out.println(4.0/3.0);
/*
System.out.println("Hello World!");
Console console = System.console();
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((int)input);*/
}
private static int minRadius = 50;

View File

@@ -3,11 +3,10 @@ import be.kuleuven.cs.som.annotate.*;
public class Worm {
// Location
private double coordX;
private double coordY;
private double[] location;
// Orientation
private double orientation;
private double direction;
// shape
private double radius;
@@ -22,26 +21,19 @@ public class Worm {
// 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.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;
this(location, direction, name, radius, 0.25);
}
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;
this.coordY = coordY;
this.orientation = orientation;
if ( location.length != 2) {
throw new IllegalArgumentException();
}
this.location = location;
this.direction = direction;
this.radius = radius;
this.minRadius = minRadius;
@@ -54,8 +46,8 @@ public class Worm {
/**
*
* @param name
* @return
* @param name ...
* @return ...
*/
private int isValidName (String name) {
@@ -74,42 +66,29 @@ public class Worm {
return -1;
}
public double getCoordX() {
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;
public double[] getLocation() {
return this.location;
}
/**
* @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() {
return this.radius;
}
public void setRadius(double radius) {
public void setRadius(double radius) throws IllegalArgumentException {
if (radius < 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
*
* @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
* @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
*
* @throws IllegalArgumentException
@@ -174,28 +153,34 @@ public class Worm {
if (numberSteps < 0)
throw new IllegalArgumentException();
double distanceX = this.radius * Math.cos(this.orientation);
double distanceY = this.radius * Math.sin(this.orientation);
setCoordX(this.coordX + numberSteps * distanceX);
setCoordY(this.coordY + numberSteps * distanceY);
double distanceX = this.radius * Math.cos(this.direction);
double distanceY = this.radius * Math.sin(this.direction);
this.location = new double[] {this.location[0] + numberSteps * distanceX, this.location[1] + numberSteps * distanceY};
}
/**
* @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)
* |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
* @post the new direction is the old direction plus the given angle
* |new.direction = direction + angleToAdd
* @post the resulting angle (= the new direction) must be between 0 and 2pi (including 0)
* |0 <= new.direction < 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));
if (0 <= (this.direction + angleToAdd) && (this.direction + angleToAdd) < (2 * Math.PI))
setdirection(this.direction + angleToAdd);
setdirection((this.direction + angleToAdd)%(2 * Math.PI));
}
private void updatePoints (double value) {
this.points -= (int) Math.round(Math.toDegrees(value) / 2);
}
}