Class cw3e

java.lang.Object
  extended bycw3e

public class cw3e
extends java.lang.Object

Author: Joanne Louise Carter
Date: 06/01/05
Module: G6DICP Introduction to Computer Programming
Title: ICP Coursework 3
SCSiT Username: jxc04m

This program implements the entire TPL specification and extends it - the extended functions are documented in the readme.txt and include: BOOLEAN (TRUE/FALSE), CALCULATE on STRING, IF and END IF, and INPUT If an error is found then an appropriate error message is printed and the program ends - you must correct the error(s) to have the program run to completion.


Constructor Summary
cw3e()
           
 
Method Summary
static void calculate(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method implements the CALCULATE function.
static void createBoolean(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method creates a new TPLboolean variable and instantiates it.
static void createInteger(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method creates a new TPLinteger variable and instantiates it.
static void createString(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method creates a new TPLstring variable and instantiates it.
static int ifStatement(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          An IF statement.
static void let(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method deals with assigning values to variables.
static void main(java.lang.String[] args)
          The main method.
static void print(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method takes the current line of code, processes it and then prints a result (a line of text, variable data etc) according to whether it is PRINT or PRINTLN.
static void userInput(java.lang.String currentLine, int lineCounter, java.util.Vector tplData)
          This method asks the user for input for a variable value, then assigns the variable that value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

cw3e

public cw3e()
Method Detail

print

public static void print(java.lang.String currentLine,
                         int lineCounter,
                         java.util.Vector tplData)
This method takes the current line of code, processes it and then prints a result (a line of text, variable data etc) according to whether it is PRINT or PRINTLN. Firstly, it asks whether the command is PRINT or PRINTLN? Then it substrings what is to be printed, and sets newLine to true if needed. If the String left is contained in quotes then it trims off the quotes, if it is a variable then it finds the variable, or prints an error and ends the program. Then it adds a new line if needed and prints the result.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

createString

public static void createString(java.lang.String currentLine,
                                int lineCounter,
                                java.util.Vector tplData)
This method creates a new TPLstring variable and instantiates it. It first assigns its name as lineComponents[1], first checking whether just one variable is being declared and whether the variable name is allowed, and erroring if there are more than one or the name is not allowed. Then it adds the data to the tplData vector.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

createInteger

public static void createInteger(java.lang.String currentLine,
                                 int lineCounter,
                                 java.util.Vector tplData)
This method creates a new TPLinteger variable and instantiates it. It first assigns its name as lineComponents[1],first checking whether just one variable is being declared and whether the variable name is allowed, and erroring if there are more than one or the name is not allowed. Then it adds the data to the tplData vector.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

createBoolean

public static void createBoolean(java.lang.String currentLine,
                                 int lineCounter,
                                 java.util.Vector tplData)
This method creates a new TPLboolean variable and instantiates it. It first assigns its name as lineComponents[1],first checking whether just one variable is being declared and whether the variable name is allowed, and erroring if there are more than one or the name is not allowed. Then it adds the data to the tplData vector.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

let

public static void let(java.lang.String currentLine,
                       int lineCounter,
                       java.util.Vector tplData)
This method deals with assigning values to variables. Firstly, it removes LET, splits the line at the = to get the variable name and data. Then it searches the Vector for the variable name, if it exists then it will remove the previous variable entry and re-add the variable to the Vector (first finding out whether it is a variable, String or an integer and adding is as the appropriate TPL variable). If the LET command has no =, or is otherwise formatted incorrectly, it will throw an error.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

calculate

public static void calculate(java.lang.String currentLine,
                             int lineCounter,
                             java.util.Vector tplData)
This method implements the CALCULATE function. First it removes CALCULATE and splits the string on the = to get the variable name that you should assign the calculation to, and the sum. Then, provided there are no errors, it will find that variable and make sure it is an integer. Then we must find out what kind of calculation it is, the remaining string is split on whichever operator has been found, the values to be operated on are fetched and the final result is put back into the Vector. (I don't think this can be simplified any more).

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

ifStatement

public static int ifStatement(java.lang.String currentLine,
                              int lineCounter,
                              java.util.Vector tplData)
An IF statement. It checks whether the condition in the IF statement is true and returns the appropriate value. BOOLEAN and STRING can only be used with EQUALS or NOT, INTEGER can be used with those plus LESS and GREATER.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program
Returns:
in main () skipIf is intially set to -1, if 0 is returned then we know there is an IF statement being executed, so startIf is set to true. If 1 is returned we know there is an IF statement, but the condition not satisfied. Therefore, startIf and skipIf are set to true, and the program waits for an END IF to continue executing statements.

userInput

public static void userInput(java.lang.String currentLine,
                             int lineCounter,
                             java.util.Vector tplData)
This method asks the user for input for a variable value, then assigns the variable that value.

Parameters:
currentLine - The current line of code to be processed
lineCounter - Keeps track of how many lines of code have been processed
tplData - The Vector containing all the variables for the program

main

public static void main(java.lang.String[] args)
The main method. Pulls all the methods together and outputs the interpreted file.