Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a Java program for programming exercise 8.10, page 309 (nam

ID: 3684137 • Letter: D

Question

Design and implement a Java program for programming exercise 8.10, page 309 (name it LargestRowColumn). The program randomly fills in 0s and 1s into a 4-by-4 matrix, prints the filled matrix, and then finds the first row and first column with the most 1s. Notice there could be other rows and columns the most ones. We just need to find the first row and first column with the most 1s. Format your output following the given sample run. Design the main method of your program such that it allows the user to re-run the program in the same session (run). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

Explanation / Answer

LargestRowColumn.java


import java.util.Random;

public class LargestRowColumn {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       int row = 4;
       int col = 4;
       int a[][] = new int[row][col];
       Random r = new Random();
       while(true){
       for(int i=0;i<row;i++){
           for(int j=0;j<col;j++){
               a[i][j] = r.nextInt(2);
           }
       }
       displayArray(a, row, col);
       int rowmaxOne = 0;
       int colmaxOne = 0;
       int count = 0;
       int rowNum =0, colNum =0;
       for(int i=0;i<row;i++){
           count = 0;
           for(int j=0;j<col;j++){
               if(a[i][j] == 1){
                   count++;
               }
           }
           if(count > rowmaxOne){
               rowmaxOne = count;
               rowNum = i;
           }
       }
       System.out.println("First Row with Most 1s : "+rowNum + " Count : "+rowmaxOne);
       for(int i=0;i<row;i++){
           count = 0;
           for(int j=0;j<col;j++){
               if(a[j][i] == 1){
                   count++;
               }
           }
           if(count > colmaxOne){
               colmaxOne = count;
               colNum = i;
           }
       }
       System.out.println("First Column with Most 1s : "+colNum + " Count : "+colmaxOne+" ");
       System.out.println("Please enter your choice : Press q for quit or press any key for continue... ");
       char c = in.next().charAt(0);
       if(c == 'q' || c == 'Q'){
           break;
       }
       }
   }

   public static void displayArray(int a[][], int row, int col){
       System.out.println("The 4 X 4 matrix is : ");
       String str = "";
       for(int i=0;i<row;i++){
           for(int j=0;j<col;j++){
               str = str + a[i][j]+" ";
           }
           str = str + " ";
       }
       System.out.println(str);
   }
}

Output:

The 4 X 4 matrix is :
0 0 1 0
1 0 0 1
1 0 0 0
1 0 0 1

First Row with Most 1s : 1 Count : 2
First Column with Most 1s : 0 Count : 3

Please enter your choice : Press q for quit or press any key for continue...
1
The 4 X 4 matrix is :
1 1 0 1
1 1 0 1
1 1 1 0
0 1 0 1

First Row with Most 1s : 0 Count : 3
First Column with Most 1s : 1 Count : 4

Please enter your choice : Press q for quit or press any key for continue...
2
The 4 X 4 matrix is :
0 1 1 0
0 1 0 1
1 1 1 1
0 0 0 0

First Row with Most 1s : 2 Count : 4
First Column with Most 1s : 1 Count : 3

Please enter your choice : Press q for quit or press any key for continue...
4
The 4 X 4 matrix is :
0 0 1 0
0 1 1 1
1 1 1 1
0 1 0 1

First Row with Most 1s : 2 Count : 4
First Column with Most 1s : 1 Count : 3

Please enter your choice : Press q for quit or press any key for continue...
5
The 4 X 4 matrix is :
1 1 0 0
0 0 0 1
1 1 1 0
1 0 0 0

First Row with Most 1s : 2 Count : 3
First Column with Most 1s : 0 Count : 3

Please enter your choice : Press q for quit or press any key for continue...
6
The 4 X 4 matrix is :
1 1 0 1
1 0 0 0
1 1 1 0
0 1 0 0

First Row with Most 1s : 0 Count : 3
First Column with Most 1s : 0 Count : 3

Please enter your choice : Press q for quit or press any key for continue...
q

Note: Row numbers will start from 0 to 3 and Column numbers will also start from 0 to 3 in this 4 X 4 matrix.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote