/* CIS 790 ARRAYS PROF. AUCIELLO
save this as parrexc.java. remove 4 tags.
compile and run it. modify as explained below.
you will be understanding arrays, and error trapping.
(parrexc.java)
TOPICS: defining and initializing arrays in one-step.
integer and string arrays.
print vs println
for statement
pre (++i) vs post-increment (i++)
"==" equals "="
---------------------------------------------------------------
Normal output (ascending order)
Index Pointer = 0 Contents = Hello World!
Index Pointer = 1 Contents = Demos loops, arrays, printing from arrays.
Index Pointer = 2 Contents = I hope this is worth it ...
Index Pointer = 3 Contents = Following is the output of printing the array.
1 2 3 5 8 13 21 -- stored internally
numbers printed out from array.
Index Pointer = 0 Contents = 1
Index Pointer = 1 Contents = 2
Index Pointer = 2 Contents = 3
Index Pointer = 3 Contents = 5
Index Pointer = 4 Contents = 8
Index Pointer = 5 Contents = 13
Index Pointer = 6 Contents = 21
Control goes here only when an error is 'caught'!
Index = 7 is Out of Range
Understand why 7 triggered an error, how it was caught.
------------------------------------------------------------------------
Your assignment: (descending order)
Print out the contents of the integer array in reverse order:
1 2 3 5 8 13 21 -- stored internally
numbers printed out from array.
Index Pointer = 6 Contents = 21
Index Pointer = 5 Contents = 13
Index Pointer = 4 Contents = 8
Index Pointer = 3 Contents = 5
Index Pointer = 2 Contents = 3
Index Pointer = 1 Contents = 2
Index Pointer = 0 Contents = 1
Very, very cool!
clues: watch pre- vs. post-increment. note base 0.
show me what you can do.
use an index out of range, say 9 to test the try..catch routine.
clip output along with source, present in HW folder.
5: sets up and initializes an array of integers.
6: sets up a string array with 4 rows.
7-10: stuffs data into the string array.
11-12: prints out contents of string array.
16-17: prints out 7 elements of integer array.
note post increment. note starts at 0.
in a try group, so if there is a crash,
the 'catch' group will handle it.
-----------------------------------------------------------------
*/
import java.io.*;
class parrexc
{ public static void main (String args[])
{ int i;
int arr[] = {1,2,3,5,8,13,21}; // 5:
String srr[] = new String[4]; // 6:
srr[0]="Hello World!"; // 7:
srr[1]="Demos loops, arrays, printing from arrays.";
srr[2]="I hope this is worth it ...";
srr[3]="Following is the output of printing the array.";
for (i = 0; i < 4; i++) // 11:
{ System.out.println("Index Pointer = " + i + " Contents = " + srr[i]); }
System.out.println("1 2 3 5 8 13 21 -- stored internally");
System.out.println("numbers printed out from array.");
try {
for (i = 0; i < 8; i++)
{ System.out.println("Index Pointer = " + i + " Contents = " + arr[i]); }
System.out.println("Very, very cool! "); } // 17:
catch (Exception e) {
System.out.println("Control goes here only when an error is 'caught'!");
System.out.println("Index = " + i + " is Out of Range");
}
}
}
/*
end of listing
*/