Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have these code on java: import java.util.Random; import java.util.Scanner; /** * @author Yaiti Rivera & Wilfredo Estevez *These program is an agent that act

I have these code on java:

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;

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: case 3: default: System.out.println("An unknown error has occured.:"); } }

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; i

I need to move the agent:

(instructions)

Move agent: This option must make a single action, if it is in a clean box you must move it from the box and if it is in a dirty box you must clear the box. The action you made must be printed and the board must be printed again. The movement of the agent will be as indicated by the arrow in the following figure. Note that this menu option can only be executed when the environment has been created. (On JAVA)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions