Purpose

In other notebooks, I explore the use of 2D arrays. It was discussed that a 2D array could sometimes be replaced with a 1D array of objects. This is a use case where a 2D array is necessary, since I use a 2D array where each entry is an object.

Description

The 2D array here represents the cartesian coordinate system. Going across a column is like the x axis and going down a row is like the y axis. At each point, we have an object which represents a vector. The name for a configuration where each point in the plane is assigned a vector is called a vector field.

import java.lang.Math;
// Defining the vector class. 
// Each entry of the 2D array is associated with an object of this class.
public class VC {
    // Constructor for the 3 components of the vector
    public VC (float x, float y, float z) {
        this.i = x;
        this.j = y;
        this.k = z;
    }
    
    // Constructor if only 2 components are provided
    public VC (float x, float y) {
        this.i = x;
        this.j = y;
        this.k = 0f;
    }
    // floats representing each component of the vector
    public float i;
    public float j;
    public float k;
    
    // method which returns the magnitude of the vector
    public double Magnitude() {
        return Math.sqrt(Math.pow(this.i, 2) + Math.pow(this.j, 2) + Math.pow(this.k, 2));
    }
    
    // method to print the components of the vector
    public void print() {
        System.out.println("Vector Components: ");
        System.out.print(this.i + "  ");
        System.out.print(this.j + "  ");
        System.out.print(this.k + "  ");
    }

}
public class VectorField {
    // defining a 2D array
    // the type of the array is the VC class defined above
    VC[][] Cartesian2D;
    
    public static void main(String[] args) {
        // Calling the print method on the field created by the CreateField method
        VectorField field1 = new VectorField();
        field1.print(field1.CreateField());
    }

    // Tester method to create a vector field
    public VC[][] CreateField() {
        // Outside dimension is columns, or x. 
        // Inside dimension is rows, or y
        return Cartesian2D = new VC[][] 
        {
            {new VC(0,0), new VC(0,1), new VC(0,2)}, 
            {new VC(1,0), new VC(1,1), new VC(1,2)}, 
            {new VC(2,0), new VC(2,1), new VC(2,2)}
        };
    }

    // method to print data in 2D array
    public void print(VC[][] field) {
        // Iterating through all the rows and columns
        for (int row = 0; row < field.length; row ++) {
            for (int col = 0; col < field[row].length; col++) {
                    // Displaying the location, magnitude, and components of each vector.
                    System.out.println("The Magnitude of the vector at "
                     + "(" + row + ", " + col + ") is: " + field[row][col].Magnitude());
                     field[row][col].print();
                     System.out.println();
                     System.out.println();
            }
        }
    }
    
}
VectorField.main(null);
The Magnitude of the vector at (0, 0) is: 0.0
Vector Components: 
0.0  0.0  0.0  

The Magnitude of the vector at (0, 1) is: 1.0
Vector Components: 
0.0  1.0  0.0  

The Magnitude of the vector at (0, 2) is: 2.0
Vector Components: 
0.0  2.0  0.0  

The Magnitude of the vector at (1, 0) is: 1.0
Vector Components: 
1.0  0.0  0.0  

The Magnitude of the vector at (1, 1) is: 1.4142135623730951
Vector Components: 
1.0  1.0  0.0  

The Magnitude of the vector at (1, 2) is: 2.23606797749979
Vector Components: 
1.0  2.0  0.0  

The Magnitude of the vector at (2, 0) is: 2.0
Vector Components: 
2.0  0.0  0.0  

The Magnitude of the vector at (2, 1) is: 2.23606797749979
Vector Components: 
2.0  1.0  0.0  

The Magnitude of the vector at (2, 2) is: 2.8284271247461903
Vector Components: 
2.0  2.0  0.0