package uk.ac.nesc.rph.myCalc2; /* *************************** PREAMBLE ********************* * A modified version of MyCalculator in which * the helper method getInput has become a class - SelectiveInput, * with an instance of that class being a selectiveInput Reader. * Here we are a bit more careful about appropriate visibility. * ********************** END ******************************* * ********************** PREAMLE ****************************************8 */ class SelectiveInput{ private char [][] syn; private char quit; SelectiveInput (char [][] syn, char quit) {this.syn=syn; this.quit=quit;} private boolean isNoticed (char c){ int i=0 ; for ( ; i=syn[i][0])&&(c<=syn[i][1])) return true;} return false;} char getInput() throws java.io.IOException, userQuit { char c; while (!isNoticed(c= (char) System.in.read())) if (c==quit) throw new userQuit(); return c; } } /** Provides Exception for IntegerVariable Interface*/ class uninitialized extends Exception {static final long serialVersionUID = 2; } /** This is for any class whose instances maintain an integer value * which can be manipulated through its set and get methods*/ interface IntegerVariable { int get()throws uninitialized; int set(int value);} /** Maintains a running integer value, initially 0, which can be incremented and reSet * and accessed using methods incr, reSet and get respectively * @author Richard Hopkins * */ class Accumulator { int total; static int uses=0; //to keep count of number of accumulator operations public Accumulator(){total=0;} //constructor public int incr(int i){++uses; total= total+i ; return total;}//increment public int reSet(int r){uses++; return total=r;} public int get(){return total;} } /** Maintains a running integer value which can be incremented, decremented, set * and accessed using methods incr, decr, set and get respectively. * @author Richard Hopkins * */ class AccMKII extends Accumulator implements IntegerVariable { public AccMKII(int initialTotal) //new constructor {total=initialTotal;} public AccMKII() //re-implemented constructor {this(0);} public int decr(int i) // new operation {uses++;total=total-i;return total;} public int incr(int i) //re-implemented operation {return this.decr(-i) ;} public int set(int r) //new operation {uses+=1;return total=r;} } class userQuit extends Exception //exception for MyCalculator {static final long serialVersionUID = 2; }; /** This application provides a number of accumulator objects (class AccMKII), * and implements an input language for operating on them. * This is a revised version with improved software structure, * but no functionality nor interface changes. * @author Richard Hopkins * */ public class MyCalculator2 { static final char QUIT='q'; // the character for quitting public static void main(String[] args) { // ****** Initialisation char [][] syn ={{'A','Z'},{'0','9'},{'+','+'},{'-','-'},{'=','='},{'?','?'}}; // syntax array for use in getInput(syn) - each pair of characters // is an inclusive range of recoginsed input characters SelectiveInput reader = new SelectiveInput(syn,QUIT); int size= Integer.parseInt(args[0]); //size of accumulator array if (size>26) size=26; Accumulator [] accs = new Accumulator [size]; for ( int i=0 ; i ["+c1+"]= "+v3); }while(true); } // ****** Exception Handling catch (userQuit e1){System.out.println("You have quit - Goodbye");} catch (java.io.IOException e1) {System.out.println("IOException");} } } // end of Class MyCalculator2