Write a method that displays an nbyn matrix of lowercase cha

Write a method that displays an n-by-n matrix of lowercase characters a - z using the following method header: public static void printMatrix(int n) Each element is a lowercase character between a and z, which is generated randomly. Also write how this method would be called in your main method. You can name your program CharacterMatrix.java In the main method, use a while loop to continuously prompt the user whether he/she wants to generate a character matrix, if yes, prompt the user to enter number n to display the n-by-n matrix. Check the sample output below. If you got an infinite loop, press CTRL + C to stop the execution. Include header comments formatted exactly as shown below but with your name, student ID, and section number (i.e., these comments should be the FIRST LINES in your script). Be sure to include the Honor Code statement. Your electronic submission of the program file will represent your endorsement of the Honor Code Statement./* Course: CSCl 111, Section X Student Name: Jane Doe Student ID: 12345678 Program 5 Due Date: In keeping with the Honor Code of UM, I have neither given nor received assistance from anyone other than the instructor. Program Description: */Before each significant step, provide a comment explaining the step (do not comment every line of code). Do you want to generate an n-by-n matrix of a-z (Enter y for yes and n for no)? y Enter n to generate an n-by-n matrix of a-z: 5 n z m x c l c h m b x y e d r a y d t b l o p v m Do you want to generate an n-by-n matrix of a-z (Enter y for yes and n for no)? y Enter n to generate an n-by-n matrix of a-z: 3 q u z e q w q k r Do you want to generate an n-by-n matrix of a-z (Enter y for yes and n for no)? n >>

Solution


import java.util.Random;
import java.util.Scanner;

/*

Program description:-

*/
public class CharacterMatrix {
  
    public static void main(String argv[])
    {
        String c;int n;
        Scanner sc = new Scanner(System.in);
        while(true)
        {//taking user input..
        System.out.print(\"Do you want to generate nXn matrix of a-z(Enter y for yes, n for no):\");
        c = sc.next();
        if(c.equals(\"n\"))break;
      
        System.out.print(\"\ Enter n to generate nXn matrix of a-z:\");
        n =sc.nextInt();
        char a[][] = new char[n][n];
        generate(a,n);//function calling
        display(a,n);
        }
    }

    private static void generate(char[][] a, int n) {//method which generates random array of a-z
        Random r = new Random();
        int i=0,j=0,k,l;
      
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                l = (r.nextInt(26));//generating a random number between 0 to 25
                k = (97+l);//ascii values start from 97
                a[i][j]=(char)k;
            }
        }
    }

    private static void display(char[][] a, int n) {//method to display array
            int i,j;
            System.out.println();
            for(i=0;i<n;i++){
                for(j=0;j<n;j++)
                {    System.out.print(a[i][j]+\" \");
                }
                System.out.println();
            }
    }
  
  
            }

output:-

run:
Do you want to generate nXn matrix of a-z(Enter y for yes, n for no):y

Enter n to generate nXn matrix of a-z:3

g e z
h b g
i v j
Do you want to generate nXn matrix of a-z(Enter y for yes, n for no):y

Enter n to generate nXn matrix of a-z:5

s v u f a
s q d o e
o v j f b
v b f h r
f h b x i
Do you want to generate nXn matrix of a-z(Enter y for yes, n for no):n
BUILD SUCCESSFUL (total time: 22 seconds)

 Write a method that displays an n-by-n matrix of lowercase characters a - z using the following method header: public static void printMatrix(int n) Each eleme
 Write a method that displays an n-by-n matrix of lowercase characters a - z using the following method header: public static void printMatrix(int n) Each eleme

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site