Lecture 9




Lecture Topic:
Strings
-- String is a character array terminated by a Null character.
String WOW = "Java is great";
String inst[ ] = {"you can learn", "about", "string"};
1. You may not change an array of character if it's used to make a string.
char C_array[ ] = {'W', 'O', 'W' };
String S = new String(C_array);
C_array[0] = 'b'; // illegal in Java
2. '+' = Concatenate two or more strings.
'+' = appends one string to another string.
String Prog = "the professor is";
String adj[ ] = {"nuts", "dumb", "boring"};
String sent = Professor + adj[0];
System.Out.println("String Output: " + Professor + adj[0]
+ adj[1] + adj[2]);
Vectors
-- vector can hold any combination of objects.
- Arrays are static, Vectors are
dynamic.
- Vector does'nt hold the primitive data
type such as int, char, float.
Syntax:
Vector V = new Vector();
Rectangle A = new Rectangle(10, 20);
V.addElement(A);
Example 1:
MenuBar menuBar= new MenuBar(); // Extended from Vector
SetmenuBar(menuBar);
Menu Grating_menu = new Menu("Objects");
menuBar.add(Grating_menu);
Grating_menu.add(new MenuItem("Grating"));
Example 2:
ObjectMoveChoice = new Choice(); // Extended from Vector
ObjectMoveChoice.addItem("Object Move");
ObjectMoveChoice.addItem("Camera");
add(ObjectMoveChoice); // building a frame class
Example 3:
public void create_geometry() {
drawnShapes = new Vector();
drawnShapes.addElement(grating.targetline[ ]);
.....
}
void printObject() {
Shape S;
int number_of_shapes = drawnShapes.size();
for (int i = 0; i <number_of_shapes; i++) {
S = (Shape)drawnShapes.elementAt(i);
S.print( );
S.move(loc); // move drawnShape object by Location
// store back to the same position in Vector
drawnShapes.setElementAt(S, i);
}
}
Exceptions
-- Exceptions handle special run-time error condition.
- "TRY"
- execute any program statements that might throw an exception
within a "TRY" block.
- "CATCH"
- execute a block of code if it catch the type of exception
specified in the "CATCH" statement.
- "THROW"
- Toss exceptions either from your own function code or library
code.
Syntax:
if () {
throw a exception() { }
}
try {
// do something that might throw an exception
}
catch (CException e) {
// handling function
//"e" contains info about the exception
}
finally {
// other handling functions
// always execute after catch, try to save the program from
unexpected errors
}
Example 1:
/*-*-*-* Input Validation
*-*-*-*/
Integer P1 =new Integer [2000];
Integer P2 =new Integer [3000];
Textfield P1_tf = new Textfield("Enter P1", 5);
Textfield P1_tf = new Textfield("Enter P2", 5);
Pitch_dialog.hide() {
// try to get string input
try { P1 =
Integer.valueOf(P1_tf);
P2 = Integer.valueOf(P2_tf);
}
// try to catch any input format
exception
catch (NumberFormatException) {
System.Out.println("****** Input Error ********");
}
}
Example 2:
if (out_of_bound) {
throw new Arithematic_Exception();
}
try {
//...What ever ...
}
catch (Arithematic_Exception e) {
System.Out.println("Out of Bound...");
}
finally {
System.Out.println("Finally I am out...");
}
Simple
Quiz
1. String object can not be modified after it's created.
a. True.
b. False.
2. Vector class defined to model two-dimensional geometic objects.
a. True.
b. False.
3. Exceptions are always thrown with the Java "throw" statement.
a. True.
b. False.




Last Update: 11/07/96
Copyright ©1996 - Douglas Lyon
Lyon@cse.bridgeport.edu