Isaac
Delicate bubble of expelled material around the cool red star U Antliae
J2F3: Java to Fortran 2003
A fundamental goal of objected-oriented design is to increase the clarity and utility of program code by focusing on what an object does and how it is used, rather than how an object is implemented.

Object-oriented design is a type of programming in which the author defines not only the type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.

One of the principal advantages of object-oriented programming techniques over procedural techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.

You can read more about object-oriented Fortran 2003 here.

Contents

  1. Complex Arithmetic
  2. Grade Point Average (GPA) from Comma-separated Values (CSV)
  3. Interface
  4. Abstract Class
  5. Garbage Collection
  6. Dependency Injection
  7. Removing Duplicate Values from an ArrayList
  8. Traversing a Tree Recursively

Some Exercises in Java and Object-oriented Fortran

Complex Arithmetic

Elijah

Java does not provide a complex number as a primitive data type, so here is a program that implements some common complex arithmetic operations.Downloads: ComplexArithmetic.java; Complex.java; ComplexArithmetic.f03; Complex.f03.

ComplexArithmetic.java

Elijah
final strictfp class ComplexArithmetic {
  private ComplexArithmetic() {
    /*prevent instantiation*/
  }
  public static void main(final String[] args) {
    final double THREE = 3.0, FOUR = 4.0;
    Complex z1 = new Complex(THREE, 2.0);
    Complex z2 = new Complex(1.0, -FOUR);
    System.out.println("z1 = " + z1);
    System.out.println("z2 = " + z2);
    z1.conjugate(); //object.method() acts like an operator
    z2.conjugate();
    System.out.println("z1* = " + z1);
    System.out.println("z2* = " + z2);
    Complex sum = z1.add(z2);
    System.out.println("z1* + z2* = " + sum);
    Complex diff = z1.subtract(z2);
    System.out.println("z1* - z2* = " + diff);
    Complex product = z1.multiply(z2);
    System.out.println("z1* * z2* = " + product);
    Complex div = z1.divide(z2);
    System.out.println("z1* / z2* = " + div);
    z1.sqrt();
    System.out.println("SQRT(z1*) = " + z1);
    z1.sin();
    System.out.println("SIN(SQRT(z1*)) = " + z1);
    z1.cos();
    System.out.println("COS(SIN(SQRT(z1*))) = " + z1);
    z1.abs();
    System.out.println("ABS(COS(SIN(SQRT(z1*)))) = " + z1);
    z1.exp();
    System.out.println("EXP(ABS(COS(SIN(SQRT(z1*))))) = " + z1);
    z1.log();
    System.out.println("LOG(EXP(ABS(COS(SIN(SQRT(z1*)))))) = " + z1);
  }
}
        

Complex.java (class)

Elijah
public class Complex {
  private double real = 0.0;
  private double imag = 0.0;
  public Complex() {
    this(0.0, 0.0); //invokes second constructor with 0 + i0
  }
  public Complex(final double real, final double imag) {
    this.real = real;
    this.imag = imag;
  }
  public void conjugate() {
    imag = -imag;
  }
  public Complex add(final Complex z) {
    Complex sum = new Complex();
    sum.real = real + z.real;
    sum.imag = imag + z.imag;
    return sum;
  }
  public Complex subtract(final Complex z) {
    Complex diff = new Complex();
    diff.real = real - z.real;
    diff.imag = imag - z.imag;
    return diff;
  }
  public Complex multiply(final Complex z) {
    Complex product = new Complex(); //FOIL
    product.real = (real * z.real) - (imag * z.imag); //First - Last
    product.imag = (imag * z.real) + (real * z.imag); //Inner + Outer
    return product;
  }
  public Complex divide(final Complex z) {
    Complex div = new Complex();
    div.real = ((real * z.real) + (imag * z.imag)) / (Math.pow(z.real, 2) + Math.pow(z.imag, 2));
    div.imag = ((imag * z.real) - (real * z.imag)) / (Math.pow(z.real, 2) + Math.pow(z.imag, 2));
    return div;
  }
  public void sqrt() {
    double a = real;
    double b = imag;
    real = Math.sqrt((a + Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))) / 2.0);
    imag = (b / Math.abs(b)) * Math.sqrt((-a + Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))) / 2.0);
  }
  public void sin() {
    double a = real;
    double b = imag;
    real = Math.sin(a) * Math.cosh(b);
    imag = Math.cos(a) * Math.sinh(b);
  }
  public void cos() {
    double a = real;
    double b = imag;
    real = Math.cos(a) * Math.cosh(b);
    imag = -Math.sin(a) * Math.sinh(b);
  }
  public void abs() {
    double a = real;
    double b = imag;
    real = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    imag = 0.0;
  }
  public void exp() {
    double a = real;
    double b = imag;
    real = Math.exp(a) * Math.cos(b);
    imag = Math.exp(a) * Math.sin(b);
  }
  public void log() {
    double a = real;
    double b = imag;
    real = Math.log(Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)));
    imag = Math.atan(b / a);
  }
  public String toString() {
    if (imag >= 0) {
      return real + " + " + Math.abs(imag) + "i";
    } else {
      return real + " - " + Math.abs(imag) + "i";
    }
  }
}
        

ComplexArithmetic.f03

Elijah
        

Complex.f03 (class)

Elijah
        

Grade Point Average (GPA) from Comma-separated Values (CSV)

Elijah

A simple program to calculate the grade point average from the input of comma-separated data.Downloads: GradePointAverage.java; GradePointAverage.f03.

GradePointAverage.java

Elijah
        

GradePointAverage.f03

Elijah
        

Interface

Elijah

Interfaces provide a way to achieve abstraction, and are used to define the contract for the subclasses to implement.Downloads: Interface.java; Interface.f03.

Interface.java

Elijah
        

Interface.f03

Elijah
        

Abstract Class

Elijah

Abstract classes are used to create a class with some default method implementation for subclasses.Downloads: AbstractClass.java; AbstractClass.f03.

AbstractClass.java

Elijah
        

AbstractClass.f03

Elijah
        

Garbage Collection

Elijah

The garbage collector is a program that runs on the Java Virtual Machine. It gets rid of objects which are not being used by a Java application anymore, and is therefore a form of automatic memory management.Downloads: GarbageCollection.java; GarbageCollection.f03.

GarbageCollection.java

Elijah
        

GarbageCollection.f03

Elijah
        

Dependency Injection

Elijah

A design pattern based on dependency injection allows the programmer to remove hard-coded dependencies, thereby making the application loosely-coupled, extendable, and maintainable.Downloads: DependencyInjection.java; DependencyInjection.f03.

DependencyInjection.java

Elijah
        

DependencyInjection.f03

Elijah
        

Removing Duplicate Values from an ArrayList

Elijah

The easiest way to remove duplicates is to pass the ArrayList to a LinkedHashSet. A LinkedHashSet doesn't support duplicates, so it will omit the duplicate values. From there you can pass the LinkedHashSet back to the ArrayList.Downloads: DuplicateArrayList.java; DuplicateArrayList.f03.

DuplicateArrayList.java

Elijah
        

DuplicateArrayList.f03

Elijah
        

Traversing a Tree Recursively

Elijah

Recursive is the best known and most frequently used method of tree traversal. The recursive algorithm uses a method-call stack in order to keep the state of the traversal for every level of the tree.Downloads: TreeRecursively.java; TreeRecursively.f03.

TreeRecursively.java

Elijah
        

TreeRecursively.f03

Elijah
        
back-to-top DANGER