2016-10-15 5 views
1

나는 거북이가 서쪽으로 이동해야하는 자바 프로그램을 작성하고 있지만, 지금까지 내가 원하는 것은 거북이가 원하는 방향으로 움직이지 않는다.자바 프로그래밍 : 거북이가 원하는 방향으로 움직이지 않는다

public void testMoveWest() 
    { 
     Turtle t=new Turtle(); 
     t.moveWest(); 
     t.moveWest(); 
     t.moveWest(); 
     t.moveWest(); 

     Assert.assertEquals(6, t.getX()); 
    } 

    @Grade(points=10) 
    @Test 

이 어떤 도움이 평가되고 : 나는 자식 배쉬에서이 방법을 실행하면 다음

package assignment; 

import java.text.MessageFormat; 
import java.util.Scanner; 

import java.awt.Point; 
import java.awt.Dimension; 
import java.awt.Rectangle; 

import java.time.LocalDate; 

/** 
* This class represents a turtle, like a Logo-like turtle, which keeps its position. 
* The turtle keeps its x and y position, and has methods moveNorth() etc which change it 
* Assume normal math coordinates and map convention, so as you move up (North) y increases, and as you move East x increases. 
* 
*/ 
class Turtle { 

    // HINT - you may want to have variables to keep the position. Keep these variables private, 
    private int x,y; 

    // TODO - The empty constructor initializes position to 10,10 
    public Turtle() { 
     this.x = 10; 
     this.y = 10; 
    } 

    public int getX() { 
     // TODO - implement this 
     return this.x; 
    } 
    public int getY() { 
     // TODO - implement this 
     return this.y; 
    } 

    public void moveNorth() { 
     // TODO - implement this. this increments the y coordinate by one unit 
     this.y += 1; 
    } 

    public void moveSouth() { 
     // TODO - implement this. this decrements the y coordinate by one unit 
     this.y -=1; 
    } 

    public void moveEast() { 
     // TODO - this increments the x coordinate by one unit 
     this.x +=1; 
    } 

    public void moveWest() { 
     // TODO - this decrements the x coordinate by one unit 
     this.x -=1; 
    } 

    public String toString() { 
     return "Turtle[x="+getX()+", y="+getY()+"]"; 
    } 

    public boolean equals(Turtle t) 
    { 
     // TODO - you need to implement this 
     // two turtles are equal if their X and Y coordinates are equal. 
     if (t == null) { 
      return false; 
     } 
     if (!(t instanceof Turtle)) { 
      return false; 
     } 
     return (x == ((Turtle) t).x && y == ((Turtle) t).y); 
    } 

    public void move(String direction) 
    { 
     // TODO - you need to implement this 
     // move to the right direction; direction can be North, South, East, West 
     moveEast(); 
    } 
} 

public class Assignment7 { 
    // TODO - you need to implement this. Move the given turtle to the West, n times 
    public static void moveTurtleWest(Turtle t, int n) 
    { 
     for(; n > t.getY();n--){ 
      t.moveWest(); 
      } 
    } 

    // TODO - you need to implement this. Move the given turtle to the East, n times 
    public static void moveTurtleEast(Turtle t, int n) 
    { 
    } 

    // TODO - you need to implement this. Move the given turtle to the North, n times 
    public static void moveTurtleNorth(Turtle t, int n) 
    { 
    } 

    // TODO - you need to implement this. Move the given turtle to the South, n times 
    public static void moveTurtleSouth(Turtle t, int n) 
    { 
    } 

    // TODO - you need to implement this. Move the turtle to the asked position, by calling MoveXXX etc 
    public static void moveTurtleTo(Turtle t, int x, int y) 
    { 

    } 
    public static void main(String[] args) { 
     // you can use this as you wish to test or exercise your function. Not graded. 
     Turtle t=new Turtle(); 
     moveTurtleTo(t,15,16); 
     System.out.println(t); 
    } 
} 

가 실행되는 코드입니다 : 여기
public static void moveTurtleWest(Turtle t, int n) 
    { 
     for(; n > t.getX();n--){ 
      t.moveWest(); 
      } 
    } 

내 코드의 나머지 부분입니다 포인트가 보상됩니다. 미리 감사드립니다.

+1

인가? 이 작업은 GUI (Swing) 인터페이스를 통해 수행됩니까? – ChiefTwoPencils

+0

아니요. GitBash에서 코드를 실행하면 10 포인트를 받게되므로 커맨드 라인을 통해 유효성 검사 및 컴파일러를 실행하고 있습니다. – rls1982

+1

그래서 통과하지 못했다고 스스로 언급 한 테스트가 있습니까? –

답변

2

아마도 올바른 : 그것은 당신이 기대하는 것보다 반대 방향

public static void moveTurtleWest(Turtle t, int n) { 
     for(int i=0; i <n;i++){ 
       t.moveWest(); 
      } 
} 
+0

Great Job !. 나는 모든 도움에 매우 감사한다! – rls1982

1

for 루프에있는 술어가 t.getY() 대신 t.getX()가 아니어야합니까? 루프

+0

예, 변경해야하지만 여전히 작동하지 않습니다. – rls1982

관련 문제