Week 3: OOP and Basic Data structures

3.1 Object Oriented World

 

3. 11 Objecct Oriented Programming

 

3.12 Speaking of OOP

 

3.13 Other Important Concept

 

3.2 Object declaration and construction

Examples:

// Declaration:
Vector v1;

// Construction:
v1 = new Vector ();

// Combined declaration and construction:
Vector v2 = new Vector ();

3.21 Other Simple Example

 

3.22 Using Java Build-in Classes

Vectors

 

More on Vectors

Examples:

Vector v = new Vector ();
v.addElement ("Apple");
v.addElement ("Banana");
v.insertElementAt ("Cherry", 1);  // Will throw an exception if invalid
// Each component in this vector with an index greater or equal 
// to the specified index is shifted upward to have an index one 
// greater than the value it had previously. 
// A,C,B

int s = v.size (); // Returns 3.

// Need to cast return value of elementAt().
// This will return "Banana".
String fruit = (String) v.elementAt (2); // Object is return 

v.removeElement ("Apple");
v.removeElementAt (1);

Hashtables

Examples:

// Create a hashtable with names as keys and
// birthdays as elements.
Hashtable birthdays = new Hashtable ();
birthdays.put ("Dylan", "June 25");
birthdays.put ("Liam", "February 28");
birthdays.put ("Angie", "February 10");

// Need to cast return value of get().
// This will return "February 28".
String bd1 = (String) birthdays.get ("Liam"); 

birthdays.remove ("Angie");

// This will return false:
boolean x = birthdays.contains ("Richard");

// This will return true:
boolean y = birthdays.contains ("Dylan");

Enumerations

Examples:

Enumeration e = v.elements ();
while (e.hasMoreElements ())
{
	String s = (String) e.nextElement ();
	System.out.println ("The next element is: " + s);
}

Date

Examples:

// Get current date and time before the operation.
Date before = new Date (); 

// ... Do something.

// Get current date and time after the operation.
Date after = new Date (); 
long difference = after.getTime () - before.getTime ();
System.out.println ("The operation took " + difference + " milliseconds.");

3.3 Packages

3.4 Lab

Create a benchmark application (called Week3) that compares the performance of arrays, Vectors, and Hashtables is 3 criteria: creation, iteration, searching.

Your application should:

  1. Create an array of 1000 Strings. Assign the values to be the String containing the digits of a number (e.g., "657"). You can create a String from a number as follows:
    String s = Integer.toString (657);
  2. Create a Vector with the same 1000 Strings.
  3. Create a Hashtable using the Strings as keys and an empty String ("") as elements.
  4. Iterate through the elements of the array using a for loop.
  5. Iterate through the elements of the Vector using size () and elementAt ().
  6. Iterate through the elements of the Vector using an Enumeration.
  7. Search for the element "888" in the array using a for loop.
  8. Search for the element "888" in the Vector using indexOf ().
  9. Search for the element "888" in the Hashtable using get ().

Your application should time each step using Dates and report the time each step takes to run.


Copyright 1996-2001 OpenLoop Computing. All rights reserved.