Q1: Write Java statements that do the following: Reviewed Tutorial about Arrays a. Declare an array alpha of 15 components of the type int int alpha[] = new int[15]; b. Print the value of the tenth component in the array alpha System.out.print(alpha[9]); c. Set the value of the fifth component in the array alpha to 35 alpha[4]=35; d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteen component of the array alpha alpha[8]=alpha[5] +alpha[12]; e. Set the value of the fourth component of the array alpha to three time the value of the eight component of the array alpha alpha[3]=3*alpha[7] f. Print the alpha so that five components per line are printed int count =0; for(int i=0;i<alpha.length;i++) System.out.print(alpha[i]+" "); count++; if(count==5) System.out.println(); count=0; Q2: Write Java statements that do the following: a. Initialize each of the five elements of array g to 8. int[] g=new int[5]; for(int i=0;i<g.length;i++) g[i]=8;
b. Total the 100 elements of floa ng-point array c. float sum=0.0f; for(int i=0; i<c.length;i++) or sum=sum+c[i]; float sum =0.0f; for( float x : c) sum=sum+x; c. Copy 11-element array a into the first portion of array b, which contains 34 elements. for(int i=0; i<a.lenght.i++) b[i]=a[i]; Q3: what is the output of the following java code: a) int i; int [] array1 = 1,2,3; int [] array2 = 1,2,3; for(i = 0; i < array1.length ; i ++) if (array1[i] == array2[i]) System.out.println("element number " + i + " are equal "); else System.out.println("element number " + i + " are not equal "); if (array1 == array2) System.out.println(" the two array are equal "); else System.out.println("the two array are not equal "); Answer: element number 0 are equal element number 1 are equal element number 2 are equal the two array are not equal
b) int i; int [] array = 1,2,3,4,5; for (i =0 ; i<= array.length -1; i+=2) System.out.println(array[i]); 1 3 5 BUILD SUCCESSFUL (total me: 0 seconds) c) int i; int [] list =new int [5]; for (i =0 ; i<5; i++) list[i] = 2 * i +5; if (i + 2 == 0) list[i] = list[i] - 3; for (i =0 ; i<5; i++) System.out.println(list[i]); Answer: 5 7 9 11 13
d) int [] list =new int [6]; list[0] =5; for (int i =0 ; i<6; i++) list[i] = i * i +5; if (i > 2) list[i] = 2 *list[i] - list[i-1]; for (int i =0 ; i<6; i++) System.out.println(list[i]); Answer: 5 6 9 19 23 37 e) int[][] b = new int[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) b[i][j] = 2 * (i + j) + 4; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) System.out.print(b[i][j] + "\t"); System.out.println();
Answers: 4 6 8 6 8 10 8 10 12 BUILD SUCCESSFUL (total me: 0 seconds) f) public static void main(string[] args) int[] array = 87, 68, 94, 100, 83; int total = 0; for (int number : array) total += number; System.out.printf("Total of array elements: %d%n", total); g)- public class JavaApplica on17 public static void main(string[] args) int[] array = 1, 2, 3, 4, 5; modify1(array); for (int value : array) System.out.printf(" %d", value); modify2(array[3]); System.out.prin ("array[3] a er modifyelement: %d%n", array[3]); public sta c void modify1(int x[]) for (int counter = 0; counter < x.length; counter++) x[counter] *= 5; public sta c void modify2(int e)
e *= 2; System.out.printf( "\nvalue of element in modifyelement: %d%n", e); // end class h) public static void main(string[] args) int[][] array1 = 1, 2, 3, 4, 5, 6; System.out.println("Values in array1 by row are"); outputarray(array1); public static void outputarray(int[][] array) for (int row = 0; row < array.length; row++) for (int column = 0; column < array[row].length; column++) System.out.printf("%d ", array[row][column]); System.out.println();
Q4 Write a method createarithmeticsseq that prompts the user to input two numbers, first and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithme c sequence. It also output the arithmetic sequence. For example if first = 21 and diff = 5, the arithmetic sequence is 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 public static void createarithmeticsseq () int first; int differ; Scanner in=new Scanner(System.in); System.out.print("Enter first:"); first =in.nextint(); System.out.print("Enter differe:"); differ=in.nextint(); int[] arr=new int[16]; for(int i=0;i<arr.length;i++) arr[i]=first; first+=differ; for(int i=0;i<arr.length;i++) System.out.print(arr[i]+ " "); Q5: Write a method that receives array and integer x, the method should search for integer in array and return the index of the integer in array. If not found the integer in array the method should return -1 public static void main(string[] args) int y=3; int[] myarray=4,3,2,1; System.out.print(Mymothod(myarray, y)); // end of main public static int Mymothod(int []arr, int x) for(int i=0;i<arr.length;i++) if (x== arr[i]) return i; return -1;