Sei sulla pagina 1di 5

Introduction to Java Exercise Packet #6: Arrays

1. What is displayed? 2. What is displayed? 3. What is displayed? 4. What is displayed? int [] cosmic = { 3, 6, 5, 8 }; System.out.println( cosmic[ 2 ] ); String [] gamma = { "do", "re", "mi" }; System.out.println( gamma.length ); String [] arr = { "one", "five", "eight" }; System.out.println( arr[ 1 ] ); int [] arr = { 5, 88, 1 }; for ( int n = 0; n <= 2; n++) System.out.println( arr[n] ); int [] arr = { 70, 6, 2 }; for ( int n = 2; n >= 0; n--) System.out.println( arr[n] ); double [] ray = new double[ 4 ]; ray[2] = 5.2; ray[0] = -9; ray[3] = 14.5; ray[1] = 0.0; for ( int n = 0; n < ray.length; n++ ) System.out.println( ray[ n ] ); 7. What is displayed? int [] arr = new int[4]; for (int k = 0; k < arr.length; k++ ) arr[ k ] = 3*k; for (int k = 0; k < arr.length; k++ ) System.out.println( arr[ k ] ); 8. What is displayed? int [] arr = new int[5]; for (int k = arr.length - 1; k >= 0; k-- ) arr[ 4 - k ] = k + 10; for (int k = 0; k < arr.length; k++ ) System.out.println( arr[ k ] ); 9. What is displayed? int x = 0; int [] arr = { 5, -5, 7, 1 }; for ( int n = 0; n < arr.length; n++) x = x + arr[ n ]; System.out.println( x );

5. What is displayed?

6. What is displayed?

Page 1

10. If a is { 5, 8, 6, 7 }, what does this method return?

11. If a is { 13 }, does this method crash? If yes, why? If no, what does the method return?

public int m33( int [] a ) { int m = a[0]; for ( int n = 1; n < a.length; n++ ){ if ( a[n] > m ) m = a[n]; } return m; } public double m99( double [] a ){ double mm = 0; for ( int i = 0; i < a.length; i++ ){ if ( a[i] > mm ) mm = a[i]; } return mm; } public boolean m77( int [] a ){ for ( int i = 0; i < a.length-1; i++ ){ if ( a[i] > a[i+1] ) return false; } return true; }

12. If a is { 188.7, 54.8, 77.2 }, what does this method return?

13. If a is { -6.3, -32.1, -4, -6.2 }, what does this method return?

14. If a is { 5, 6, 6, 5, 7 }, what does this method return? 15. This method returns true if the values in the array are arranged in a) non-decreasing order b) non-increasing order c) increasing order d) decreasing order 16. If a is { 8 }, what does this method return? 17. Give an example of an array that causes this method to return true. The array must have a length of 4.

18. If a is { 9, 5, 2, 6, 5, 7 }, what does this method return?

public boolean m66( int [] a ){ for ( int i = 0; i < a.length-1; i++ ){ if ( a[i]+ 1 != a[i+1] ) return false; } return true; } public int m55( int [] a ) { int k = 0; for ( int n = 1; n < a.length; n++ ){ if ( a[n] < a[k] ) k = n; } return k; } public Monster[] movie() { // code }

19. What does this method return? a) A Monster object b) An array of Monster objects c) A Movie object d) An array of Movie objects e) There is not enough information to answer the question.

Page 2

public class Runner { public static void main(String[] args) { Word w = new Word( "pony" ); System.out.println( w ); // _________

public class Word { private String [] ltrs; public Word( String s ) { ltrs = new String[ s.length() ]; for ( int n = 0; n < ltrs.length; n++ ) ltrs[n] = s.substring( n, n+1 ); } public void change( int n1, int n2 ) { if ( n1 < 0 || n1 >= ltrs.length ) return; if ( n2 < 0 || n2 >= ltrs.length ) return; String temp = ltrs[ n1 ]; ltrs[ n1 ] = ltrs[ n2 ]; ltrs[ n2 ] = temp; } public String toString() { String temp = ""; for ( int n = 0; n < ltrs.length; n++ ) temp += ltrs[n]; return temp; } }

w.change( 2, 3 ); System.out.println( w ); // _________

w.change( 3, 4 ); System.out.println( w ); // _________

w.change( 3, 0 ); System.out.println( w ); // _________ } } 20. What is displayed? Write your answers in the spaces provided above.

Do 5 exercises from Array 1 at javabat.com Do 5 exercises from Array 2 at javabat.com 21. Create a new public class Test { project. Add a new private int [] grades; file named Test. public Test ( int num ) { Complete the code for // create an int array of length num, assign this array to grades the Test class // Fill it with random numbers from 70 to 100 } public int getAverage() { // returns the average of all the grades } public String toString() { String s = ""; for (int n = 0; n < grades.length; n++ ) s += grades[n] + "\t"; return s; } }
Page 3

21 (continued) Test your Test class with the code to the right.

public class TestRunner { public static void main(String[] args) { Test biology = new Test( 3 ); Test algebra = new Test( 5 ); System.out.println( "Grades on the Bio test are " + biology ); int avg1 = biology.getAverage(); System.out.println( "Average grade is " + avg1 ); System.out.println( "Grades on the Algebra test are " + algebra ); int avg2 = algebra.getAverage(); System.out.println( "Average grade is " + avg2 ); } }

22. This builds on exercise 16. You are going to rewrite the above main method as follows: Create an array of 5 Test objects. Each test should have a random number of grades (between 3 and 7). Print each test's average, each test's individual grades, and the average grade for all the tests. The results should be look something like this: Average 86 87 85 86 85 Individual Grades 76 96 81 96 83 94 87 85 89 77 85 97 91 83 90

88 90 81 77

97 78

79 81

The average grade for all tests is 85.8 To calculate the average grade for everyone you need to calculate the average of the average grades.

Page 4

23. This is a version of Yahtzee. Complete the methods. Afterwards write another class and in the main method do the following: - create an array of 4 Y_Guy objects. Use a Scanner object to let the user enter names. - write one while loop that keeps running until one of the four Y_Guys wins. The output should look something like this. Enter player 0's name: Craig Enter player 1's name: Sarah Enter player 2's name: Alex Enter player 3's name: Chris ... (many many rolls) Craig rolled a Sarah rolled a Alex rolled a Chris rolled a Craig rolled a Sarah rolled a Alex rolled a Chris rolled a Game Over. 2 2 4 3 2 5 1 3 4 5 4 4 2 5 1 5 6 4 5 2 2 2 3 2 5 5 1 1 2 5 2 3 3 6 3 6 2 6 2 2

public class Y_Guy { private String name; private int [] dice; public Y_Guy( String s ) { name = s; dice = new int[5]; roll(); } public void roll(){ // assign random values, 1 - 6, to dice } public boolean won(){ returns true if all 5 dice match } public String toString() { String temp = name + " rolled a\t"; for ( int n = 0; n < dice.length; n++ ) temp += dice[n] + " "; return temp; } }

24. Complete the Words class. Test it with this: String [] a = { "It", "was", "the", "best of", "times" }; Words w1 = new Words( a ); System.out.println(w1.countAllLetter()); System.out.println(w1.getLongest()); System.out.println(w1.lastLetters() );

public class Words { private String [] str; public Words( String [] s ) { str = new String[ s.length ]; for ( int n = 0; n < s.length; n++ ) str[n] = s[n]; } public int countAllLetter(){ // returns a count of all characters in the array } public String getLongest() { // returns the longest string in the array } public String lastLetters() { // returns a string of the last letters in str } }
Page 5

This should display: 20 best of tsefs

Potrebbero piacerti anche