Lecture 5: Strings, Arrays and Math

What You Will Learn Today

  1. Use basic object data structures such as strings, arrays, ArrayLists and vectors.
  2. Use Math objects and random numbers.
  3. Identify the scope of a variable in a given program, and differentiate local and global scope.
  4. Convert a procedural program into an object-based program.
  5. Create a menu-driven interface.
  6. Redirect and pipe input and output, and use text files and shell scripts.
  7. Explain the structure and use of the Java SDK and API documentation.

Strings

Arrays

Copying Arrays

ArrayLists

Vectors

A vector is a list with limited capacity that can be increased (doubled by default). Methods include:

Math Class

The Math class contains no objects; all its methods are class methods.

Most of the following methods take a double parameter and return a double.

Random Numbers

Variable Scope

Converting Procedural Programs to Object-Based Programs

Menu-Driven Interface

Redirection and Pipes

Batch Files and Shell Scripts

To run your program on more than one data set, create a DOS batch file or UNIX shell script:

  1. Create text files each containing program input, e.g. input1.txt, input2.txt and input3.txt.
  2. Type lines like these into a text editor:
    java myprogram < input1.txt
    java myprogram < input2.txt
    java myprogram < input3.txt
  3. Save as a text file with a name like myprogram.bat
  4. Change the permissions so the file is executable.
    chmod +x myprogram.bat
  5. Run the program.
    ./myprogram.bat

SDK and API Documentation

To Do After Class