Question: Javadoc the code below - public class Animal { - - // attributes - - private String name; - - private int x;Point class representing
Javadoc the code below
- public class Animal { - - // attributes - - private String name; - - private int x;Point class representing a point in (x, y) coordinate space * @author CS141 Class Demo * @version 1.0 (06/09/2020) * @see (include related class here) - - private int y; - - private int maxSpeed; - - // boundary value for grid (same for all directions) - - private static int BOUNDARY = 20; - - // default constructor - - public Animal() { - - x = 0; - - y = 0; - - name = "unknown"; - - maxSpeed = 2; - - } - - // constructor taking values for all fields - - public Animal(String name, int x, int y, int maxSpeed) { - - this.name = name; - - this.x = x; - - this.y = y; - - if (maxSpeed < 2 || maxSpeed > 5) { - - // if max speed provided is invalid, using a default value 2 - - maxSpeed = 2; - - } - - this.maxSpeed = maxSpeed; - - } - - // getter methods - - public int getX() { - - return x; - - } - - public int getY() { - - return y; - - } - - public String getName() { - - return name; - - } - - public String toString() { - - return name + " at (" + x + "," + y + ")"; - - } - - // returns true if two animals are in same spot - - public boolean touching(Animal other) { - - return this.x == other.x && this.y == other.y; - - } - - // moves the animal in a random direction - - public void move() { - - // generating random number of spaces to move (between 1 and maxSpeed) - - int units = (int) (Math.random() * maxSpeed) + 1; - - // generating a random direction (0-right,1-left,2-up,3-down) - - int dir = (int) (Math.random() * 4); - - if (dir == 0) { - - // moving right - - x += units; - - } else if (dir == 1) { - - // moving left - - x -= units; - - } else if (dir == 2) { - - // moving up - - y -= units; - - } else { - - // moving down - - y += units; - - } - - // if any values went out of range, updating them inside bounds as - - // needed. - - if (x < -BOUNDARY) { - - x = -BOUNDARY; - - } - - if (x > BOUNDARY) { - - x = BOUNDARY; - - } - - if (y < -BOUNDARY) { - - y = -BOUNDARY; - - } - - if (y > BOUNDARY) { - - y = BOUNDARY; - - } - - } - - } -
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
