package uk.ac.nesc.rph.myCalc4; /* *************************** PREAMBLE ********************* * A modified version of MyCalculator3 in which * There is line-oriented input * For an accumulator comand the first three characters of the line are taken * as the command and the rest ignored. * For a "quit" the first character of the line is 'q' and the rest are ignored. * The changed parts of the program have comments //!!! * ********************** END ******************************* * ********************** PREAMBLE ****************************************8 */ import java.io.*; //!!! /** 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; }; class UIError extends Exception // exception for MyCalculator {static final long serialVersionUID = 2; String anErrorMess = "No Comment"; UIError(String s){anErrorMess=s;} public String getMessage() {return anErrorMess;} } /** This application provides a number of accumulator objects (class AccMKII), * and implements an input language for operating on them. * This is a revised verison having line-oriented input. * @author Richard Hopkins * */ public class MyCalculator4 { static final char QUIT='q'; // the character for quitting public static void main(String[] args) { // ****** Initialisation int size; // try{size= Integer.parseInt(args[0]);}//size of accumulator array catch (java.lang.NumberFormatException e1) {System.out.println("MyCalculator4: Argument error"); return;} if (size>26) size=26; else if (size < 1) // {System.out.println("MyCalculator4: Argument error"); return;} Accumulator [] accs = new Accumulator [size]; for ( int i=0 ; i0 && ca[0]==QUIT) throw new userQuit(); //!!! try { if (ca.length<3) throw new UIError("Input less than 3 characters");//!!! c1= ca[0]; c2=ca[1]; c3=ca[2];//!!! char[]ca3={c1,c2,c3}; ca=ca3;//!!! now only want first 3 characters p1= c1-'A'; //the index of the accumulator identifed by first param if (p1<0 || p1>=size)throw new UIError("Input 1 out of range"); // if (('A' <= c3) && (c3 <= 'Z')) {p2= c3-'A'; isLiteral=false; if (p2>=size)throw new UIError("Input 3 out of range"); // } else if (('0' <= c3) && (c3 <= '9')) {p2= c3-'0' ; isLiteral=true;} else throw new UIError("Input 3 out of range"); // if (!(c2=='+' || c2=='-' || c2== '?' || c2=='=')) // throw new UIError("Input 2 out of range"); // *** Doing the requested action int v2, v3=0; //values of second operand and result boolean show=false; // whether there is a result to show AccMKII target = (AccMKII) accs[p1]; v2=(isLiteral ? p2 : accs[p2].get()); switch (c2){ case '+' : v3=target.incr(v2); show=true; break; case '-' : v3=target.decr(v2); show=true; break; case '=' : v3=target.set(v2); show=true; break; case '?' : String s = ""; do { s=s+"["+c1+"]="+accs[p1].get()+" "; p1++; c1++; }while (p1 <= p2); System.out.println(s); show = false; break; } if (show) System.out.println(Accumulator.uses+")----------> ["+c1+"]= "+v3); }catch (UIError e1) //!! {System.out.println("Try Again - \"" +String.copyValueOf(ca)+ //!!! "\" input error: "+e1.getMessage());} }while(true); } // ****** Exception Handling catch (userQuit e1){System.out.println("You have quit - Goodbye");} catch (IOException e1) {System.out.println("IOException");} //!!! } } // end of Class MyCalculator4