QUESTION 2 I have is once this is working I have to create a new method within the driver to determine it (I have the code started in a block comment at the end) what does this actually look like, I am having trouble expressing in words (so i can make pseudocode or write it out) what it is that I am supposed to include.
QUESTION 3 I have is then I am supposed to return a maxDistance from within the class, but how do I return a max distance when I have two separate objects created in the driver that calls the same getX() and getY() methods?
QUESTION 4 I have is, are my getX() and getY() methods within my class actually written right? Is this why the program doesnt detect a collision ever?
If you have any questions for me I will answer asap. Thanks for the help!
This is a take off of the drunkard walk or w/e that one is. Below is my code for the class and the Collisions.java driver.
/////////////////// Class \\\\\\\\\\\\\\\\\\\\
import java.util.Random;
public class RandomWalk {
private int x, y;
private int maxSteps;
private int currentSteps;
private int boundary;
private int maxDistance;
private int max;
// Constructor that takes in only the maximum amount of steps and the
// boundary, sets the
// starting location at 0,0.
public RandomWalk(int steps, int edge) {
maxSteps = steps;
boundary = edge;
x = 0;
y = 0;
currentSteps = 0;
maxDistance = 0;
}
// Constructor that takes in the starting location, maximum amount of steps,
// and the boundary.
public RandomWalk(int steps, int edge, int startX, int startY) {
maxSteps = steps;
boundary = edge;
x = startX;
y = startY;
currentSteps = 0;
maxDistance = 0;
}
// Randomly takes a step and also records the amount of steps taken
// by incrementing currentSteps each time a step is taken.
public void takeStep() {
Random numGen = new Random();
int direction;
direction = numGen.nextInt(4) + 1;
switch (direction) {
case 1:
x += 1; // right
currentSteps += 1; // increment steps
break;
case 2:
x -= 1; // left
currentSteps += 1; // increment steps
break;
case 3:
y += 1; // up
currentSteps += 1; // increment steps
break;
case 4:
y -= 1; // down
currentSteps += 1; // increment steps
break;
}
}
public boolean moreSteps() {
if (currentSteps < maxSteps)
return true;
else
return false;
}
public boolean inBounds() {
return (x <= boundary && y <= boundary && x >= -boundary && y >= -boundary);
}
public void walk() {
max = 0;
do {
takeStep();
if (inBounds() == false) {
max += 1;
}
} while (moreSteps() == true);
}
public String toString() {
return "Steps: " + currentSteps + "; Position: (" + getX() + ","
+ getY() + ")";
}
public int getY() {
return y;
}
public int getX() {
return x;
}
private int maxDistance(int num1, int num2) {
num1 = getX();
num2 = getX();
if (num1 > num2)
maxDistance = num1;
else if (num1 < num2)
maxDistance = num2;
else if (num1 == num2)
maxDistance = num1;
return maxDistance;
}
}
/////////////////// Driver \\\\\\\\\\\\\\\\\\\
public class Collisions {
private static int maxStep = 100000, bounds = 2000000;
private static RandomWalk collider = new RandomWalk(maxStep, bounds, -3, 0);
private static RandomWalk collider2 = new RandomWalk(maxStep, bounds, 3, 0);
public static void main(String[] args) {
int x1, x2, y1, y2;
String group1, group2;
int count = 0;
collider.walk();
collider2.walk();
x1 = collider.getX();
x2 = collider2.getX();
y1 = collider.getY();
y2 = collider2.getY();
if (x1 == x2 && y1 == y2) {
count += 1;
System.out.println("Collision detected");
}
System.out.println(x1 + " " + x2 + " " + y1 + " " + y2);
System.out.println(count + " collisions.");
}
}
/*
* public static boolean samePosition (RandomWalk p1, RandomWalk p2) { p1 =
* collider.getX(); max()
*
* return (p1 == p2);
*/

New Topic/Question
Reply




MultiQuote






|