📄 driver.java
字号:
//Name : Tong Yuxin//Student No. a1103993/* ============================================================= Implementation of a simple driver for Reduction and Mapper. Consists of the bare minimum to make calls to Reduce and Map. It will ignore the return value for now and just print a message to say it has finished. =============================================================*/public class Driver{ public static Driver me = new Driver(); public static void main(String[] args){ int i; me.callReduceWithSum(); me.callReduceWithConcat(); me.callMapperWithPlus100(); me.callScanWithMulit();// call ScanWithMulit method. System.out.println("Finished testing"); } private void callReduceWithSum(){ Integer[] inputArray; // input array pointer Integer result; // place to store the result final int inLength = 6; // length is constant Reduction x; // reduction object holder PlusFun holder; // holder for Plus function int i; // scratch variable. inputArray = new Integer[inLength]; // Alloc array for (i=0; i< inLength; i++) // fill with new integer inputArray[i] = new Integer(i); // objects. x = new Reduction(); // Alloc reduction object holder = new PlusFun(); // Alloc new Plus Function result = (Integer) x.Reduce( (ApplyObj) holder, inputArray); // Reduce msg sent to xSystem.out.println( "******************************************"); System.out.println ("Result of reduction 1 is "+result.intValue()); // display resultSystem.out.println( "******************************************"); } private void callReduceWithConcat(){ String[] inputArray; // input array pointer String result; // place to store the result final int inLength = 6; // length is constant Reduction x; // reduction object holder ConcatFun holder; // holder for Concat fun int i; // scratch variable. inputArray = new String[inLength]; // Alloc array for (i=0; i< inLength; i++) // fill with new string inputArray[i] = new String("a"); // objects. x = new Reduction(); // Alloc reduction object holder = new ConcatFun(); // Alloc new Concat Function result = (String) x.Reduce(holder, inputArray); // Reduce msg sent to x System.out.println( "******************************************"); System.out.println ("Result of reduction 2 is "+result); // display result string. System.out.println( "******************************************"); } private void callMapperWithPlus100(){ Integer[] inputArray; // input array pointer Object[] result; // place to store the result final int inLength = 6; // length is constant Mapper x; // mapper object holder Plus100 holder; int i; // scratch variable. inputArray = new Integer[inLength]; // Alloc array for (i=0; i< inLength; i++) // fill with new integer inputArray[i] = new Integer(i); // objects. x = new Mapper(); // Alloc mapping object holder = new Plus100(); // Alloc new Plus100 Function result = x.Map( (ApplyObjUnary) holder, inputArray); // map msg sent to xSystem.out.println( "******************************************"); System.out.println("RESULT "); // display result for (i=0; i< inLength; i++) System.out.println( (Integer) result[i]); System.out.println( "******************************************"); } //////////////////////////////////////////////////////////////////////////// // Scan int *int/////////////////////////////////////////////////////////// private void callScanWithMulit() {Integer[] inputArray; // input array pointerObject[] result; // place to store the resultfinal int inLength = 6; // length is constantScanner x; // scanner object holderMultiply holder; // holder for Multiply functionint i; // scratch variable.inputArray = new Integer[inLength]; // Alloc arrayfor (i=0; i< inLength; i++) // fill with new integer inputArray[i] = new Integer(i+1); // objects.x = new Scanner(); // Alloc scanner objectholder = new Multiply(); // Alloc new Multiply Functionresult = x.Scan( (ApplyObj) holder, inputArray); // Scan msg sent to x//sample tracing code -- outputs "storage"System.out.println( "******************************************");System.out.println("the final result of multiply are following");for (i=0; i< inLength; i++){System.out.println( "^^ " + (Integer) result[i]);};System.out.println( "******************************************");System.out.println ("Result of Scan is "+(Integer) result[inLength-1]); // display result} }/* ================================================================== Class containing a method to add int components of two Integers. The method returns Integer ==================================================================*/class PlusFun implements ApplyObj{ public Object f (Object x, Object y){ // <insert code here> int a; a=((Integer)x).intValue()+((Integer)y).intValue();// add int components of two Integers//System.out.println("PlusFun"+a); return new Integer (a); // Delete this line when you've written your solution }}/* ================================================================== Class containing a function to concatenate two strings The method returns string. ==================================================================*/class ConcatFun implements ApplyObj{ public Object f (Object x, Object y){ // <insert code here> String str; str=((String)x).concat((String)y);//concatenate two strings into one string return new String(str);// return the new string//System.out.println("ConcatFun"+str); // Delete this line when you've written your solution }}/* ================================================================== Class containing a function to add 100 to its argument The method returns Integer. ==================================================================*/ // almost complete but needs a minor correctionclass Plus100 implements ApplyObjUnary{ public Object f (Object x){ int x1; x1 = ((Integer)x).intValue();// get the int value from object //System.out.println("intValue"+x1); return new Integer (x1 + 100);//add 100 to its value }}/* ================================================================== Class containing a method to multiplication int components of two Integers. The method returns Integer ==================================================================*/class Multiply implements ApplyObj{ public Object f (Object x,Object y){ int x1; x1 = ((Integer)x).intValue()*((Integer)y).intValue();// multiplication int components of two Integers //System.out.println("multiple"+x1); return new Integer (x1);// return new intrger }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -