Lecture 9

ContentsIndexPrevNext

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.
 
Syntax:
 
 
 
 
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:
 
 
 
 
Example 1:
 
 
 
 
 
 
Example 2:
 
 

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.
 
 
 


ContentsIndexPrevNext

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