100% found this document useful (1 vote)
250 views

Arrays

The document discusses arrays in Java and includes short answer exercises and programming exercises related to arrays, such as declaring and initializing arrays, accessing array elements, looping through arrays, finding the largest/smallest elements and their indexes, permutations, and grading a true/false test by calculating student scores based on correct, wrong, and blank answers. Methods are used to return results like the largest element, smallest index, and total student points from arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
250 views

Arrays

The document discusses arrays in Java and includes short answer exercises and programming exercises related to arrays, such as declaring and initializing arrays, accessing array elements, looping through arrays, finding the largest/smallest elements and their indexes, permutations, and grading a true/false test by calculating student scores based on correct, wrong, and blank answers. Methods are used to return results like the largest element, smallest index, and total student points from arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

WXES1116/1114 Sem 2/2012 Arrays Part A: Short answer exercises 1.

Write Java statements that do the following : a) Declare an array alpha of 15 elements of type int. b) Output the value of the tenth element of the array alpha. c) Set the value of the fifth element of the array alpha to 35. d) Set the value of the ninth element of the array alpha to the sum of the sixth and thirteenth elements of the array alpha. e) Set the value of the fourth element of the array alpha to three times the value of the eighth element, minus 57. f) Output alpha so that five elements per line are printed. 2. Suppose list is an array of six elements of type int. What is stored in list after the following Java code executes ? for (int n = 0; n < 5; n++) { list[n] = 2 * n + 5 ; if ( n % 2 == 0) list[n] = list[n] 3 ; } 3. Consider the following declarations. following code executes ? int [] [] beta = new int[3][3] ; a) for (int n = 0 ; n < 3 ; n++) for (int j = 0 ; n <3 ; j++ ) beta[n][j] = n + j ; Part B : programming exercises 4. (Finding the largest element). Write a program with a method, that finds the largest element in an array of integers. Use {10, 111, 4, 5, 10,-120, 2, 22} to test the method. 5. (Finding the index of the smallest element) Write a program that has a method to return the index of the smallest element in an array of integers. If there are more than one such element, return the smallest index. Use {5, 9, 4,-22 ,10, 100,2,-22} to test the method. 6. (Finding the index of the largest element) Write a program that has a method to return the index of the largest element in an array of integers. If there are more than one such elements, return the biggest index or the last occurrence of the same largest element. Use {1,2,4,5,10,100, 2, 100, -22} to test the method.
Arrays

What is stored in beta after the

7. In a gymnastics or diving competition, each contestants score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that allows the user to enter eight judgess scores and then outputs the points received by the contestant. Format your output with two decimal places. A judge awards points between 1 and 10, with 1 being the lowest and 10 being the highest. For example, if the scores are 9.2, 9.3, 9.0, 9.9, 9.5, 9.5, 9.6 and 9.8, the contestant receives a total of 56.90 points.
8. A permutation of three letters, a, b, and c is any arrangement of these letters in a row. For example, some of the permutation of these characters are abc, bca, and cab. The number of distinct permutations of three different characters is 6. Design a solution where given any three letters, you could produce the six distinct permutation of those letters.

9.

The teacher in your class needs help grading a True/False test and generating a grade for a student. The test consist of twenty questions with the following answer : TFFTFFTTTTFFTFTFTFTT. Store this as an array of character. Your program will ask the user to enter the answer for each of the 20 questions , either a T or F or a blank. Each correct answer is awarded 2 points, each wrong answer gets negative one point, and no answer gets zero points. Calculate the total points obtained by the student, display it followed by the grade. Assume the following grade scale : 90%-100% A 80%-89% B 70%-79% C 60%-69% D <59% F Use this test data : TFTFTFTT TFTFTFFTTFT (take note : there is one blank for question 9 ; after the underlined T)

Arrays

You might also like