Write a static method named countLastDigits that accepts an array of integers as
ID: 3699327 • Letter: W
Question
Write a static method named countLastDigits that accepts an array of integers as a parameter and examines its elements to determine how many of the integers end in 0, how many end in 1, how many end in 2 and so on. Your method will return an array of counters. The count of how many elements end in 0 should be stored in its element [0], how many of the values end in 1 should be stored in its element [1], and so on. For example, if a variable named list refers to an array containing the values {9, 29, 44, 103, 2, 52, 12, 12, 76, 35, 20}, the call of countLastDigits(list) should return an array containing {1, 0, 4, 1, 1, 1, 1, 0, 0, 2} because 1 element ends with 0 (20), no elements end with 1, 4 elements end with 2 (2, 52, 12, 12), and so on.
Test your code with the following class:
import java.util.*;
public class TestCountLastDigits {
public static void main(String[] args) {
int[] list = {9, 29, 44, 103, 2, 52, 12, 12, 76, 35, 20};
int[] count = new int[10];
countLastDigits(list, count);
System.out.println(Arrays.toString(list)); // [9, 29, 44, 103, 2, ...]
System.out.println(Arrays.toString(count)); // [1, 0, 4, 1, 1, 1, 1, 0, 0, 2]
}
// finish me
}
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] list = {9, 29, 44, 103, 2, 52, 12, 12, 76, 35, 20};
int[] count = new int[10];
countLastDigits(list, count);
System.out.println(Arrays.toString(list));
System.out.println(Arrays.toString(count));
}
public static void countLastDigits( int[] list, int[] count)
{
int i;
for(i=0;i<10;i++)
count[i] = 0;
for(i=0;i<list.length;i++)
count[list[i]%10] += 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.