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
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 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 for(int j=0; j Random random = new Random(); /** * for loop to put dirt on 30% on the board */ for(int i=0; i 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; iComplete: This option must complete the cleaning of the entire grid, for each step you must print the action you made and you must print the board again. Note that this menu option can only be executed when it has been created the environment. (ON JAVA)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started