/** * A4Q1: Purpose of this program is to execute 'milen' programs and store the variables in a hashtable * COMP 2140 SECTION L01 * INSTRUCTOR Helen Cameron * ASSIGNMENT Assignment #4, Question #1 * AUTHOR Jonathan Chartrand, 6818334 * VERSION November 15th, 2007 */ import java.io.*; public class A4Q1 { //********************************************************************** /** Node * PURPOSE: Node class which is used to store a variable's name and its corresponding value for a milen program. Nodes are used so the program can employ the separate chaining method. */ static class Node { int number = 0; String name = null; Node next = null; //******************** //Constructor //******************** public Node(String name, int number, Node next) { this.name = name; this.number = number; this.next = next; } //******************** //Accessors //******************** public String getName() { return name; } public int getNum() { return number; } public Node getNext() { return next; } //******************** //Mutators //******************** public void setName(String name) { this.name = name; } public void setNum(int number) { this.number = number; } public void setNext(Node next) { this.next = next; } } ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// //********************************************************************** /** Hashtable * PURPOSE: stores a variable with its corresponding value into a hashtable. position in which information is stored is based on the variable's name using Horner's method. */ static class Hashtable { int size = 0; Node[] table; int constant = 23; int elements = 0; //******************** //Constructor //******************** public Hashtable() { size = 79; table = new Node[size]; } //******************** //Methods //******************** /**printVariables * PURPOSE: prints all the variables and their corresponding value contained in the hashtable * PARAM: none * RETURN: void */ public void printVariables() { for (int i=0;i 0)//if a line isnt blank or just whitespace, the milen statement is processed { parseEq(line,mytable); } line = buf_reader.readLine(); // new line is read in from text file if (line.trim().equals("Q"))//if the new line is a quit line, loop is stopped { quit = true; count++; line = buf_reader.readLine();//reads in new line so current line still isnt quit statement } } System.out.println(); System.out.println("Final values of variables:"); mytable.printVariables(); mytable = new Hashtable(); System.out.println(); quit = false; } buf_reader.close (); } catch (IOException e) { System.out.println ("IO exception =" + e ); } return mytable; } /**main * PURPOSE: creates a hashtable and reads in milen statements from a given textfile * PARAM: None. * RETURN: None. */ public static void main(String[] args) { System.out.println("Comp 2140 Assignment 4 November 2007"); System.out.println ("Executing Milen Programs"); System.out.println("(Author Jonathan Chartrand)"); System.out.println(); System.out.println(); String File = "A4Data.txt"; Hashtable mytable = new Hashtable(); mytable = FileIn(File); System.out.println("Program completed normally."); } }