Java Problem 1. (20 points) What is output of the following code and why? public
ID: 3668683 • Letter: J
Question
Java
Problem 1.(20 points)
What is output of the following code and why?
public class Test {
public static void main(String[] args) {
int[] x = {0120, 200, 016};
for (int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}
Problem 2.(20 points)
(Identical array) The arrays list1 and list2 are identical if they have the same size and content only (same content with different order still consider as identical). Write a method that returns true if list1 and list2 are identical, using following header:
public static boolean equals(int[] list1, int[] list2)
Write a test program that prompts the user to enter two lists of integers and displays whether the two are identical. Here are the sample runs.
Here are sample runs of the program:
Enter list1: 5 2 -5 6 6 1
Enter list1: 5 -5 2 6 1 6
Two lists are identical
Enter list1: 5 5 5 6 6 1
Enter list1: 5 2 5 6 6 1
Two lists are not identical
Enter list1: 5 -5 5 6 6 1
Enter list1: 5 2 5 6 6 1
Two lists are not identical
Problem 3.(20 points)
(Largest row and column) Write a program that randomly fills in 0s and 1s into a 4-by-4 matrix, prints the matrix, and finds the first row and column with the most 1s.
Here is a sample run of the program:
0011
0011
1101
1010
The largest row index: 2
The largest column index: 2
Explanation / Answer
Answer for Question 1:
It is printing 18, 200, 14
Answer for Question 2:
Java method for compare two lists are identical or not with equal sizes but different elemenets as per given sample output..See the below method..
public static boolean equals(int[] list1, int[] list2)
{
if (list1 == null && list2 == null){
return true;
}
if((list1 == null && list2 != null)
|| list1 != null && list2 == null
|| list1.size() != list2.size()){
return false;
}
//to avoid messing the order of the lists we will use a copy
//as noted in comments by A. R. S.
list1 = new ArrayList<String>(list1);
list2 = new ArrayList<String>(list2);
Collections.sort(list1);
Collections.sort(list2);
return list1.equals(list2);
}
Answer for Question 3:
See the below java code will work as per given problem statement in question3 ..this code will print the first row and first coloum which is having more number of ones in a given 4 by 4 matrix..
see the code below..
public class binary {
public static void main(String[] args) {
// create 4 by 4 matrix
int[][] matrix = new int[4][4];
// generate 1's and 0's for each each rows and columns
// and track largest row index with the most ones
int largestRI = 0;
int largest = -1;
for (int i = 0; i < matrix.length; i++) {
int rowCount = 0;
for (int k = 0; k < matrix[i].length; k++) {
matrix[i][k] = (int)(Math.random() * 2);
rowCount += matrix[i][k];
}
if (rowCount > largest) {
largestRI = i;
largest = rowCount;
}
}
// find largest column index
int largestCI = 0;
largest = -1;
for (int k = 0; k < matrix[0].length; k++) {
int columnCount = 0;
for (int i = 0; i < matrix.length; i++) {
columnCount += matrix[i][k];
}
if (columnCount > largest) {
largest = columnCount;
largestCI = k;
}
}
// display matrix
for (int i = 0; i < matrix.length; i++) {
for (int k = 0; k < matrix[i].length; k++) {
System.out.printf("%d", matrix[i][k]);
}
System.out.printf(" ");
}
System.out.println("The largest row index: " + largestRI);
System.out.println("The larges column index: " + largestCI);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.