From 03d8081a7382705a81ed2c80e2bc1e4f807b5dc6 Mon Sep 17 00:00:00 2001 From: mo khan Date: Sun, 11 Aug 2019 17:52:15 -0600 Subject: add documentation --- src/Q9/MovingRobot.java | 41 +++++++++++++++++++++++++++++++++++++++++ src/Q9/Robot.java | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/Q9/MovingRobot.java b/src/Q9/MovingRobot.java index fa51410..21c46a1 100644 --- a/src/Q9/MovingRobot.java +++ b/src/Q9/MovingRobot.java @@ -1,3 +1,11 @@ +/** + * Assignment 2, COMP268 Class: MovingRobot.java + * + * @description A class used to move a robot on a 9x9 grid. + * @author: mo khan Student ID: 3431709 + * @date August 5, 2019 + * @version 1.0 + */ package Q9; import java.util.*; @@ -6,11 +14,23 @@ public class MovingRobot extends Robot { private ArrayList moves = new ArrayList(); private Random rng; + /** + * Creates an instance of a moving robot. + * + * @param x the x coordinate on the 9x9 grid + * @param y the y coordinate on the 9x9 grid + */ public MovingRobot(int x, int y) { super(x, y); this.rng = new Random(); } + /** + * Checks to see if a robot can advance in the direction provided. + * + * @param the direction that a robot wants to advance to. + * @return returns true if the robot can advance to the given direction. + */ public boolean validateNextMove(int direction) { switch (direction) { case Robot.NORTH: @@ -34,26 +54,43 @@ public class MovingRobot extends Robot { } } + /** + * @return a random direction. + */ public int generateNextMove() { return this.rng.nextInt(7) + 1; } + /** + * @return true if the two robots are in the same coordinate. + */ public static boolean sameSlot(Robot r1, Robot r2) { return r1.getX() == r2.getX() && r1.getY() == r2.getY(); } + /** + * @return The list of moves made by the robot. + */ public String printMoves() { ArrayList printableMoves = new ArrayList(); for (Integer move : this.moves) printableMoves.add(String.valueOf(move)); return String.join(",", printableMoves); } + /** + * Moves the robot in a random direction. + */ public void move() { int direction = generateNextMove(); while (!validateNextMove(direction)) direction = generateNextMove(); this.move(direction); } + /** + * An overload of the move method that attempts to move in the direction + * specified. + * @param direction the direction to move the robot towards. + */ public void move(int direction) { if (!validateNextMove(direction)) return; @@ -124,6 +161,10 @@ public class MovingRobot extends Robot { this.setX(this.getX() - 1); } + /** + * The entry point to the console application. + * @param args the argument vector provided to the program. + */ public static void main(String[] args) { MovingRobot r1 = new MovingRobot(0, 0); MovingRobot r2 = new MovingRobot(9, 9); diff --git a/src/Q9/Robot.java b/src/Q9/Robot.java index 62a972d..787f7cc 100644 --- a/src/Q9/Robot.java +++ b/src/Q9/Robot.java @@ -1,3 +1,11 @@ +/** + * Assignment 2, COMP268 Class: Robot.java + * + * @description A class used to represent the location of a robot on a grid. + * @author: mo khan Student ID: 3431709 + * @date August 5, 2019 + * @version 1.0 + */ package Q9; public class Robot { @@ -27,31 +35,56 @@ public class Robot { private int x; private int y; + /** + * Constructs an instance of a robot. + * @param x the x coordinate of the robot. + * @param y the y coordinate of the robot. + */ public Robot(int x, int y) { this.x = x; this.y = y; } + /** + * @return the x coordinate + */ public int getX() { return x; } + /** + * @return the y coordinate + */ public int getY() { return y; } + /** + * @param x the x coordinate to move to. + */ public void setX(int x) { this.x = x; } + /** + * @param y the y coordinate to move to. + */ public void setY(int y) { this.y = y; } + /** + * @return true if the robot is at the given coordinate + */ public boolean atPosition(int x, int y) { return getX() == x && getY() == y; } + /** + * @param r1 robot one. + * @param r2 robot two. + * @return the grid with each of the robots printed. + */ public static String printGrid(Robot r1, Robot r2) { String grid = ""; -- cgit v1.2.3