NetRexx Programming for the JVM
vol 5 issue 5 p.89
 
Listing 1: NetRexx Employee class
 
package employee;
class Employee
Properties Private
firstName=String
lastName=String
id=int
dept=int
Properties indirect
manager=Employee
method Employee()
firstName = "John"
lastName = "Doe"
id = 1
manager=null
dept=1
method Employee(fname=String, lname=String, aId=int, aManager=Employee, aDept=int)
firstName = fname
lastName = lname
this.id = aId
this.manager = aManager
this.dept = aDept
method toString() returns String
buf=StringBuffer StringBuffer()
buf.append(lastName ',')
buf.append(firstName ',')
buf.append("" id)
return buf.toString()
method main(args=String[]) static
say Employee()
joe = Employee Employee("Joe", "Batista", 100, null, 1);
ron = Employee("Ron", "Furgeson", 101, joe, 1);
say ron
say ron.getManager()
 
Listing 2: Java Employee class
 
public class Employee{
private String firstName, lastName;
private int id, dept;
private Employee manager;
public Employee(){
firstName = "John";
lastName = "Doe";
id = 1;
manager=null;
dept=1;
}
public Employee(String fname, String lname, int id, Employee manager, int dept){
firstName = fname;
lastName = lname;
this.id = id;
this.manager = manager;
this.dept = dept;
}
public Employee getManager(){
return manager;
}
public void setManager(Employee manager){
this.manager=manager;
}
public String toString(){
StringBuffer buf = new StringBuffer();
buf.append(lastName+',');
buf.append(firstName+',');
buf.append(""+id);
return buf.toString();
}
...
...
}
 
Listing 3: NetRexx EmployeeForm
 
import javax.swing.
import java.awt.GridBagLayout
import java.awt.GridBagConstraints
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
import employee.Employee
class EmployeeForm extends JFrame implements ActionListener
Properties private
name=JTextField
id=JTextField
method EmployeeForm
super("Employee Form")
pane = JPanel()
getContentPane().add(pane)
pane.setLayout(GridBagLayout())
-- Create a name, and id text field.
name = JTextField(25)
id = JTextField(10)
-- Create and add a "Name" and "ID" label.
nameLabel = JLabel("Name")
nameLabel.setLabelFor(name)
nameLabel.setDisplayedMnemonic('N')
constraint = GridBagConstraints()
pane.add(nameLabel, constraint)
idLabel = JLabel("ID")
idLabel.setLabelFor(id)
idLabel.setDisplayedMnemonic('I')
constraint.gridy=1
pane.add(idLabel, constraint)
-- Add the name and ID text field to the form.
constraint.gridy=0; constraint.gridx=1
constraint.weightx=80.00
constraint.anchor=GridBagConstraints.WEST
pane.add(name, constraint)
constraint.gridy=1
pane.add(id, constraint)
-- Create an okay button, add it, and set up its event handler.
okay = JButton("Okay")
okay.setMnemonic('O')
constraint.gridx=1; constraint.gridy=2
constraint.anchor=GridBagConstraints.EAST
pane.add(okay, constraint)
okay.addActionListener(this)
this.setVisible(1)
this.pack()
method actionPerformed(event=ActionEvent)
handleOkay()
method handleOkay
fname = String
lname = String
_id = 0
_name = Rexx(this.name.getText())
fname = _name.word(1)
lname = _name.word(2)
_id = Integer.parseInt(this.id.getText());
theEmployee = Employee(fname, lname, _id, null, 100);
say theEmployee
say theEmployee.getClass().getName()
method main(args=String[]) static
EmployeeForm();
 
Listing 4: Java EmployeeForm
 
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import employee.Employee;
public class EmployeeForm extends JFrame{
private JTextField name;
private JTextField id;
public EmployeeForm(){
super("Employee Form");
JPanel pane = new JPanel();
getContentPane().add(pane);
pane.setLayout(new GridBagLayout());
// Create a name, and id text field.
name = new JTextField(25);
id = new JTextField(10);
// Create and add a "Name" and "ID" label.
JLabel nameLabel = new JLabel("Name");
nameLabel.setLabelFor(name);
nameLabel.setDisplayedMnemonic('N');
GridBagConstraints constraint = new GridBagConstraints();
pane.add(nameLabel, constraint);
JLabel idLabel = new JLabel("ID");
idLabel.setLabelFor(id);
idLabel.setDisplayedMnemonic('I');
constraint.gridy=1;
pane.add(idLabel, constraint);
// Add the name and ID text field to the form.
constraint.gridy=0; constraint.gridx=1;
constraint.weightx=80.00;
constraint.anchor=GridBagConstraints.WEST;
pane.add(name, constraint);
constraint.gridy=1;
pane.add(id, constraint);
// Create an okay button, add it,and set up its event handler.
JButton okay = new JButton("Okay");
okay.setMnemonic('O');
constraint.gridx=1; constraint.gridy=2;
constraint.anchor=GridBagConstraints.EAST;
pane.add(okay, constraint);
okay.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
handleOkay();
}
});
this.setVisible(true);
this.pack();
}
public void handleOkay(){
String name, fname, lname;
int index=0;
int id =0;
name = this.name.getText();
index = name.indexOf(" ");
fname = name.substring(0, index);
lname = name.substring(index+1,
name.length());
id = Integer.parseInt(this.id.get
Text());
Employee employee = new Employee(fname, lname, id, null, 100);
System.out.println(""+employee);
}
public static void main(String [] args){
new EmployeeForm();
}
}
 
Listing 5: getRange
 
method getRange(nums=ArrayList) public static returns ArrayList
-- Find the range. Returns a tuple with the minimum,
-- maximum, and range value
min=double
max=double
ranges=ArrayList
min = ( Double Collections.min(nums)).doubleValue()
max = ( Double Collections.max(nums)).doubleValue()
ranges = ArrayList()
ranges.add(Double(min))
ranges.add(Double(max))
ranges.add(Double(max-min))
return ranges
 
Listing 6: getMean
 
method getMean(nums=ArrayList, sample=boolean 1) public static returns double
-- Define mean that finds two types of mean, namely:
-- population mean and sample mean
value=Double
sum=0.0
average=0.0
iterator = nums.iterator()
loop while iterator.hasNext()
value = Double iterator.next()
sum = sum + value.doubleValue()
end
-- Check to see if this is a sample mean
if sample then
average = sum / nums.size()-1
else
average = sum / nums.size()
return average
 
Listing 7: getMode
 
method getMode(nums=ArrayList) public static returns double
-- Find the number that repeats the most.
-- make a duplicate copy of the nums argument
duplicate = ArrayList(nums)
Collections.sort(duplicate)
highest_count =double -100
mode =double -100
iterator = nums.iterator()
-- iterate through nums removing each item out of the duplicate
-- calculate the highest_count and the mode
loop while iterator.hasNext()
count =double 0
item = iterator.next()
-- Count the number of times the item occurs in the list
-- If Count is 0 go to the next iteration
count = countMode(item, duplicate)
if count == 0 then iterate
-- determine the highest count. The highest counted item
-- is the mode.
if count > highest_count then
do
highest_count = count
mode = (Double item).doubleValue()
end
end
method countMode(object=Object, list=ArrayList) private static returns int
index=int 0
count=int 0
loop until index > =0
index = Collections.binarySearch(list, object)
if index > =0 then list.remove(index)
count = count + 1
end
return count
 
Listing 8: getMedian
 
method getMedian(nums=ArrayList) public static returns double
-- Find the Median number
-- create a duplicate since we are going to modify the sequence
seq = ArrayList(nums)
-- sort the list of numbers
Collections.sort(seq)
median = 0.0 -- to hold the median value
length = seq.size() -- to hold the length of the sequence
index=0
-- Check to see if the length is an even number
if ( ( length % 2) == 0) then
do
-- since it is an even number
-- add the two middle number together
index = length / 2
m1 = (Double seq.get(index-1)).doubleValue()
m2 = (Double seq.get(index)).doubleValue()
median = (m1 + m2) /2.0
end
else
do
-- since it is an odd number
-- just grab the middle number
index = (length / 2)
median = (Double seq.get(index)).doubleValue()
end
return median