Worksheet 4 by Kim Ludwig

Where in a class would the following statements normally be?

 

1.)    import java.util.ArrayList; (Gain access to the library)

____________________________

2.)    private ArrayList notes; (Define field notes as type ArrayList class)

____________________________

3.)    notes = new ArrayList() (Create an instance of ArrayList)

____________________________

 

4.)    Circle the result of the following:

       

int x = 0;                                                   a.     X = 12

while(x < 12) {                                           b.     X = 11

x+=3;                                                       c.      an Error

}                                                              d.     x = 0

5.)                                          

int x;                                                         a.     X = 12

while(x < 12) {                                           b.     X = 11

x+=1;                                                       c.      an Error

}                                                              d.     x = 0

 

6.)    Identify and correct the errors in the following code. (Hint: There are 3 errors)

public void indexPrintLibrary (){

        int index;

        while(index > library.size()){

            System.out.println("Book Number: " + (index));

            ((Book)library.get(Book)).printBook();

            i++;

       }

7.)    What will be returned by code below? ____________________

(Since you do not know the contents of myArray, don’t be specific -- just decide what the method is doing!)

public int total (){

        ArrayList myArray = new ArrayList();

        int x = 0;

        Iterator iter = myArray.iterator();

        while(iter.hasNext()){

                (iter.next()).print();

        x++; // same as x = x + 1;

        }

        return x;

    }

8.)    Why is it necessary to initialize a variable to a specific value before using it?

____________________________________________________________

9.)    What does an uninitialized variable contain? Do we know? 

____________________________________________________________

10.) Tell the value of x after each time the following loop is executed.

 


int x;                                                         a. )  x = ___________

x = 3;                                                        b. )   x = ___________

y = 10;                                                      c. )    x = ___________

while ( x < y) {                                           d. )   x = ___________

        x += y - 2;                                         e. )   x = ___________

}

11.)  Explain (specifically) what each line of the following code is doing.

a.)    String Books[ ] = new String[3];      ___________________________

b.)    Books[ 0 ] = "Instant ASP Components";_______________________

c.)     Books[ 1 ]  = “Mastering XML”;        ___________________________

d.)    System.out.println(Books[1]);         ___________________________

 

 

 

12.)  When is it appropriate to use a for loop?

______________________________________________________

13.)  When is it appropriate to use a while loop?

_____________________________________________________________

 

14.)  Write a for loop that will print all the days of the week.

String days[ ] = new String[7];

days [ 0 ] = "Sun";              days [ 4 ] = “Thurs”;

days [ 1 ] = "Mon";              days [ 5 ] = “Fri”;

days [ 2 ] = “Tue”;              days [ 6 ] = “Sat”;

days [ 3 ] = “Wed”;

 


 

Answers

1.     Before the class header

2.     Where fields are defined or in a method

3.     In a constructor or method

4.     a – the loop will execute 4 times resulting in x = 12

5.     c – because x was not initialized to a value

6.

        public void indexPrintLibrary () {

        int index;                                                              int index = 0;

        while(index > library.size()){                      index < library.size()

            System.out.println("Book Number: " + (index));

            ((Book)library.get(Book)).printBook();       get(index)

            i++;

       }

 

7.     The number of items in the array

8.     Because the variable will hold some unknown value

9.     We do not know because it is not initialized to a value.

 

 

10.

        a.     x = 4

        b.     x = 6

        c.      x = 10

        d.     x = 18

        e.     x = 34

11.

a.     Creates an array of size 3 named Books.)      

b.     Adds the String “Instant ASP Components" to the array at   index 0

c.)     Adds the String “Mastering XML " to the array at index 1

d.)    Prints “Mastering XML” to the terminal window

12.    When we know exactly how many times the loop will execute

13.    When we do not know exactly how many times the loop will execute

14.    for (int i = 0; i < 7; i++) {

            System.out.println("Day: " + days[i]);

        }