Introduction to Java

Below we give simple translations of the True BASIC programs in chapter 2 of Introduction to Computer Simulation Methods, second edition, by Harvey Gould and Jan Tobochnik.

When lists of numbers are output they are sent to the Java Console Output window.

For simple input and output TextFields are used with ActionListeners to wait for user to enter values. You can enter new values whenever you like without rerunning program.

product product2 series
series_test tasks Local Variables
Program product

import java.awt.*;

public class product {
 
 public static void main(String args[])  {
      double m = 2;
      double a = 4;
      double f = m*a;
      System.out.println(String.valueOf(f));  //print to Java Console
  }
  
}    

Program product2

import java.awt.*;
import java.awt.event.*;

public class product2 extends Frame implements ActionListener {

TextField in1,in2,out1; 

 public static void main(String args[])  {
     product2 pr2 = new product2();   
  }   

  
  public product2() {    
     setSize(500,200);
     init();
     setVisible(true);
  }

  public void init() {
     setLayout(new FlowLayout());    // use a Layout manager 
     in1 = new TextField("Enter mass here");
     in2 = new TextField("Enter acceleration here");
     out1 = new TextField("Force given here");
     in1.addActionListener(this);   // call actionPerformed when
     in2.addActionListener(this);   // numbers entered in these TextFields
     add(in1);
     add(in2);
     add(out1);
  }
  
  public void actionPerformed(ActionEvent e) {
      Double m =  new Double(in1.getText().trim());  //use Double class to convert string to number
      Double a =  new Double(in2.getText().trim());
      double f = m.doubleValue()*a.doubleValue();
      out1.setText(String.valueOf(f));               // output to TextField
  }
  
}    
Program series
import java.awt.*;

public class series {
 
 public static void main(String args[])  {
   /* add the first 100 terms of a simple series  */
   double sum = 0;
      for(int n = 1; n <= 100; n++) {
         sum = sum + 1.0/(n*n);
         System.out.println(n + " " + sum);
      }
  }
  
}    
  

Program series_test

import java.awt.*;

public class series_test {
 
 public static void main(String args[])  {
   /* illustrate use of while loop */
   double sum = 0;               // initialize variables
   int n = 0;
   double relative_change = 1;  // choose large value
   while(relative_change > 0.0001) {
      n++;   // shorthand for n = n +1
      double newterm = 1.0/(n*n);
      sum += newterm;  // shorthand for sum = sum + newterm
      relative_change = newterm/sum;
      System.out.println(n + " " + relative_change + "  " + sum);
      }
   }
  
}
   

Program tasks

import java.awt.*;
import java.awt.event.*;


public class Tasks extends Frame implements ActionListener{
 
 TextField in1,in2,out1,out2;
 
 public static void main(String args[])  {
     Tasks t = new Tasks();  
  }   
  
 public Tasks() {     
     setSize(500,200);
     init();
     setVisible(true);
  }

  public void init() {
     setLayout(new FlowLayout());
     in1 = new TextField("        ");
     in2 = new TextField("        ");
     out1 = new TextField("    Sum Here    ");
     out2 = new TextField("    Product Here  ");
     in1.addActionListener(this);
     in2.addActionListener(this);
     add(in1);
     add(in2);
     add(out1);
     add(out2);


  }
  
  public void actionPerformed(ActionEvent e) {
      Double x =  new Double(in1.getText().trim());
      Double y =  new Double(in2.getText().trim());
      double sum = add(x.doubleValue(),y.doubleValue());
      double product = multiply(x.doubleValue(),y.doubleValue());
      out1.setText("Sum = " + String.valueOf(sum));
      out2.setText("Product = " + String.valueOf(product));
  }
 
  public double add(double x, double y) {
      return x+y;
  }
  public double multiply(double x, double y) {
      return x*y;
  }
}       

Passing values in and out of methods

This section discusses the issues on pages 17 and 18 about local variables. When a primitive data type such as a int, float, or double is used as an argument to a method, then the value of this argument in the calling program does not change. This is because a copy of the value of the argument is passed not its address or location in memory. This is different from True BASIC. Thus, the following program will output the value 10.

public class Scope {

	public static void main(String args[]) {
		Scope sc = new Scope();
		sc.testScope();
	}
	
	public void testScope() {
	   int x = 10;
	   addOne(x);
	   System.out.println(x);
	}
	
	private void addOne(int s) {
	   s = s + 1;   
	}
	   
}

One solution to obtaining the new value of x is to list x as a variable for the class as follows. This program outputs the value 11.

public class Scope {
    int x = 10;    // accessible to all methods in class

	public static void main(String args[]) {
		Scope sc = new Scope();
		sc.testScope();
	}
	
	public void testScope() {
	   addOne();
	   System.out.println(x);
	}
	
	private void addOne() {
	   x = x + 1;   
	}
	   
}

This solution however should only be used if x is a variable used by a number of methods. If only one value is needed from the method then the cleanest procedure is to return the value as shown below.

public class Scope {

	public static void main(String args[]) {
		Scope sc = new Scope();
		sc.testScope();
	}
	
	public void testScope() {
	   int x = 10;
	   x = addOne(x);
	   System.out.println(x);
	}
	
	private int addOne(int x) {
	   x = x + 1;
	   return x;   
	}
	   
}

Another trick way is to put x in an object. This way the address of the object is passed and any change in the values within the object remain after the method is returned. The simplest object to use is a one element array as shown below.

public class Scope {

	public static void main(String args[]) {
		Scope sc = new Scope();
		sc.testScope();
	}
	
	public void testScope() {
	   int x[] = {10};
	   addOne(x);
	   System.out.println(x[0]);
	}
	
	private void addOne(int s[] ) {
	   s[0] = s[0] + 1;   
	}
	   
}