CS 1073 Introductory Programming
for Scientific Applications
Non-static Java


Alternative use of non-static methods: The programs below illustrate the way to write Java programs where only the main function is static (as it always must be). The differences between the two versions are highlighted in red.

Static VersionNon-static Version

public class Arrays {

   public static void printArray(int[] x) {
      for (int i = 0; i < x.length; i++)
         System.out.print(x[i] + " ");
      System.out.println();
   }

   public static int addElements(int[] x) {
      int sum = 0;
      for (int i = 0; i < x.length; i++)
         sum = sum + x[i];
      return sum;
   }

   public static int innerProduct(int[] x, int[] y) {
      int innProd = 0;
       for (int i = 0; i < x.length; i++)
         innProd = innProd + x[i]*y[i];
      return innProd;
   }

   public static int[] addArrays(int[] x, int[] y) {
      int[] z = new int[x.length];
       for (int i = 0; i < x.length; i++)
         z[i] = x[i] + y[i];
      return z;
   }

   public static void main(String[] args) {
      int[] a = {12, 23, 34, 45, 56};
      int[] b = {4, 1, 2, 5, 3};


      
      printArray(a);

      System.out.println("Sum of entries: " +
         addElements(a));

      int[] c = addArrays(a, b);
      printArray(c);

      System.out.println("Inner Product: " +
         innerProduct(a, b));

   }
}

public class Arrays {

   public void printArray(int[] x) {
      for (int i = 0; i < x.length; i++)
         System.out.print(x[i] + " ");
      System.out.println();
   }

   public int addElements(int[] x) {
      int sum = 0;
      for (int i = 0; i < x.length; i++)
         sum = sum + x[i];
      return sum;
   }

   public int innerProduct(int[] x, int[] y) {
      int innProd = 0;
       for (int i = 0; i < x.length; i++)
         innProd = innProd + x[i]*y[i];
      return innProd;
   }

   public int[] addArrays(int[] x, int[] y) {
      int[] z = new int[x.length];
       for (int i = 0; i < x.length; i++)
         z[i] = x[i] + y[i];
      return z;
   }

   public static void main(String[] args) {
      int[] a = {12, 23, 34, 45, 56};
      int[] b = {4, 1, 2, 5, 3};

      Arrays arrays = new Arrays();

      arrays.printArray(a);

      System.out.println("Sum of entries: " +
         arrays.addElements(a));

      int[] c = arr.addArrays(a, b);
      arrays.printArray(c);

      System.out.println("Inner Product : " +
         arrays.innerProduct(a, b));

   }
}

Here are results of a run. Both versions produce the same output.


A second example: dice: An old dice example done both ways.

Static VersionNon-static Version

public class Dice {

   public static final int N = 10000; // total rolls
   public static int s[] = new int[13]; // counters for rolls
   public static double t[] = {0., 0.,  // expected fraction
      1./36., 2./36., 3./36., 4./36., 5./36., 6./36.,
      5./36., 4./36., 3./36., 2./36., 1./36};

   public static int roll() {
      return (int)(6.0*Math.random() + 1.0);
   }

   public static void rollDice() {
      for (int i = 0; i < N; i++)
         s[roll() + roll()]++;
   }

   public static void printResults() {
      System.out.println("Total rolls: " + N + "\n");
      System.out.println("Spots\tRolls\tExpected\n");
      for (int i = 2; i < 13; i++) {
         System.out.println(i + "\t" + s[i] +
                                "\t" + t[i]*N);
      }
   }

   public static void main (String[] args) {

   

      rollDice();

      printResults();
   }
}

public class Dice {

   public final int N = 10000; // total rolls
   public int s[] = new int[13]; // counters for rolls
   public double t[] = {0., 0.,  // expected fraction
      1./36., 2./36., 3./36., 4./36., 5./36., 6./36.,
      5./36., 4./36., 3./36., 2./36., 1./36};

   public int roll() {
      return (int)(6.0*Math.random() + 1.0);
   }

   public void rollDice() {
      for (int i = 0; i < N; i++)
         s[roll() + roll()]++;
   }

   public void printResults() {
      System.out.println("Total rolls: " + N + "\n");
      System.out.println("Spots\tRolls\tExpected\n");
      for (int i = 2; i < 13; i++) {
         System.out.println(i + "\t" + s[i] +
                                "\t" + t[i]*N);
      }
   }

   public static void main (String[] args) {

      Dice dice = new Dice();

      dice.rollDice();

      dice.printResults();
   }
}

Here are typical results of one run of either program. Both versions produce the same output, except for the variation due to the random numbers used.


A second example: dice: An old dice example done both ways.

Static VersionNon-static Version

// SumOfSquares: sum of squares of integers
public class SumOfSquares {

   public static final int N = 10;










   public static int square(int x) {
      return x*x;
   }

   public static int cube(int x) {
      return x*square(x);
   }

   public static int sumOfSquares(int n) {
       int sum = 0;
       for (int i = 1; i <= n; i++) {
          sum = sum + square(i);
       }
       return sum;
   }

   public static double sumOfSquaresFormula(int n) {
      return cube(n)/3.0 + square(n)/2.0 + n/6.0;
   }

   public static void main(String[] args) {
   
       System.out.println("Sum of first " + N +
          " integers squared = " + sumOfSquares(N));
       System.out.println("Sum of first " + N +
          " integers squared (using formula) = " +
          sumOfSquaresFormula(N));
   }
}

// SumOfSquares: sum of squares of integers
public class SumOfSquares {
   
   public  int n; // add squares from 1 to n
   
   // constructor: feed in value for n
   public SumOfSquares(int nParam) {
      n = nParam;
   }
    // accessor: get out the value of n
   public int getN() {
      return n;
   }

   public int square(int x) {
      return x*x;
   }

   public int cube(int x) {
      return x*square(x);
   }

   public int sumOfSquares() {
       int sum = 0;
       for (int i = 1; i <= n; i++) {
          sum = sum + square(i);
       }
       return sum;
   }

   public double sumOfSquaresFormula() {
      return cube(n)/3.0 + square(n)/2.0 + n/6.0;
   }

   public static void main(String[] args) {
      SumOfSquares sumSquares = new SumOfSquares(10);
      System.out.println("Sum of first " + sumSquares.getN() +
         " integers squared = " + sumSquares.sumOfSquares());
      System.out.println("Sum of first " + sumSquares.getN() +
         " integers squared (using formula) = " +
         sumSquares.sumOfSquaresFormula());
   }
}

Common output of either program.