In: Computer Science
Exercise 18: Running Ex. 16 You will write exercise 16 into a program and make it run. Note that there are two parts, 16A and 16B. Type 16A into a program called Exercise16A and type 16B into a program called Exercise16B. Be sure label each section using comments. You may notice that additional code needs to be written for the program to run. For example, Section I assumed that an array was already declared and initialized. Here, you will have to declare and initialize an array with random values to test it.
Section A. Declare and initialize an array firstNames containing the following names: Jane, Jim, Beth, Mary, Sara, and Bob. Remember to choose the correct data type for the array elements.
Section B. Using a FOR loop, output the elements of the array firstNames so that they are displayed on the same line and separated by a space. Use .length to get the length of the array.
public class Exercise16A { public static void main(String[] args) { /** * Declare and initialize an array firstNames containing the * following names: Jane, Jim, Beth, Mary, Sara, and Bob. */ String[] firstNames ={"Jane","Jim","Beth","Mary","Sara","Bob"}; } }
//================================================
public class Exercise16B { public static void main(String[] args) { /** * Declare and initialize an array firstNames containing the * following names: Jane, Jim, Beth, Mary, Sara, and Bob. */ String[] firstNames ={"Jane","Jim","Beth","Mary","Sara","Bob"}; /** * Using a FOR loop, output the elements of the array firstNames * so that they are displayed on the same line * and separated by a space. Use .length to get the length of the array. */ //Since array index starts with 0 so firstNames[0] will contain "Jane" and // so on. for (int i = 0; i <firstNames.length ; i++) { // System.out.print(firstNames[i]+" "); } } }
//Output
//If you need any help regarding this solution ........ please leave a comment ...... thanks