Can somebody help me create a UML for this code please import java.util.*; impor
ID: 3564520 • Letter: C
Question
Can somebody help me create a UML for this code please
import java.util.*;
import java.io.*;
public class CreateMouseIsland {
private static final int UP = 1;
private static final int DOWN = 2;
private static final int LEFT = 3;
private static final int RIGHT = 4;
private static int drowned = 0;
private static int starved = 0;
private static int escaped = 0;
private static final int WATER = -1;
private static final int LAND = 0;
private static final int BRIDGE = 99;
private static final int MOUSE = 1;
private static int mouseRow;
private static int mouseCol;
private static int firstBridgeRow;
private static int firstBridgeCol;
private static int secondBridgeRow;
private static int secondBridgeCol;
private static String status;
private static int[][] stepCount;
public static void main(String[] args) throws Exception {
int size, col1, col2, row1, row2, mouseCol, mouseRow; // initializing
// variables
size = getArraySize();
stepCount = new int[size][size];
int[][] mouseIsland = new int[size][size]; // initializing 2d char array
col1 = bridgeOneCol(mouseIsland, size); // Sending values to the various
// methods and calling from them
row1 = bridgeOneRow(mouseIsland, size, col1);
col2 = bridgeTwoCol(mouseIsland, size);
row2 = bridgeTwoRow(mouseIsland, size, col2);
mouseCol = mouseCol(mouseIsland, size);
mouseRow = mouseRow(mouseIsland, size);
mouseIsland = fillIsland(mouseIsland, size, col1, col2, row1, row2,
mouseCol, mouseRow);
displayIsland(mouseIsland, size);
displayInitialResults(mouseIsland, size);
moveMouse(mouseIsland, size);
System.out.println();
displayFinalResults(mouseIsland, size);
reset(size);
displayTotals();
System.out.println("#-----Thanks-for-Playing-----#");
}
public static int getArraySize() // method to get array size. It is square
// so only one value needed. In future I
// would
{ // also initialize the array here and return it, versus having two methods
// for length and height
int size;
Scanner input = new Scanner(System.in);
System.out.println("What is the Island Size? ");
size = input.nextInt();
while (size < 3 || size > 10) // anything smaller than 3 wont work for
// this scenario
{ // anything over 10 is just getting obnoxious
if (size < 3) // if statements so prompt user for new island size
{
System.out.println("That Island is too small.");
} else {
System.out.println("That Island is too Big.");
}
System.out.println("Please enter a new size: ");
size = input.nextInt();
}
return size;
}
public static int bridgeOneCol(int[][] mouseIsland, int size) // method to
// receive
// input
// from user
{ // declaring the location of bridge one's column
Scanner input = new Scanner(System.in);
System.out.println("Column of First Bridge? ");
firstBridgeCol = input.nextInt();
while (firstBridgeCol < 1 || firstBridgeCol > size) {
System.out
.println("Column does not exist. Please enter new column: ");
firstBridgeCol = input.nextInt();
}
return firstBridgeCol;
}
public static int bridgeOneRow(int[][] mouseIsland, int size, int col1) // method
// to
// receive
// input
// from
// user
{ // declaring the location of bridge one's row
Scanner input = new Scanner(System.in);
if (col1 != 1 && col1 != size) // if the bridge is in any other column
// than a
{ // border column, its row must be a border row
System.out.println("Row of First Bridge? Must be 1 or " + size
+ ":"); // to bridge the water. **Same for bridge two row**
firstBridgeRow = input.nextInt();
while (firstBridgeRow != 1 && firstBridgeRow != size) {
System.out.println("Row Must be 1 or " + size
+ ". Please enter new row: ");
firstBridgeRow = input.nextInt();
}
} else {
System.out.println("Row of First Bridge? ");
firstBridgeRow = input.nextInt();
while (firstBridgeRow <= 1 || firstBridgeRow >= size) {
System.out
.println("Row does not exist. Please enter new row: ");
firstBridgeRow = input.nextInt();
}
}
return firstBridgeRow;
}
public static int bridgeTwoCol(int[][] mouseIsland, int size) // identical
// method
// from
// bridge
// one
// column
{ // just receiving data for bridge two
Scanner input = new Scanner(System.in);
System.out.println("Column of Second Bridge? ");
secondBridgeCol = input.nextInt();
while (secondBridgeCol < 1 || secondBridgeCol > size) {
System.out
.println("Column does not exist. Please enter new column: ");
secondBridgeCol = input.nextInt();
}
return secondBridgeCol;
}
public static int bridgeTwoRow(int[][] mouseIsland, int size, int col2) // identical
// method
// from
// bridge
// one
// row
{ // just receiving data for bridge two
Scanner input = new Scanner(System.in);
if (col2 != 1 && col2 != size) {
System.out.println("Row of Second Bridge? Must be 1 or " + size
+ ":");
secondBridgeRow = input.nextInt();
while (secondBridgeRow != 1 && secondBridgeRow != size) {
System.out.println("Row Must be 1 or " + size
+ ". Please enter new row: ");
secondBridgeRow = input.nextInt();
}
} else {
System.out.println("Row of Second Bridge? ");
secondBridgeRow = input.nextInt();
while (secondBridgeRow <= 1 || secondBridgeRow >= size) {
System.out
.println("Row does not exist. Please enter new row: ");
secondBridgeRow = input.nextInt();
}
}
return secondBridgeRow;
}
public static int mouseCol(int[][] mouseIsland, int size) // prompt user for
// column of
// mouse
{
Scanner input = new Scanner(System.in);
System.out.println("Column of Mouse? ");
mouseCol = input.nextInt();
while (mouseCol < 2 || mouseCol > size - 1) // user must input valid
// column so mouse is on
// land
{
System.out
.println("Mouse must be on land. Please enter a new column: ");
mouseCol = input.nextInt();
}
return mouseCol;
}
public static int mouseRow(int[][] mouseIsland, int size) // prompt user for
// row of mouse
{
Scanner input = new Scanner(System.in);
System.out.println("Row of Mouse? ");
mouseRow = input.nextInt();
while (mouseRow < 2 || mouseRow > size - 1) // also validating that
// mouse will be on land
{
System.out
.println("Mouse must be on land. Please enter a new Row: ");
mouseRow = input.nextInt();
}
return mouseRow;
}
public static int[][] fillIsland(int[][] mouseIsland, int size, int b1c,
int b2c, int b1r, int b2r, int mc, int mr) // filling the array with
// char data
{
b1c = b1c - 1; // b1c = bridge one column
b2c = b2c - 1;// b2c = bridge two column
b1r = b1r - 1;// b1r = bridge one row
b2r = b2r - 1;// b2r = bridge two row
mc = mc - 1;// mc = mouse column
mr = mr - 1;// mr = mouse row
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == 0 || row == size - 1 || col == 0 || col == size - 1) {
mouseIsland[row][col] = -1; // Water
} else {
mouseIsland[row][col] = 0; // Land
}
if ((col == b1c && row == b1r) || (col == b2c && row == b2r)) {
mouseIsland[row][col] = 99; // Bridge
}
if ((col == mc && row == mr)) {
mouseIsland[row][col] = 1; // Mouse
}
}
}
return mouseIsland;
}
public static void displayIsland(int[][] mouseIsland, int size) // Displaying
// the
// Island in
// the
// console
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
System.out.print(mouseIsland[row][col] + " ");
}
System.out.println();
}
}
public static void stepCount() {
stepCount[mouseRow][mouseCol] = stepCount[mouseRow][mouseCol] + 1;
}
public static void moveMouse(int[][] mouseIsland, int size) {
int move;
int moveCount = 0;
int newRow = 0;
int newCol = 0;
int min = 1;
int max = 4;
CreateMouseIsland createMouseIsland = new CreateMouseIsland();
do {
move = min + (int) (Math.random() * ((max - min) + 1));
stepCount();
switch (move) {
case UP:
newRow = (mouseRow - 1);
newCol = mouseCol;
break;
case DOWN:
newRow = (mouseRow + 1);
newCol = mouseCol;
break;
case LEFT:
newRow = mouseRow;
newCol = (mouseCol - 1);
break;
case RIGHT:
newRow = mouseRow;
newCol = (mouseCol + 1);
}
if (mouseIsland[newRow][newCol] == LAND && moveCount < 20) {
mouseIsland[mouseRow][mouseCol] = LAND;
mouseIsland[newRow][newCol] = MOUSE;
mouseRow = newRow;
mouseCol = newCol;
stepCount();
moveCount++;
} else {
switch (mouseIsland[newRow][newCol]) {
case BRIDGE:
mouseIsland[mouseRow][mouseCol] = LAND;
mouseIsland[newRow][newCol] = MOUSE;
escaped++;
status = "Escaped";
break;
case (char) WATER:
mouseIsland[mouseRow][mouseCol] = LAND;
mouseIsland[newRow][newCol] = MOUSE;
drowned++;
status = "Drowned";
break;
default:
if (moveCount == 20)
mouseIsland[mouseRow][mouseCol] = LAND;
mouseIsland[newRow][newCol] = MOUSE;
starved++;
status = "Starved";
}
mouseRow = newRow;
mouseCol = newCol;
stepCount();
break;
}
} while (moveCount < 20);
}
public static void displayInitialResults(int[][] mouseIsland, int size) {
char[] border = new char[2 * size + 10];
System.out.println("Island Size " + size);
System.out.println("Mouse Original Location Row: " + mouseRow
+ " Column: " + mouseCol);
System.out.println("Bridge One Row: " + firstBridgeRow + " Column: "
+ firstBridgeCol);
System.out.println("Bridge Two Row: " + secondBridgeRow + " Column: "
+ secondBridgeCol);
System.out.print(" ");
for (int ndx = 0; ndx < border.length; ndx++) {
border[ndx] = '*';
System.out.printf("%2c", border[ndx]);
}
System.out.println(" ");
for (int row = 0; row < mouseIsland.length; row++) {
for (int col = 0; col < mouseIsland.length; col++) {
System.out.print(mouseIsland[row][col] + " ");
}
System.out.println();
}
}
public static void displayFinalResults(int[][] mouseIsland, int size) {
char[] border = new char[2 * size + 10];
System.out.print(" ");
for (int ndx = 0; ndx < border.length; ndx++) {
border[ndx] = '*';
System.out.printf("%2c", border[ndx]);
}
System.out.println(" ");
System.out.printf(" Mouse " + status);
System.out.println();
for (int row = 0; row < mouseIsland.length; row++) {
for (int col = 0; col < mouseIsland.length; col++) {
System.out.print(mouseIsland[row][col] + " ");
}
System.out.print(" ");
for (int col = 0; col < mouseIsland.length; col++) {
System.out.print(stepCount[row][col] + " ");
}
System.out.println();
}
}
public static void reset(int size) throws Exception {
setStepArray(size);
}
public static void setStepArray(int size) {
stepCount = new int[size][size];
for (int row = 0; row < stepCount.length; row++) {
for (int col = 0; col < stepCount.length; col++) {
stepCount[row][col] = 0;
}
}
}
public static void displayTotals() {
System.out.println(" Mouse Escaped: " + escaped);
System.out.println("Mouse Drowned: " + drowned);
System.out.println("Mouse Starved: " + starved + " ");
}
}
Explanation / Answer
The java program given contains only one class. So, UML diagram contains only one class diagram.
In UML Class is represented using a rectangle with 4 divisions (compartments).
The first division contains the name of the class. Here the class name is