90% Wed Apr 12 9:47:29 AM Q O E Chrome File Edit View History Bookmarks People W
ID: 3816144 • Letter: 9
Question
90% Wed Apr 12 9:47:29 AM Q O E Chrome File Edit View History Bookmarks People Window Help C Computer Science question nbox jking199@students. A11.pd ksuweb.kennesaw.edu hhaddad/Spring2017/CS1301/A11.pd Apps k D cs 130 LIowlLink-Online Ca CompTIA A+ Certifi PlayStation Vue D Y2U40DAXNDgyZS... Program YouTube home M Inbox king 199@s A11.pdf 50 45 37 Program 2 points: Design and implement a Java program for programming exercise 8.10, page 309 estRowColumn). The program randomly fills in 0s and 1s into a 4-by-4 matrix, nts the filed name it La matrix, and then finds the first row and first column with the most 1s. Notice there could be other rows and umns the most ones. We just need to find the first row and first column with the most 1s. Format 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 outpu properly. Use escape characters and formatting objects when applicable Program 10 points Design and implement a Java program for programming exercise 8.27, page 315 name it columnsorting) as described in the problem statement. Write method sortColumns( as specified (you may change the element type to integer). Notice that this method returns a new array, the original array remains unchanged. To test this method, the main method of your program prompts the user to enter a two- dimensional array and displays the original array followed by the column-sorted array as shown in the 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 Program P4 (10 points Design and implement a Java program for programming exercise 8.29, page 316 (name it IdenticalArrays) as described in the problem statement. Write method equals as specified. T test this method, the main method of your program prompts the user to enter 2 two-dimensional arrays of integers and displays whether the two arrays are identical or not. Follow the given sample run to format your outputs. Design the main method of your program such that it allows the user to re-run the program with different inputs (i e., use a loop) Document your code, and organize and space the outputs property. Use escape characters and formatting objects when applicable. Submission Instructions 1. Assignment due date: Wednesday 4112/2017 at the beginning of the lab session 2. Email your code (only the java files) to the instructor (hhaddad@kennesaw.e edu) and the graderExplanation / Answer
LargestRowColumn .java
import java.util.Random;
import java.util.Scanner;
public class LargestRowColumn {
private static int ROW = 4;
private static int COL = 4;
private static Random rn = new Random();
// fill an array with 1 and 0's randomly
public static void fillArray(int matrix[][])
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
matrix[i][j] = rn.nextInt(2);
}
}
}
// counting max number of 1's in a row and returning first row with max 1's
public static int findMaxRow(int matrix[][])
{
int maxRow = 0;
int maxSum = 0;
for(int i = 0; i < ROW; i++)
{
int sum = 0;
for(int j = 0; j < COL; j++)
{
sum += matrix[i][j];
}
if (sum > maxSum)
{
maxSum = sum;
maxRow = i;
}
}
return maxRow;
}
// counting max number of 1's in a col and returning first col with max 1's
public static int findMaxCol(int matrix[][])
{
int maxCol = 0;
int maxSum = 0;
int i;
for(i = 0; i < COL; i++)
{
int sum = 0;
for(int j = 0; j < ROW; j++)
{
sum += matrix[j][i];
}
if (sum > maxSum)
{
maxSum = sum;
maxCol = i;
}
}
return maxCol;
}
// printing array in given format
public static void print(int matrix[][])
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
public static void main(String[] args)
{
int matrix[][] = new int[ROW][COL];
Scanner sc = new Scanner(System.in);
// looping for multiple run based on user choice
while(true)
{
fillArray(matrix);
print(matrix);
int row = findMaxRow(matrix);
int col = findMaxCol(matrix);
System.out.println("The largest row index: " + row);
System.out.println("The largest col index: " + col);
System.out.print("Do you want to try again (y/n)?: ");
String choice = sc.nextLine();
if (choice.toLowerCase().equals("n"))
{
System.out.println("Exiting...");
break;
}
}
sc.close();
}
}
Sample output:
0100
0110
1101
1000
The largest row index: 2
The largest col index: 1
Do you want to try again (y/n)?: y
1011
1100
1001
1010
The largest row index: 0
The largest col index: 0
Do you want to try again (y/n)?: y
1011
1010
1001
1010
The largest row index: 0
The largest col index: 0
Do you want to try again (y/n)?: n
Exiting...
Please give positive rating if this answered your question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.