Free Response Practice Question (with solution)- 2D Alphabet Matrix


<-- Back to 2D Balanced Matrix Next to Square Function Matrix -->





2D Alphabet Matrix


Write a Java program that reads a 5 X 5 array of characters and counts and displays the frequency of vowels in it, both row-wise and column-wise. The program should ensure that the user is able to enter only valid alphabets as inputs to the array.

For example: Consider the following array:

The row-wise frequency is:

Row 1: a=2; e=1; i=0; o=0; u=0;

Row 2: a=0; e=0; i=0; o=0; u=2;

Row 3: a=0; e=1; i=0; o=0; u=0;

Row 4: a=0; e=1; i=2; o=0; u=1;

Row 5: a=1; e=0; i=1; o=2; u=0;


And, the column-wise frequency is:

Col 1: a=1; e=0; i=1; o=0; u=0;

Col 2: a=0; e=1; i=1; o=0; u=0;

Col 3: a=1; e=1; i=1; o=0; u=1;

Col 4: a=0; e=0; i=0; o=1; u=2;

Col 5: a=1; e=1; i=0; o=1; u=0;


Solution:

View Solution

Alphabet2DArray.java


import java.util.InputMismatchException;
import java.util.Scanner;

public class Alphabet2DArray{

    public static void main(String args[])
    {
    int ROW = 5;
    int COL = 5;
    char[][] charArr = new char[ROW][COL];
    try (Scanner input = new Scanner(System.in)) {
    char alpha;
    System.out.println("Enter alphabets for "+ ROW + " X " + COL +" matrix: ");
    for (int i=0; i< ROW ; i++)
    {
        for (int j=0; j < COL; j++)
        {
            System.out.print("Enter the ["+ i +"] ["+j+"] valid alphabet: ");
            alpha = input.next().charAt(0);
    
            if (Character.isAlphabetic(alpha))
            {
                charArr[i][j] = alpha;
            }
            else
            {
                System.out.println("You must enter valid alphabet only!");
                j--;
            }
        }
    }
        System.out.println("Alphabets in the matrix are: ");
    
        for (int k=0; k < ROW; k++)
        {
            for (int j=0; j < COL; j++)
            {
                System.out.print(charArr[k][j]+ " ");
            }
            System.out.println();
        }
        System.out.println("Rowwise frequency of alphabets is: ");
        for (int k=0; k < ROW; k++)
        {
            int countA=0;
            int countE=0;
            int countI=0;
            int countO=0;
            int countU=0;
            
            for (int j=0; j < COL; j++)
            {
                    if (Character.toUpperCase(charArr[k][j]) == 'A') countA++;
                    if (Character.toUpperCase(charArr[k][j]) == 'E') countE++;
                    if (Character.toUpperCase(charArr[k][j]) == 'I') countI++;
                    if (Character.toUpperCase(charArr[k][j]) == 'O') countO++;
                    if (Character.toUpperCase(charArr[k][j]) == 'U') countU++;
            }
            System.out.println("Row "+k+":"+" a:"+countA+" e:"+countE+" i:"+countI+" o:"+countO+" u:"+countU);
        }
     
    System.out.println("Colwise frequency of alphabets is: ");
    for (int k=0; k < ROW; k++)
    {
        int countA=0;
        int countE=0;
        int countI=0;
        int countO=0;
        int countU=0;
        
        for (int j=0; j < COL; j++)
        {
                if (Character.toUpperCase(charArr[j][k]) == 'A') countA++;
                if (Character.toUpperCase(charArr[j][k]) == 'E') countE++;
                if (Character.toUpperCase(charArr[j][k]) == 'I') countI++;
                if (Character.toUpperCase(charArr[j][k]) == 'O') countO++;
                if (Character.toUpperCase(charArr[j][k]) == 'U') countU++;
        }
        System.out.println("Row "+k+":"+" a:"+countA+" e:"+countE+" i:"+countI+" o:"+countO+" u:"+countU);
    }
    } catch (InputMismatchException  e)
     {
        System.out.println("Error reading input");
     }
    }// end of main
}




Sample Output:

View Output



Java project files (with Runner code):


<-- Back to 2D Balanced Matrix Next to Square Function Matrix -->