Could somebody write comments for each line of code explaining what each line do
ID: 3839619 • Letter: C
Question
Could somebody write comments for each line of code explaining what each line does? I got this code from the book and it tells you which numbers come up the most frequently in arrays. I just need help comprehending each of the loops and if/else variables
import java.util.*;
public class lastIndexOf
{
public static void main(String[] args){
int a[]={27, 15, 15, 11, 27};
int b[]={27, 15, 15, 11, 27,15};
int c[]={27, 15,11, 15, 11, 27};
System.out.println("The mode of a is " + mode(a));
System.out.println("The mode of b is "+ mode(b));
System.out.println("The mode of c is " + mode(c));
}
public static int mode(int[] array) {
int[] spareArray = new int[101];
for (int i = 0; i < array.length; i++) {
spareArray[array[i]]++;
}
int mode = 101;
int count = 0;
for (int i = 0; i < spareArray.length; i++) {
if (spareArray[i] > count) {
count = spareArray[i];
mode = i;
}
}
return mode;
}
}
Explanation / Answer
//Here we are importing util package. This package consists of collection framework, date class etc.
import java.util.*;
// Here we are declaring a public class with name LastIndexOf. As per naming conventions the first letter of a class //name must be a capital letter
public class LastIndexOf
{
//Here we are declaring a main method. This is the from which execution of this program starts
public static void main(String[] args){
//Here we are declaring an integer array 'a' with five elements.
int a[]={27, 15, 15, 11, 27};
//Here we are declaring another integer array 'b' with five elements.
int b[]={27, 15, 15, 11, 27,15};
//Here we are declaring another integer array 'c' with five elements.
int c[]={27, 15,11, 15, 11, 27};
//This statement prints the frequently occuring numbers in the array. Here in this statement mode() methd will be //called. mode() method returns the frequently occuring element in the array.
System.out.println("The mode of a is " + mode(a));
//This statement prints the frequently occuring numbers in the array. Here in this statement mode() methd will be //called. mode() method returns the frequently occuring element in the array.
System.out.println("The mode of b is "+ mode(b));
//This statement prints the frequently occuring numbers in the array. Here in this statement mode() methd will be //called. mode() method returns the frequently occuring element in the array.
System.out.println("The mode of c is " + mode(c));
}
//Here we are declaring a mode method which takes integer array as input and returns frequently occuring number //number in that array
public static int mode(int[] array) {
//Here we are declaring a new integer array with size 101.
int[] spareArray = new int[101];
//Here we are using a for loop. In this for loop we are copying the values of array to spareArray .
for (int i = 0; i < array.length; i++) {
// Here if i value is less than array length then we are inserting thatvalue into spareArray
spareArray[array[i]]++;
}
//Here we are declaring an integer variable with value 101.
int mode = 101;
//Here we are declaring an integer named count with initial value as 0.
int count = 0;
//In this for loop we are traversing through the array and checking how many times the number is occured in the //array
for (int i = 0; i < spareArray.length; i++) {
//Here we are checking if the element at i location is greater than count.
if (spareArray[i] > count) {
// If element greater than count then copy that element into count variable.
count = spareArray[i];
//copying that i values into mode and the loop continues untill condition fails.
mode = i;
}
}
//Here we are returing mode value.
return mode;
}
}