Thursday, January 24, 2013

lesson 19 excercise


Lesson 19 Exercise
Matthew walker
Balcar 3A 22/01/13
1. Write code that will create an array of 300 BankAccount objects. You are only to
instantiate two of them. The object with index 47 should have a beginning balance of
$92, and index 102 should have $1007. The name of your array will be ba.

BankAccount ba[]= new BankAccount[300];
ba[47] = new BankAccount ($92);
ba[102] = new BankAccount ($1002);

2. Write an if statement that will decide if k[3] is equal to jm[5] where it is assumed that k
and jm are numeric arrays.
If ( k[3] = = jm[5] )

3. Write an if statement that will decide if s[2] is equal to ss[19] where it is assumed that s
and ss are String arrays.
If (s[2].equals(ss[19]))

4. Write an if statement that will decide if cir[2] is equal to cirr[10] (with regard to content)
where it is assumed that cir and cirr are object arrays of type Circle.
If (cir[2].equals(cirr[10]))

5. What’s wrong with the following code?
char months[];
months[0] = ‘j’;
months has not been initialized yet

6. String suv[] = new String[20];
j = 0;
while(j < 17 )
    {
       suv[j] = “Hello”;
       j++;
    }
What is the logical size of the suv array? 20
What is the physical size of the suv array? 17

7. Write code using toCharArray to convert String d = “The quick brown fox jumped over
the lazy dog.” into the character array qbf.
char qbf[];
String d = "The quick brown fox jumped over the lazy dog.";
qbf = d.toCharArray();

8. double rub[] = {23.0, -102.1, 88.23, 111, 12.02, 189.119, 299.88};
double dub[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Write a single line of code (using arraycopy) that will result in dub looking like this:
  1. {1, 2, 3, 4, 111, 12.02, 189.119, 8, 9}]

9. double[] zz, top = {12.1, 13.1, 14.1, 15.1, 18};
zz = top;
zz[2] = 99;
top[3] = 100.2;
Show what “both” arrays would look like at the completion of the above code.

10. char[] a, b;
a = “Groovy dude”.toCharArray( );
b = “I like this”.toCharArray( );
System.arraycopy(a, 1, b, 0, 4);
What do the two arrays look like at the completion of this code?

11. What must be true of any array before we can use Arrays.binarySearch( )?

12. Write code that will establish an array called myArray having the following elements,
{189.01, 2000, -32, 56, 182, 2}. Then sort the array.

13. Assume the array myArray in #12 has been correctly sorted. What would be printed with
the following?
System.out.println( Arrays.binarySearch(myArray, 56) );
System.out.println( Arrays.binarySearch(myArray, 102) );

14. What does the following print?
int xc[] = {123, 97, -102, 17};
int pk[] = {123, 79, -102, 17};
int gs[] = {123, 97, -102, 17};
System.out.println( Arrays.equals(xc, pk) + “\n” + Arrays.equals(xc, gs));
False
True

15. What does the following print?
int pickle[] = {1, 2, 3, 4, 5, 6, 7, 8};
Arrays.fill(pickle, -1);
System.out.println( pickle[4] );
-1

16. If a command line reads, java BigClass Munster Herman dude, what will the following
line inside the main method print?
System.out.println(“Name=” + args[2] +args[1] );

17. What’s printed by the following?
int px[] = {3, 4, 5, 6, 7, 8, 9};
System.out.println( px[ px[1] + 1 ]);
8

18. Write code using the “for-each” style of a for loop that will accumulate and print the
product of the state variables int jj within each object of object array objArray. Assume
the objects are created from the class DummyClass.

1 comment: