From 41128b67a499e6b9912ff871b8fec8e6e5b9b1d6 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Thu, 1 Mar 2018 17:14:01 +0100 Subject: [PATCH] changed constructor location and points --- src/Main.java | 5 ++- src/Worm.java | 101 +++++++++++++++++++++----------------------------- 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/src/Main.java b/src/Main.java index 89a9d64..168d2c7 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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; diff --git a/src/Worm.java b/src/Worm.java index df20aa2..10cc066 100644 --- a/src/Worm.java +++ b/src/Worm.java @@ -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); } }