package uk.ac.nesc.rph.myCalc1; /* *************************** PREAMBLE ********************* * The purpose of this example is try to illustrate all the constructs that you have been * told about in the lecture, and for this reason some things may be done in a somewhat * unnatural way - do not take this as always "best practice". * ********************** END ******************************* * ********************** PREABMLE ****************************************8 */ /* The following IntegerVariable is an interface used by one version of accumulator. * Would normally be in a separate file and package. */ /** 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);} /* The following is a 2-level class heirarchy of accumulator classes. * A basic one Accumulator and an extended one AccMKII. * Would normally be Public in separate files and package(s) */ /** 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;} } /* The AccMKII extension - * - provides an additional constructor with an initial value, * and re-implemets the old one * - supports the "standard" IntegerVariable Interface * - inherits the reSet and get methods * - includes an additional decrement method (decr) */ /** 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. * @see uk.ac.nesc.rph.myCalc1.AccMKII, * @author Richard Hopkins * */ public class MyCalculator1 { 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 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");} } /* Below are helper methods for reading user input. * Both take a parameter syn. This is an array of pairs of characters. * Each pair defines an exclusive range of characters. * The getInput method reads the input unitl finding a character which is within one * of those ranges and returns that. Except that it iterprets a "q" input as * quitting the program. * The isNoticed is method tests a character to determine whether it * is within one of the ranges */ static boolean isNoticed (char c, char [][] syn){ int i=0 ; for ( ; i=syn[i][0])&&(c<=syn[i][1])) return true;} return false;} static char getInput(char [][] syn)throws java.io.IOException, userQuit{ char c; while (!isNoticed(c= (char) System.in.read(),syn)) if (c==MyCalculator1.QUIT) throw new userQuit(); return c; } }