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

Java code: import java.util.Random; import java.util.Scanner; /** * @author Yait

ID: 3744510 • Letter: J

Question

Java code:

import java.util.Random;
import java.util.Scanner;

/**
* @author Yaiti Rivera & Wilfredo Estevez
*These program is an agent that act like a vacuum cleaner
*/
public class Vacuum
{
   private int n;

   boolean exit;

   int currentX = 0;
   int currentY = 0;

   char[][] environment;

   public static void main(String[] args)
   {
      Vacuum menu = new Vacuum();
      menu.runMenu();
   }

   public void runMenu()
   {
       printHeader();
       while(!exit)
       {
           printMenu();
           int choice = getInput();
           performAction(choice);
       }
   }
   public void printHeader()
   {
       System.out.println("+------------------------+");
       System.out.println("|     Welcome to Menu    |");
       System.out.println("|         Program        |");
       System.out.println("+------------------------+");
   }

   public void printMenu()
   {
       System.out.println(" Please make a selection");
       System.out.println(" 1) Create Environment ");
       System.out.println(" 2) Move Agent ");
       System.out.println(" 3) Complete ");
       System.out.println(" 0) Exit ");
   }

   private int getInput()
   {
       Scanner kb = new Scanner(System.in);
       int choice = -1;
       while(choice < 0 || choice >2)
       {
           try
           {
               System.out.print(" Enter your choice: ");
               choice = Integer.parseInt(kb.nextLine());
           }
           catch(NumberFormatException e)
           {
               System.out.println("Invalid selection, Please try again.");
           }
       }
       return choice;
   }

   private void performAction(int choice)
   {
   switch(choice)
   {
   case 0:
        exit = true;
        System.out.println("Thanks for using aour program.");
        break;
   case 1:
         Vacuum();
         printBoard();
         break;
   case 2:
         moveAgent();
         break;
   case 3:
  
   default:
         System.out.println("An unknown error has occured.:");
   }
   }

   private void moveAgent()
   {

   char current = environment[currentX][currentY];
     
       if(current == 'X')
       {
           // dirt cell, then just clean
           System.out.println("Currently at Dirt cell, cleaning it.");
           environment[currentX][currentY] = 'A';
       }
       else
       {
           for(int i=0; i<environment.length; i++)
           {
               for(int j=0; j<environment.length; j++)
               {
                   if(environment[i][j] == 'X')
                   {
                       // fond dirty cell
                       System.out.println("Agent moving to (" + i + "," + j +")");
                       environment[currentX][currentY] = '0';
                       currentX = i;
                       currentY = j;
                     
                       printBoard();
                       return;
                   }
               }
           }
         
           System.out.println("No dirty cel left.");
         
       }
     
       //you may print
       printBoard();
     
   }

public void Vacuum()
   {
       Scanner sc = new Scanner(System.in);

       while(true)
       {
           System.out.println(" Enter the value of n (between 2 and 10) : ");

           n = sc.nextInt();

           if(n >= 2 && n <= 10)
           {
               break;
           }
         
           System.out.println("Invalid input!!!");
       }

       environment = new char[n][n];

       /**
        * initialize the empty board
        */

       for(int i=0; i<n; i++)
       {

           for(int j=0; j<n; j++)
           {
               environment[i][j] = '0'; // '0' represent a empty space
               environment[0][0] = 'A'; // A is the Agent
           }
       }

       Random random = new Random();

       /**
        * for loop to put dirt on 30% on the board
        */

       for(int i=0; i<n*n*0.3; i++)
       {
           while(true)
           {
               int randomRow = random.nextInt(n);

               int randomCol = random.nextInt(n);

               if(environment[randomRow][randomCol] != 'X')
               {
                   /**
                    * X represent dirt
                    */

                   environment[randomRow][randomCol] = 'X';

                   break;
               }
           }
       }
   }

   public void printBoard()
   {
       for(int i=0; i<n; i++)
       {
           for(int j=0; j<n; j++)
           {
               System.out.print(environment[i][j] + " ");
           }
           System.out.println();
       }
   }

   public static void main1(String args[])
   {
       Vacuum vacuum = new Vacuum();
     
       vacuum.printBoard();
   }
}

Explanation / Answer

package vaccum;

import java.util.Random;
import java.util.Scanner;

/**
* @author Yaiti Rivera & Wilfredo Estevez
*These program is an agent that act like a vacuum cleaner
*/
public class Vacuum
{
private int n;

boolean exit;

int currentX = 0;
int currentY = 0;

char[][] environment=null;

public static void main(String[] args)
{
Vacuum menu = new Vacuum();
menu.runMenu();
}

public void runMenu()
{
printHeader();
while(!exit)
{
printMenu();
int choice = getInput();
performAction(choice);
}
}
public void printHeader()
{
System.out.println("+------------------------+");
System.out.println("| Welcome to Menu |");
System.out.println("| Program |");
System.out.println("+------------------------+");
}

public void printMenu()
{
System.out.println(" Please make a selection");
System.out.println(" 1) Create Environment ");
System.out.println(" 2) Move Agent ");
System.out.println(" 3) Complete ");
System.out.println(" 0) Exit ");
}

private int getInput()
{
Scanner kb = new Scanner(System.in);
int choice = -1;
while(choice < 0 || choice >3)
{
try
{
System.out.print(" Enter your choice: ");
choice = Integer.parseInt(kb.nextLine());
}
catch(NumberFormatException e)
{
System.out.println("Invalid selection, Please try again.");
}
}
return choice;
}

private void performAction(int choice)
{
switch(choice)
{
case 0:
exit = true;
System.out.println("Thanks for using our program.");
break;
case 1:
Vacuum();
printBoard();
break;
case 2:
if(environment!=null)
moveAgent();
else
System.out.println("Create environment first");

break;
case 3:
if(environment!=null)
complete();
else
System.out.println("Create environment first");

break;
default:
System.out.println("An unknown error has occured.:");
}
}

private void moveAgent()
{

char current = environment[currentX][currentY];

if(current == 'X')
{
// dirt cell, then just clean
System.out.println("Currently at Dirt cell, cleaning it.");
environment[currentX][currentY] = 'A';
}
else
{
for(int i=0; i<environment.length; i++)
{
for(int j=0; j<environment.length; j++)
{
if(environment[i][j] == 'X')
{
// fond dirty cell
System.out.println("Agent moving to (" + i + "," + j +")");
environment[currentX][currentY] = '0';
currentX = i;
currentY = j;

printBoard();
return;
}
}
}

System.out.println("No dirty cel left.");

}

//you may print
printBoard();

}

public void Vacuum()
{
environment=null;
Scanner sc = new Scanner(System.in);

while(true)
{
System.out.println(" Enter the value of n (between 2 and 10) : ");

n = sc.nextInt();

if(n >= 2 && n <= 10)
{
break;
}

System.out.println("Invalid input!!!");
}

environment = new char[n][n];

/**
* initialize the empty board
*/

for(int i=0; i<n; i++)
{

for(int j=0; j<n; j++)
{
environment[i][j] = '0'; // '0' represent a empty space
environment[0][0] = 'A'; // A is the Agent
}
}

Random random = new Random();

/**
* for loop to put dirt on 30% on the board
*/

for(int i=0; i<n*n*0.3; i++)
{
while(true)
{
int randomRow = random.nextInt(n);

int randomCol = random.nextInt(n);

if(environment[randomRow][randomCol] != 'X')
{
/**
* X represent dirt
*/

environment[randomRow][randomCol] = 'X';

break;
}
}
}
}

public void printBoard()
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print(environment[i][j] + " ");
}
System.out.println();
}
}

public static void main1(String args[])
{
Vacuum vacuum = new Vacuum();

vacuum.printBoard();
}

/**
* This method iterates through the environment and cleans each dirt cell.
*/
private void complete() {

for(int i=0; i<environment.length; i++)
{
for(int j=0; j<environment.length; j++)
{
if(environment[i][j] == 'X') //if dirt cell found
{
System.out.println("Dirt cell at (" + i + "," + j +")");
System.out.println("Agent moving to (" + i + "," + j +") and cleaning it");
environment[i][j] = 'A';
environment[currentX][currentY]='0'; //clears the previous location of agent
printBoard();
currentX = i; //updates location of agent
currentY = j;
System.out.println(" ");

}
}
}

System.out.println("No dirty cell left.");   
//you may print
printBoard();

  
}

}

Sample Output

+------------------------+
| Welcome to Menu |
| Program |
+------------------------+

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 2
Create environment first

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 1

Enter the value of n (between 2 and 10) :
4
A 0 0 X
0 0 0 X
X X 0 0
0 0 X 0

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 2
Agent moving to (0,3)
0 0 0 X
0 0 0 X
X X 0 0
0 0 X 0

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 2
Currently at Dirt cell, cleaning it.
0 0 0 A
0 0 0 X
X X 0 0
0 0 X 0

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 3
Dirt cell at (1,3)
Agent moving to (1,3) and cleaning it
0 0 0 0
0 0 0 A
X X 0 0
0 0 X 0


Dirt cell at (2,0)
Agent moving to (2,0) and cleaning it
0 0 0 0
0 0 0 0
A X 0 0
0 0 X 0


Dirt cell at (2,1)
Agent moving to (2,1) and cleaning it
0 0 0 0
0 0 0 0
0 A 0 0
0 0 X 0


Dirt cell at (3,2)
Agent moving to (3,2) and cleaning it
0 0 0 0
0 0 0 0
0 0 0 0
0 0 A 0


No dirty cell left.
0 0 0 0
0 0 0 0
0 0 0 0
0 0 A 0

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 2
No dirty cel left.
0 0 0 0
0 0 0 0
0 0 0 0
0 0 A 0

Please make a selection
1) Create Environment
2) Move Agent
3) Complete
0) Exit

Enter your choice: 0
Thanks for using our program.
BUILD SUCCESSFUL (total time: 44 seconds)

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