Question: Hello, I am struggling with a lab in Java. The topics are recursion and arrays, if someone can solve this problem so i have a
Hello, I am struggling with a lab in Java. The topics are recursion and arrays, if someone can solve this problem so i have a reference on how to do it correctly it would be appreciated. The Check Input file should be used in a separate class and call the methods to validate input as needed. Thank you!

grid1 txt.file:
##oooooo ##oo#oo# oo##o##o oo##o##o o#oo#oo# oo##o##o oo##o##o o#oo#oo#
grid2 txt.file:
o##oo#oo ####o#oo ooo##ooo o#o#oo## o#ooo#o# o####ooo o#o##ooo o#o##ooo
grid3 txt.file:
##oo##o# oooo#oo# ##oo#oo# ##oo#oo# o####ooo o##o##oo oooo##oo #oo#oo#o
grid4 txt.file:
o##o#### oooo##o# ###o##o# #o#o##o# #o#oooo# #ooo#o## #o###o#o ##oooooo
Check Input File (use this as separate class and call upon methods inside tho validate user input):
import java.util.Scanner;
/**
* Static functions used to check console input for validity.
*
* Use: Place CheckInput class in the same project folder as your code.
* Call CheckInput functions from your code using "CheckInput."
*
* Example: int num = CheckInput.getInt();
*
*/
public class CheckInput {
/**
* Checks if the inputted value is an integer.
* @return the valid input.
*/
public static int getInt() {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is an integer and
* within the specified range (ex: 1-10)
* @param low lower bound of the range.
* @param high upper bound of the range.
* @return the valid input.
*/
public static int getIntRange( int low, int high ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input = low ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a non-negative integer.
* @return the valid input.
*/
public static int getPositiveInt( ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input >= 0 ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a double.
* @return the valid input.
*/
public static double getDouble() {
Scanner in = new Scanner( System.in );
double input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextDouble() ) {
input = in.nextDouble();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Takes in a string from the user.
* @return the inputted String.
*/
public static String getString() {
Scanner in = new Scanner( System.in );
String input = in.nextLine();
return input;
}
/**
* Takes in a yeso from the user.
* @return 1 if yes, 0 if no.
*/
public static int getYesNo(){
boolean valid = false;
while( !valid ) {
String s = getString();
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) {
return 1;
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) {
return 0;
} else {
System.out.println( "Invalid Input." );
}
}
return 0;
}
}
Grid Area Counter-Write a program that reads a grid from a file that consists of s and 'o's. The 'o's make up areas in the grid that are separated by the '#'s Count the number of 'o's in each area recursively Functions 1 . Read in a file that consists of an 8x8 grid of" #'s and '0's a. There are multiple files with different grids in them b. The file's name will be numbered something like: gridl.txt C. Place the grid into the middle of a 10x10 array of '#'s i. The extra two rows and columns create a boundary edge so you don't accidentally go out of bounds of the array 2. 3. Traverse the grid from left to right, top to bottom. Every time you find a new "o 4. Display the grid a. Display as an 8x8 grid, not 10x10 that hasn't been counted before, call the function that counts an area of 'o's Recursively count the number of'o's in an area. Check up, down, left and right (in whichever order you like), from each spot you visit. Mark spots as you visit them so they aren't counted multiple times. Keep a tally as you encounter each new 'o'. Return that final value for that area Allow the user to choose from several different grids that are read in from a file. Display the results in a list of areas with their counts. Check all user input for invalid entries Example-The following grid has two areas of 10 and eight areas of 2 Grid Area Counter Enter Grid #: 1 ofootoo Area 1 -10 Area 210 Area 32 Area 4 2 Area 52 Area 62 Area 7-2 Area 82 Area 92 Area 102 Grid Area Counter-Write a program that reads a grid from a file that consists of s and 'o's. The 'o's make up areas in the grid that are separated by the '#'s Count the number of 'o's in each area recursively Functions 1 . Read in a file that consists of an 8x8 grid of" #'s and '0's a. There are multiple files with different grids in them b. The file's name will be numbered something like: gridl.txt C. Place the grid into the middle of a 10x10 array of '#'s i. The extra two rows and columns create a boundary edge so you don't accidentally go out of bounds of the array 2. 3. Traverse the grid from left to right, top to bottom. Every time you find a new "o 4. Display the grid a. Display as an 8x8 grid, not 10x10 that hasn't been counted before, call the function that counts an area of 'o's Recursively count the number of'o's in an area. Check up, down, left and right (in whichever order you like), from each spot you visit. Mark spots as you visit them so they aren't counted multiple times. Keep a tally as you encounter each new 'o'. Return that final value for that area Allow the user to choose from several different grids that are read in from a file. Display the results in a list of areas with their counts. Check all user input for invalid entries Example-The following grid has two areas of 10 and eight areas of 2 Grid Area Counter Enter Grid #: 1 ofootoo Area 1 -10 Area 210 Area 32 Area 4 2 Area 52 Area 62 Area 7-2 Area 82 Area 92 Area 102
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
