2 Replies - 1181 Views - Last Post: 24 November 2010 - 06:38 PM Rate Topic: -----

#1 SummitWhiteMaro  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 08-November 10

Collisions.java programming questions.

Posted 24 November 2010 - 06:18 PM

Hey all, So I am completely lost in this program. QUESTION 1 I have is when I am trying to get it to detect when x1 == x2 && y1 == y2. Why does it never detect a collision, I know it is all chance and can just get further and further away (for example: if x1 always = positive and x2 always = negative). But I have run it a ton of times and when i use numbers as high as 100,000 steps it still never detects a collision.
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);
 */



Is This A Good Question/Topic? 0
  • +

Replies To: Collisions.java programming questions.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10666
  • View blog
  • Posts: 39,612
  • Joined: 27-December 08

Re: Collisions.java programming questions.

Posted 24 November 2010 - 06:20 PM

Moved to Java Help. Please reserve the Java Programmers subforum for topical, non-help related questions.
Was This Post Helpful? 0
  • +
  • -

#3 SummitWhiteMaro  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 08-November 10

Re: Collisions.java programming questions.

Posted 24 November 2010 - 06:38 PM

Ok sorry :oops:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1