Isaac Handcrafted.codes // 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

Complex Arithmetic

Java does not provide a complex number as a primitive data type, so here is a program that implements some common complex arithmetic operations.

ComplexArithmetic.javaDownload ComplexArithmetic.java

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);
  }
}
              

ComplexArithmetic.f03Download ComplexArithmetic.f03

              

Complex.java (class)Download Complex.java

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";
    }
  }
}
              

Complex.f03 (class)Download Complex.f03

              
 

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

A simple program to calculate the grade point average from the input of comma-separated data.

GradePointAverage.javaDownload GradePointAverage.java

              

GradePointAverage.f03Download GradePointAverage.f03

              
 

Interface

Interfaces provide a way to achieve abstraction, and are used to define the contract for the subclasses to implement.

Interface.javaDownload Interface.java

              

Interface.f03Download Interface.f03

              
 

Abstract Class

Abstract classes are used to create a class with some default method implementation for subclasses.

AbstractClass.javaDownload AbstractClass.java

              

AbstractClass.f03Download AbstractClass.f03

              
 

Garbage Collection

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.

GarbageCollection.javaDownload GarbageCollection.java

              

GarbageCollection.f03Download GarbageCollection.f03

              
 

Dependency Injection

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

DependencyInjection.javaDownload DependencyInjection.java

              

DependencyInjection.f03Download DependencyInjection.f03

              
 

Removing Duplicate Values from an ArrayList

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.

DuplicateArrayList.javaDownload DuplicateArrayList.java

              

DuplicateArrayList.f03Download DuplicateArrayList.f03

              
 

Traversing a Tree Recursively

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.

TreeRecursively.javaDownload TreeRecursively.java

              

TreeRecursively.f03Download TreeRecursively.f03

              
  back-to-top DANGER