Maximum positivity Purpose To enhance your ability to work with two-dimensional
ID: 3596236 • Letter: M
Question
Maximum positivity Purpose To enhance your ability to work with two-dimensional arrays. Directions You will read in a two-dimensional array of characters from the user. Each value will be either a plus sign (+)or a minus sign (-). Your task is to output the maximum number of plus signs in any row or column. Hint: To read in a single character, you can use the Scanner's next method and then get the first character, as shown here Scanner in - new Scanner(System.in) String temp -in.next); char c-temp.charAt (0) Example If the input is: d be:Explanation / Answer
SearchPlusSign.java
import java.util.Scanner;
public class SearchPlusSign {
public static void main(String[] args) {
// Creating an char 2-D array of size 10 by 10
char arrays[][] = new char[10][10];
// Declaring variables
int row = 0, col = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.println("Enter Input :");
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[0].length; j++) {
arrays[i][j] = sc.next(".").charAt(0);
}
}
//calling the method
int rowOrCol = maxPlusInRowOrColumn(arrays);
//Displaying the output
System.out.println("Maximum no of Plus Signs in row or column is :" + rowOrCol);
}
//This Method will count the maximum no of plus signs in a row or column
private static int maxPlusInRowOrColumn(char[][] arrays) {
char ch = '+';
int maxRow = 0, rowIndex = 0, maxCol = 0;
int rowCount = 0, colCount = 0, colIndex = 0;
for (int i = 0; i < arrays.length; i++) {
rowCount = 0;
for (int j = 0; j < arrays[0].length; j++) {
if (ch == arrays[i][j]) {
rowCount++;
}
}
if (maxRow < rowCount) {
maxRow = rowCount;
}
}
for (int i = 0; i < arrays.length; i++) {
colCount = 0;
for (int j = 0; j < arrays[0].length; j++) {
if (ch == arrays[j][i]) {
colCount++;
}
}
if (maxCol < colCount) {
maxCol = colCount;
}
}
if (maxRow > maxCol)
return maxRow;
else if (maxCol > maxRow)
return maxCol;
else
return maxRow;
}
}
________________
Output:
Enter Input :
+ - - - + - - - - -
- - + - - - + + - +
+ + - - - - - + + -
+ - - + - + - + - +
- - - + - - + - - +
+ + + - + - - + + -
- - + + - - + + - +
+ + - - + + - - - -
- + + - - + - + - +
- + + + - - - - - -
Maximum no of Plus Signs in row or column is :6
_____________Could you rate me well.Plz .Thank You