Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Anyone can help me to commenting which is doing which? I got this comment import

ID: 3596328 • Letter: A

Question

Anyone can help me to commenting which is doing which?

I got this comment

import java.awt.Color;
import java.awt.Graphics;
public class SimpleBotView {
  
private Color robot;
private Color wall;
private Color Robot;
private Color trail;
private DrawingPanel dp;
private Color background;
private int size;
private int r;
private int c;
private char[][] initial;
private void drawPanel() {
Graphics g = dp.getGraphics();
  
for(int i=0 ; i<initial.length ; i++) {
for(int j=0; j<initial[i].length ; j++) {

switch( initial[i][j] ) {
case 'w':
g.setColor( wall );
g.fillRect(j*size, i*size, size, size);
break;
case 't':
g.setColor( trail );
g.fillRect(j*size, i*size, size, size);
break;
  
case 'r':
g.setColor( robot );
g.fillRect(j*size, i*size, size, size);
break;
  
case 'b':
g.setColor( background );
g.fillRect(j*size, i*size, size, size);
break;
}
}
}
}
private SimpleBotView(char[][] initial, Color robot, Color wall, Color trail, Color background, int size) {
this.initial = initial;
this.robot = robot;
this.wall = wall;
this.trail = trail;
this.background = background;
this.size = size;

r = initial.length;
c = initial[0].length;

dp = new DrawingPanel(r*size, c*size);
dp.setVisible(true);
drawPanel();
}
private void move(char direction) {
for(int j=0 ; j<r ; j++) {
for(int i=0 ; i<c ; i++) {
if( initial[j][i] == 'r' ) {
switch( direction ) {
case 'u':
if( i - 1 > 0 && initial[i-1][j] != 'w' ) {
initial[j][i] = 't';
initial[j-1][i] = 'r';
}
break;
case 'd':
if( j + 1 < r && initial[j+1][i] != 'w' ) {
initial[j][i] = 't';
initial[j+1][i] = 'r';
}
break;
case 'l':
if( i - 1 > 0 && initial[j][i-1] != 'w' ) {
initial[j][i] = 't';
initial[j][i-1] = 'r';
}
break;
case 'r':
if( j + 1 < c && initial[i][j+1] != 'w' ) {
initial[i][j] = 't';
initial[i][j+1] = 'r';
}
break;
}
break;
}
}
}
drawPanel();
}

}

Explanation / Answer

Included all the comments to your code. Check it. You have already declared the variables in the class as private which is good practise.

import java.awt.Color;
import java.awt.Graphics;
public class SimpleBotView {
  
private Color robot;
private Color wall;
private Color Robot;
private Color trail;
private DrawingPanel dp;
private Color background;
private int size;
private int r;
private int c;
private char[][] initial;
private void drawPanel() {
// Initializing the graphics object
Graphics g = dp.getGraphics();
  
// This for loop is used to chose the shape of the object present in te initial array
// Initial is a 2D matrix
for(int i=0 ; i<initial.length ; i++) {
for(int j=0; j<initial[i].length ; j++) {
// Switch case is used to perform operations based on the value of initial element
switch( initial[i][j] ) {
// This block is executed if initial element is w
case 'w':
// Setting color of the graphics object which is stored in wall
g.setColor( wall );
// Filling a rectangle with x-cordinate, y-coordinate, width and height respectively as arguments
g.fillRect(j*size, i*size, size, size);
break;
// This block is executed if initial element is t
case 't':
// Setting color of the graphics object which is stored in trail
g.setColor( trail );
// Filling a rectangle with x-cordinate, y-coordinate, width and height respectively as arguments
g.fillRect(j*size, i*size, size, size);
break;
// This block is executed if initial element is r
case 'r':
// Setting color of the graphics object which is stored in robot
g.setColor( robot );
// Filling a rectangle with x-cordinate, y-coordinate, width and height respectively as arguments
g.fillRect(j*size, i*size, size, size);
break;
// This block is executed if initial element is b
case 'b':
// Setting color of the graphics object which is stored in background
g.setColor( background );
// Filling a rectangle with x-cordinate, y-coordinate, width and height respectively as arguments
g.fillRect(j*size, i*size, size, size);
break;
}
}
}
}
// Constructor triggered to initalize variables
private SimpleBotView(char[][] initial, Color robot, Color wall, Color trail, Color background, int size) {
this.initial = initial;
this.robot = robot;
this.wall = wall;
this.trail = trail;
this.background = background;
this.size = size;
// Assign the length of the initial matrix to r which is the no of rows
r = initial.length;
// Assign the length of the initial matrix of first row to c which is the no of columns
c = initial[0].length;
// initializing the drawing panel with width = r*size and height = c*size
dp = new DrawingPanel(r*size, c*size);
// Displaying the drawing panel
dp.setVisible(true);
// Calling the method
drawPanel();
}
// Method used to move the shapes
private void move(char direction) {
// Iterating over the complete drawing panel
for(int j=0 ; j<r ; j++) {
for(int i=0 ; i<c ; i++) {
// If the matrix value is r
if( initial[j][i] == 'r' ) {
// If the director is up
switch( direction ) {
case 'u':
// If the x coordinate greater than 0 and matix value is not equal to w
if( i - 1 > 0 && initial[i-1][j] != 'w' ) {
initial[j][i] = 't';
initial[j-1][i] = 'r';
}
break;
// If the director is down
case 'd':
// If the x coordinate less than row value and matix value is not equal to w
if( j + 1 < r && initial[j+1][i] != 'w' ) {
initial[j][i] = 't';
initial[j+1][i] = 'r';
}
break;
// If the director is left
case 'l':
// If the x coordinate greater than 0 and matix value is not equal to w
if( i - 1 > 0 && initial[j][i-1] != 'w' ) {
initial[j][i] = 't';
initial[j][i-1] = 'r';
}
break;
// If the director is right
case 'r':
// If the y coordinate less than column value and matix value is not equal to w
if( j + 1 < c && initial[i][j+1] != 'w' ) {
initial[i][j] = 't';
initial[i][j+1] = 'r';
}
break;
}
break;
}
}
}
drawPanel();
}

}

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote