日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Katas编写的Java教程:Mars Rover

發布時間:2023/12/3 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Katas编写的Java教程:Mars Rover 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編程kata是一種練習,可以幫助程序員通過練習和重復練習來磨練自己的技能。

本文是“ 通過Katas進行Java教程 ”系列的一部分。

本文假定讀者已經具有Java的經驗,熟悉單元測試的基礎知識,并且知道如何從他最喜歡的IDE(我是IntelliJ IDEA )運行它們。

下面顯示證明解決方案正確的測試。 解決此問題的推薦方法是使用測試驅動的開發方法(編寫第一個測試的實現,確認它通過并轉到下一個測試)。 一旦所有測試通過,就可以認為解決了問題。 有關最佳做法的更多信息,請閱讀“ 測試驅動開發(TDD):使用Java范例的最佳做法” 。

測試下方提供了一種可能的解決方案。 嘗試先自己解決kata。

火星漫游者

開發一個可在網格上移動漫游車的API。

規則:

  • 您將獲得流動站的初始起點(x,y)及其面向的方向(N,S,E,W)。
  • 流動站接收命令的字符數組。
  • 實施使漫游車前進/后退(f,b)的命令。
  • 實現使流動站左/右(l,r)旋轉的命令。
  • 實現從網格的一個邊緣到另一邊緣的環繞。 (行星畢竟是球體)
  • 每次移動到新的廣場之前,請執行障礙檢測。 如果給定的命令序列遇到障礙物,則流動站將移動到最后一個可能的點并報告障礙物。

測驗

以下是一組可用于以TDD方式解決此問題的單元測試。

package com.technologyconversations.kata.marsrover;import org.junit.Before; import org.junit.Test;import java.util.ArrayList; import java.util.Arrays; import java.util.List;import static org.assertj.core.api.Assertions.*;/* Source: http://dallashackclub.com/roverDevelop an api that moves a rover around on a grid. * You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing. * - The rover receives a character array of commands. * - Implement commands that move the rover forward/backward (f,b). * - Implement commands that turn the rover left/right (l,r). * - Implement wrapping from one edge of the grid to another. (planets are spheres after all) * - Implement obstacle detection before each move to a new square. * If a given sequence of commands encounters an obstacle, the rover moves up to the last possible point and reports the obstacle. */ public class RoverSpec {private Rover rover;private Coordinates roverCoordinates;private final Direction direction = Direction.NORTH;private Point x;private Point y;private List<Obstacle> obstacles;@Beforepublic void beforeRoverTest() {x = new Point(1, 9);y = new Point(2, 9);obstacles = new ArrayList<Obstacle>();roverCoordinates = new Coordinates(x, y, direction, obstacles);rover = new Rover(roverCoordinates);}@Testpublic void newInstanceShouldSetRoverCoordinatesAndDirection() {assertThat(rover.getCoordinates()).isEqualToComparingFieldByField(roverCoordinates);}@Testpublic void receiveSingleCommandShouldMoveForwardWhenCommandIsF() throws Exception {int expected = y.getLocation() + 1;rover.receiveSingleCommand('F');assertThat(rover.getCoordinates().getY().getLocation()).isEqualTo(expected);}@Testpublic void receiveSingleCommandShouldMoveBackwardWhenCommandIsB() throws Exception {int expected = y.getLocation() - 1;rover.receiveSingleCommand('B');assertThat(rover.getCoordinates().getY().getLocation()).isEqualTo(expected);}@Testpublic void receiveSingleCommandShouldTurnLeftWhenCommandIsL() throws Exception {rover.receiveSingleCommand('L');assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.WEST);}@Testpublic void receiveSingleCommandShouldTurnRightWhenCommandIsR() throws Exception {rover.receiveSingleCommand('R');assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST);}@Testpublic void receiveSingleCommandShouldIgnoreCase() throws Exception {rover.receiveSingleCommand('r');assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST);}@Test(expected = Exception.class)public void receiveSingleCommandShouldThrowExceptionWhenCommandIsUnknown() throws Exception {rover.receiveSingleCommand('X');}@Testpublic void receiveCommandsShouldBeAbleToReceiveMultipleCommands() throws Exception {int expected = x.getLocation() + 1;rover.receiveCommands("RFR");assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected);assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.SOUTH);}@Testpublic void receiveCommandShouldWhatFromOneEdgeOfTheGridToAnother() throws Exception {int expected = x.getMaxLocation() + x.getLocation() - 2;rover.receiveCommands("LFFF");assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected);}@Testpublic void receiveCommandsShouldStopWhenObstacleIsFound() throws Exception {int expected = x.getLocation() + 1;rover.getCoordinates().setObstacles(Arrays.asList(new Obstacle(expected + 1, y.getLocation())));rover.getCoordinates().setDirection(Direction.EAST);rover.receiveCommands("FFFRF");assertThat(rover.getCoordinates().getX().getLocation()).isEqualTo(expected);assertThat(rover.getCoordinates().getDirection()).isEqualTo(Direction.EAST);}@Testpublic void positionShouldReturnXYAndDirection() throws Exception {rover.receiveCommands("LFFFRFF");assertThat(rover.getPosition()).isEqualTo("8 X 4 N");}@Testpublic void positionShouldReturnNokWhenObstacleIsFound() throws Exception {rover.getCoordinates().setObstacles(Arrays.asList(new Obstacle(x.getLocation() + 1, y.getLocation())));rover.getCoordinates().setDirection(Direction.EAST);rover.receiveCommands("F");assertThat(rover.getPosition()).endsWith(" NOK");}}

以下是一種可能的解決方案。

package com.technologyconversations.kata.marsrover;/* Method receiveCommands should be used to transmit commands to the rover.*/ public class Rover {private Coordinates coordinates;public void setCoordinates(Coordinates value) {coordinates = value;}public Coordinates getCoordinates() {return coordinates;}public Rover(Coordinates coordinatesValue) {setCoordinates(coordinatesValue);}public void receiveCommands(String commands) throws Exception {for (char command : commands.toCharArray()) {if (!receiveSingleCommand(command)) {break;}}}public boolean receiveSingleCommand(char command) throws Exception {switch(Character.toUpperCase(command)) {case 'F':return getCoordinates().moveForward();case 'B':return getCoordinates().moveBackward();case 'L':getCoordinates().changeDirectionLeft();return true;case 'R':getCoordinates().changeDirectionRight();return true;default:throw new Exception("Command " + command + " is unknown.");}}public String getPosition() {return getCoordinates().toString();}}

完整源代碼位于GitHub存儲庫[https://github.com/vfarcic/mars-rover-kata-java)中。 上面的代碼僅表示主類的代碼。 還有其他幾個類/對象及其相應的規范。 除了測試和實現之外,存儲庫還包括build.gradle,可用于下載AssertJ依賴項并運行測試README.md包含有關如何設置項目的簡短說明。

您有什么解決方案? 將其發布為評論,以便我們可以比較解決此kata的不同方法。

翻譯自: https://www.javacodegeeks.com/2014/10/java-tutorial-through-katas-mars-rover.html

總結

以上是生活随笔為你收集整理的Katas编写的Java教程:Mars Rover的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。