Create the TestArrayMethods class to test the methods in ArrayMethods.java. In t
ID: 3631668 • Letter: C
Question
Create the TestArrayMethods class to test the methods in ArrayMethods.java. In this file, you should:Create an array of char with 100 items
Initialize your array with initArray
Print the array on the screen
Find and print for each letter how many times it appears in the array (use countLetters).
ArrayMethods.java:
public class ArrayMethods
{
public static void initArray(char[] array)
{
// for each element in the array
for(int i = 0; i < array.length; i++)
// give it a random letter, A through E
array[i] = (char)('A'+(int)(Math.random()*('E'-'A'+1)));
}
public static int countLetters(char[] array, char letter)
{
int count = 0;
// look through array for letter
for(char c : array)
if(c == letter)
count++;
return count;
}
}
Explanation / Answer
public class TestArrayMethods {public static void main(String[] args) {char[] array=new char[100]; int i; char c; ArrayMethods.initArray(array); System.out.println("The characters"); for(i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.