Vous êtes sur la page 1sur 5

Introduction to Java Exercise Packet #6: Arrays

1. What is displayed?

int [] cosmic = { 3, 6, 5, 8 }; System.out.println( cosmic[ 2 ] );

2. What is displayed?

String [] gamma = { "do", "re", "mi" }; System.out.println( gamma.length );

3. What is displayed?

String [] arr = { "one", "five", "eight" }; System.out.println( arr[ 1 ] );

4. What is displayed?

int [] arr = { 5, 88, 1 }; for ( int n = 0; n <= 2; n++) System.out.println( arr[n] );

5. What is displayed?

int [] arr = { 70, 6, 2 }; for ( int n = 2; n >= 0; n--) System.out.println( arr[n] );

6. What is displayed?

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 );

10.

If a is { 5, 8, 6, 7 }, what does this method

public int m33( int [] a ) { int m = a[0];

 

return?

 

for ( int n = 1; n < a.length; n++ ){ if ( a[n] > m )

11.

If a is { 13 }, does this method crash? If

 

m

= a[n];

yes, why? If no, what does the method return?

 

}

return m;

}

12.

If a is { 188.7, 54.8, 77.2 }, what does this

public double m99( double [] a ){ double mm = 0; for ( int i = 0; i < a.length; i++ ){ if ( a[i] > mm ) mm = a[i];

method return?

13.

If a is { -6.3, -32.1, -4, -6.2 }, what does

this method return?

 

}

return mm;

}

14.

If a is { 5, 6, 6, 5, 7 }, what does this

public boolean m77( int [] a ){ for ( int i = 0; i < a.length-1; i++ ){ if ( a[i] > a[i+1] ) return false;

}

method return?

15. This method returns true if the values in the array are arranged in

a) non-decreasing order

 

return true;

b) non-increasing order

}

c) increasing order

d) decreasing order

16. If a is { 8 }, 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;

}

17. Give an example of an array that causes

this method to return true. The array must have a length of 4.

 

return true;

}

18.

If a is { 9, 5, 2, 6, 5, 7 }, what does this

public int m55( int [] a ) { int k = 0; for ( int n = 1; n < a.length; n++ ){ if ( a[n] < a[k] ) k = n;

}

method return?

 

return k;

}

19. What does this method return?

public Monster[] movie() {

a) A Monster object

 

//

code

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.

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 );

 

w.change( 2, 3 );

}

System.out.println( w ); //

public void change( int n1, int n2 ) { if ( n1 < 0 || n1 >= ltrs.length ) return; if ( n2 < 0 || n2 >= ltrs.length ) return;

w.change( 3, 4 );

System.out.println( w ); //

w.change( 3, 0 );

String temp = ltrs[ n1 ]; ltrs[ n1 ] = ltrs[ n2 ]; ltrs[ n2 ] = temp;

System.out.println( w ); //

}

}

public String toString() { String temp = ""; for ( int n = 0; n < ltrs.length; n++ ) temp += ltrs[n]; return temp;

}

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 { private int [] grades;

public Test ( int num ) { // create an int array of length num, assign this array to grades // Fill it with random numbers from 70 to 100

project. Add a new file named Test. Complete the code for the Test class

}

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;

}

}

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).You are going to rewrite the above main method as follows: Print each test's average, each

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:should have a random number of grades (between 3 and 7). Average Individual Grades   86

Average

Individual Grades

 

86

76

96

81

88

97

79

87

96

83

94

90

78

81

85

87

85

89

81

86

77

85

97

85

91

83

90

77

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.

23.

This is a version of Yahtzee.

public class Y_Guy { private String name; private int [] dice;

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.

public Y_Guy( String s ) { name = s; dice = new int[5]; roll();

}

public void roll(){ // assign random values, 1 - 6, to dice

Enter player 0's name: Craig Enter player 1's name: Sarah Enter player 2's name: Alex Enter player 3's name: Chris

}

public boolean won(){ returns true if all 5 dice match

}

(many many rolls)

 

public String toString() { String temp = name + " rolled a\t"; for ( int n = 0; n < dice.length; n++ ) temp += dice[n] + " ";

Craig rolled a 2 Sarah rolled a 2

4

6

5

3

5

4

5

6

Alex rolled a

4

4

5

1

3

Chris rolled a 3

4

2

1

6

Craig rolled a 2 Sarah rolled a 5

2

2

2

2

 

return temp;

5

2

5

6

}

Alex rolled a

1

1

3

2

2

}

Chris rolled a 3

5

2

3

2

Game Over.

24.

Complete the Words class. Test it

public class Words { private String [] str;

with this:

String [] a = { "It", "was", "the", "best of", "times" };

public Words( String [] s ) { str = new String[ s.length ]; for ( int n = 0; n < s.length; n++ ) str[n] = s[n];

}

Words w1 = new Words( a );

System.out.println(w1.countAllLetter());

System.out.println(w1.getLongest());

public int countAllLetter(){ // returns a count of all characters in the array

System.out.println(w1.lastLetters() );

}

This should display:

public String getLongest() { // returns the longest string in the array

20

}

 

best of

tsefs

public String lastLetters() { // returns a string of the last letters in str

}

}