1 Replies - 310 Views - Last Post: 08 September 2012 - 12:23 PM Rate Topic: ***-- 2 Votes

#1 In.Co  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-September 12

Applying turn method for bug moving on a 2-D grid.

Posted 08 September 2012 - 12:04 PM

The program calls for a bug moving in a 2-d grid and printing out the x & y coordinates (position) of the bug at various points. In the beginning the bug is facing towards the positive x -axis and is located at the origin (0,0). When the move method is executed it moves one unit, now at position (1,0). However, after the turn method is called the move method still only moves the x position. Is there a way to move the y position when it is turned and not x position?

Here is the tester code:
public class Bug2Tester
{
   public static void main(String[] args)
   {
      Bug2 superBug = new Bug2(); // Position (0, 0)
      System.out.println(superBug.getX());
      System.out.println("Expected: 0");      
      System.out.println(superBug.getY());
      System.out.println("Expected: 0");              
      
      System.out.println("");
      
      superBug.move(); // Position (1, 0)
      System.out.println(superBug.getX());
      System.out.println("Expected: 1");      
      System.out.println(superBug.getY());
      System.out.println("Expected: 0");              
      
      System.out.println("");
     
      superBug.move(); // Position (2, 0)
      System.out.println(superBug.getX());
      System.out.println("Expected: 2");      
      System.out.println(superBug.getY());
      System.out.println("Expected: 0");         
      
      System.out.println("");
     
      superBug.turn(); // Position still (2, 0)
      System.out.println(superBug.getX());
      System.out.println("Expected: 2");      
      System.out.println(superBug.getY());
      System.out.println("Expected: 0");              
     
      System.out.println("");
      
      superBug.move(); // Position (2, -1)
      System.out.println(superBug.getX());
      System.out.println("Expected: 2");      
      System.out.println(superBug.getY());
      System.out.println("Expected: -1");                    
   }

---------------------------------------------------------
here is my bug class code 



public class Bug2 {


int newX; 
int newY;
int x; 
int y;

public Bug2 ()
{

}

public void move()
{
	x = x+1; 
	
}

 public void turn () 
{
	 newX = y;
	 newY = -x;
	 
}

public int getX()
{
	return x;
}

public int getY()
{
	return y; 
}


}


Here is a rotation formula our professor provided for the turn method. Attached Image

This post has been edited by macosxnerd101: 08 September 2012 - 12:25 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Applying turn method for bug moving on a 2-D grid.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Applying turn method for bug moving on a 2-D grid.

Posted 08 September 2012 - 12:23 PM

Think of your professor's formula for the turn() method as a state transition diagram. There are clever ways to program these things, but think of it first using brute-force:

When you enter the turn() method:

if dX = 1, dX = 0 and dY = -1
if dY = -1, dX = -1 and dY = 0
if dX = -1, dX = 0 and dY = 1
if dY = 1, dX = 1 and dY = 0

Does that help? After you think about the brute-force method, a more clever way to program it may occur to you. If not, no big deal.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1