MaSH Environment files

Contents

Purpose

This environment supports programming for a console program that reads from standard input and writes to standard output, as per environment console, but also adds the capability to read and write files on disk.

Rewrites

mandatory
void main()

Purpose: A program that is organised into methods must have a main method (a procedure with no arguments). This will be the first method to execute. mashc automatically rewrites this method to conform to standard Java.

Console input

Purpose

The following are methods for reading from standard input (which is usually the keyboard) and for checking to see if there is more input to read.

Methods

int readInt()

Purpose: Returns the next integer from standard input.

Precondition: Will cause a run time error unless there is an integer in standard input to read. Use isNextInt() to check first.

long readLong()

Purpose: Returns the next long from standard input.

Precondition: Will cause a run time error unless there is a long in standard input to read. Use isNextLong() to check first.

boolean readBoolean()

Purpose: Returns the next boolean from standard input.

Precondition: Will cause a run time error unless there is a boolean in standard input to read. Use isNextBoolean() to check first.

double readDouble()

Purpose: Returns the next double from standard input.

Precondition: Will cause a run time error unless there is a double in standard input to read. Use isNextDouble() to check first.

float readFloat()

Purpose: Returns the next float from standard input.

Precondition: Will cause a run time error unless there is a float in standard input to read. Use isNextFloat() to check first.

String readWord()

Purpose: Returns the next word as a String. A “word” is a sequence of one-or-more non-whitespace characters.

Precondition: Will cause a run time error unless there is a word in standard input to read. Use isNextWord() to check first.

String readLine()

Purpose: Returns the next line of text as a String. A line is a sequence of zero-or-more characters terminated by the end of line, which is not returned as part of the line.

Precondition: Will cause a run time error unless there is a line in standard input to read. Use isNextLine() to check first.

boolean isNextInt()

Purpose: Returns true if and only if there is an integer in standard input available to read, that is readInt() would succeed.

boolean isNextLong()

Purpose: Returns true if and only if there is a long in standard input available to read, that is readLong() would succeed.

boolean isNextBoolean()

Purpose: Returns true if and only if there is an boolean in standard input available to read, that is readBoolean() would succeed.

boolean isNextDouble()

Purpose: Returns true if and only if there is a double in standard input available to read, that is readDouble() would succeed.

boolean isNextFloat()

Purpose: Returns true if and only if there is a float in standard input available to read, that is readFloat() would succeed.

boolean isNextWord()

Purpose: Returns true if and only if there is a word in standard input available to read, that is readWord() would succeed.

boolean isNextLine()

Purpose: Returns true if and only if there is a line in standard input available to read, that is readLine() would succeed.

Console output

Purpose

The following are methods for printing to standard output (which is usually the terminal window).

Methods

void print(char c)

Purpose: Writes character c to standard output.

void print(String s)

Purpose: Writes string s to standard output.

void print(long i)

Purpose: Writes integral i to standard output.

void print(boolean p)

Purpose: Writes boolean p to standard output.

void print(double x)

Purpose: Writes floating point number x to standard output.

void println(char c)

Purpose: Writes character c and then a newline to standard output.

void println()

Purpose: Writes a newline to standard output.

void println(String s)

Purpose: Writes string s and then a newline to standard output.

void println(long i)

Purpose: Writes integral i and then a newline to standard output.

void println(boolean p)

Purpose: Writes boolean p and then a newline to standard output.

void println(double x)

Purpose: Writes floating point number x and then a newline to standard output.

Opening and closing files

Purpose

These are the methods by which we open and close files.

When a file is opened, the method that opens the file will return an int, a fileID, which identifes that file to the methods for reading or writing. If the returned int is negative, the file could not be opened for some reason.

Files opened for reading can not be written to. Files opened for writing can not be read from.

Files opened for reading or writing must be closed by calling the close method before your program ends.

Files are opened by specifying the path to it. Example paths: fred.txt; E:\holiday\itenerary.txt.

Methods

int openRead(String path)

Purpose: openRead(path) opens the file at path for reading and returns the unique ID by which this file may be accessed. IDs are always non-negative. If a negative number is returned, the file could not be opened.

Precondition: There must be a file at path to read from.

int openWrite(String path)

Purpose: openWrite(path) opens the file at path for writing and returns the unique ID by which this file may be accessed. IDs are always non-negative. If a negative number is returned, the file could not be opened. If there is an existing file at path, its contents are discarded.

void close(int fileID)

Purpose: close(fileID) closes the file selected by fileID.

Reading from files

Purpose

The following are methods for reading from files and for checking to see if there is more input to read from them.

Methods

int readInt(int fileID)

Purpose: Returns the next integer from the file selected by fileID.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is an integer in the file to read. Use isNextInt(fileID) to check first.

long readLong(int fileID)

Purpose: Returns the next long from the file selected by fileID.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a long in the file to read. Use isNextLong(fileID) to check first.

boolean readBoolean(int fileID)

Purpose: Returns the next boolean from the file selected by fileID.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a boolean in the file to read. Use isNextBoolean(fileID) to check first.

double readDouble(int fileID)

Purpose: Returns the next double from the file selected by fileID.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a double in the file to read. Use isNextDouble(fileID) to check first.

float readFloat(int fileID)

Purpose: Returns the next float from the file selected by fileID.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a float in the file to read. Use isNextFloat(fileID) to check first.

String readWord(int fileID)

Purpose: Returns the next word as a String from the file selected by fileID. A “word” is a sequence of one-or-more non-whitespace characters.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a word in the file to read. Use isNextWord(fileID) to check first.

String readLine(int fileID)

Purpose: Returns the next line of text as a String from the file selected by fileID. A line is a sequence of zero-or-more characters terminated by the end of line, which is not returned as part of the line.

Precondition: fileID selects a file that is open for reading.

Precondition: Will cause a run time error unless there is a line in the file to read. Use isNextLine(fileID) to check first.

boolean isNextInt(int fileID)

Purpose: Returns true if and only if there is an integer in the file selected by fileID available to read, that is readInt(fileID) would succeed.

Precondition: fileID selects a file that is open for reading.

boolean isNextLong(int fileID)

Purpose: Returns true if and only if there is a long in the file selected by fileID available to read, that is readLong(fileID) would succeed.

Precondition: fileID selects a file that is open for reading.

boolean isNextBoolean(int fileID)

Purpose: Returns true if and only if there is an boolean in the file available to read, that is readBoolean(fileID) would succeed.

boolean isNextDouble(int fileID)

Purpose: Returns true if and only if there is a double in the file selected by fileID available to read, that is readDouble(fileID) would succeed.

Precondition: fileID selects a file that is open for reading.

boolean isNextFloat(int fileID)

Purpose: Returns true if and only if there is a float in the file selected by fileID available to read, that is readFloat(fileID) would succeed.

Precondition: fileID selects a file that is open for reading.

boolean isNextWord(int fileID)

Purpose: Returns true if and only if there is a word in the file selected by fileID available to read, that is readWord(fileID) would succeed.

boolean isNextLine(int fileID)

Purpose: Returns true if and only if there is a line in the file selected by fileID available to read, that is readLine(fileID) would succeed.

Precondition: fileID selects a file that is open for reading.

Writing to files

Purpose

The following are methods for printing to files.

Methods

void print(int fileID, char c)

Purpose: Writes character c to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void print(int fileID, String s)

Purpose: Writes string s to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void print(int fileID, long i)

Purpose: Writes integral i to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void print(int fileID, boolean p)

Purpose: Writes boolean p to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void print(int fileID, double x)

Purpose: Writes floating point number x to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void println(int fileID, char c)

Purpose: Writes character c and then a newline to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void println(int fileID, String s)

Purpose: Writes string s and then a newline to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void println(int fileID, long i)

Purpose: Writes integral i and then a newline to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void println(int fileID, boolean p)

Purpose: Writes boolean p and then a newline to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

void println(int fileID, double x)

Purpose: Writes floating point number x and then a newline to the file selected by fileID.

Precondition: fileID selects a file that is open for writing.

Command line arguments

Purpose

These methods give access to the command line arguments.

Methods

String[] args()

Purpose: Returns the array of command line agruments.

int numArgs()

Purpose: Returns the number of command line arguments.

Math

Purpose

The following are some commonly used numeric constants and functions.

Constants

final int MAX_INT

Purpose: A constant holding the maximum value an int can have, 231 − 1.

final int MIN_INT

Purpose: A constant holding the minimum value an int can have, − 231.

final long MAX_LONG

Purpose: A constant holding the maximum value a long can have, 263 − 1.

final long MIN_LONG

Purpose: A constant holding the minimum value a long can have, − 263.

final double PI

Purpose: The closest double approximation to π.

final double E

Purpose: The closest double approximation to e.

Methods

double abs(double a)

Purpose: Returns the absolute value of a.

float abs(float a)

Purpose: Returns the absolute value of a.

long abs(long a)

Purpose: Returns the absolute value of a.

int abs(int a)

Purpose: Returns the absolute value of a.

double ceil(double a)

Purpose: Returns the least double value that is greater than or equal to a and equal to an integer.

double exp(double x)

Purpose: Returns ex, that is Euler's constant e raised to power x.

double floor(double a)

Purpose: Returns the greatest double value that is less than or equal to a and equal to an integer.

double log(double x)

Purpose: Returns the natural logarithm of x.

double rint(double x)

Purpose: Returns the closest mathematical integer to x.

long round(double a)

Purpose: Returns the closest long to a.

int round(float a)

Purpose: Returns the closest int to a.

double sqrt(double a)

Purpose: Returns the square root of a.

Precondition: a ≥ 0.0.

double pow(double a, double b)

Purpose: Returns a raised to the power b, ab.

double sin(double a)

Purpose: Returns the trigonometric sine of a radians.

double cos(double a)

Purpose: Returns the trigonometric cosine of a radians.

double tan(double a)

Purpose: Returns the trigonometric tangent of a radians.

double asin(double a)

Purpose: Returns the trigonometric arc sine of a in radians.

double acos(double a)

Purpose: Returns the trigonometric arc cosine of a in radians.

double atan(double a)

Purpose: Returns the trigonometric arc tangent of a in radians.

double atan2(double y, double x)

Purpose: Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).

double max(double a, double b)

Purpose: Returns the greater of a and b.

float max(float a, float b)

Purpose: Returns the greater of a and b.

int max(int a, int b)

Purpose: Returns the greater of a and b.

long max(long a, long b)

Purpose: Returns the greater of a and b.

double min(double a, double b)

Purpose: Returns the lesser of a and b.

float min(float a, float b)

Purpose: Returns the lesser of a and b.

int min(int a, int b)

Purpose: Returns the lesser of a and b.

long min(long a, long b)

Purpose: Returns the lesser of a and b.

double random()

Purpose: Returns a random value x such that 0.0 ≤ x < 1.0.

Strings

Purpose

The following are methods for working with Strings.

Methods

int length(String s)

Purpose: Returns the length of s.

char charAt(String s, int i)

Purpose: Returns the character at position i in s.

Precondition: 0 ≤ i < length(s).

boolean equals(String a, String b)

Purpose: Returns true if and only if a contains the same sequence of characters as in b.

String format(boolean p, int w)

Purpose: Returns p converted to a string, padded with spaces to a minimum width |w|. If w is negative, the result is left-justified, otherwise right-justified.

Examples:
format(true, 10) returns ”      true”; and
format(true, -10) returns ”true      ”.

String format(char c, int w)

Purpose: Returns c converted to a string, padded with spaces to a minimum width |w|. If w is negative, the result is left-justified, otherwise right-justified.

Examples:
format('a', 3) returns ”  a”; and
format('a', -3) returns ”a  ”.

String format(double d, int w, char f, int p)

Purpose: Returns d converted to a string, padded with spaces to a minimum width |w|. If w is negative, the result is left-justified, otherwise right-justified. f controls the format: 'e' selects scientific notation; 'f' selects fixed point; or 'g' selects the best format depending on the number and p. For 'e' and 'f', p is the number of decimal digits to display after the decimal point, but for 'g' it is the total number of digits.

Examples:
format(1234.56789, 12, 'e', 4) returns ”  1.2346e+03”;
format(1234.56789, 12, 'f', 4) returns ”   1234.5679”;
format(1234.56789, 12, 'g', 4) returns ”        1235”;
format(0.000001234567, 12, 'e', 4) returns ”  1.2346e-06”;
format(0.000001234567, 12, 'f', 4) returns ”      0.0000”; and
format(0.000001234567, 12, 'g', 4) returns ”   1.235e-06”.

String format(long l, int w)

Purpose: Returns l converted to a string, padded with spaces to a minimum width |w|. If w is negative, the result is left-justified, otherwise right-justified.

Examples:
format(42, 5) returns ”   42”; and
format(42, -5) returns ”42   ”.

String format(String s, int w)

Purpose: Returns s padded with spaces to a minimum width |w|. If w is negative, the result is left-justified, otherwise right-justified.

Examples:
format(”aaa”, 5) returns ”  aaa”; and
format(”aaa”, -5) returns ”aaa  ”.

boolean parseBoolean(String s)

Purpose: Returns s converted to a boolean.

int parseInt(String s)

Purpose: Returns s converted to an int.

long parseLong(String s)

Purpose: Returns s converted to a long.

float parseFloat(String s)

Purpose: Returns s converted to a float.

double parseDouble(String s)

Purpose: Returns s converted to a double.

Debugger

Purpose

This environment supports a debugger with a graphical user interface. It allows the user to slow down the execution of a program, so that the flow of control through the program may be observed and the values in variables monitored.

To activate the debugger, compile your program with the mashc +debug option.

While the program is running under the control of the debugger, the original MaSH program's source code is displayed on the left, with a pointer showing the statement that is about to execute. Sometimes it will point to a closing brace, indicating that the current method is about to be exited, or a loop guard is about to be tested again, depending on the context.

The contents of all the program's variables are displayed on the right in a table. The entries in the scope column, consist of a number and a word. The number indicates which method invocation the variable belongs to. For no-method MaSH programs the number is always 0. For method MaSH programs, 0 indicates a global variable, 1 indicates the main method, 2 indicates a method called from main, 3 indicates a method called from 2, etc. The word is blank for variables in a no-method program, unless the variable is declared in a block, which makes it local. In method programs, the word can be: global; local; or param. Variables are added to the table as they come into scope and removed again when their enclosing block exits. Anything changed on the last program step is displayed in green.

Limitations: The debugger can not display values of arrays with 3 or more dimensions. The debugger can not step through methods that run in the program's event dispatch thread (for example, the paintWindow method in the graphics environment). This GUI debugger can not work in environments without a big screen (for example, Lego Mindstorms).

Methods

void suspendDebugger()

Purpose: Suspend the debugger.

Calling this in your MaSH program disables the debugger from pausing your program or updating the debugger window.

If the debugger has not been incuded at compile time, this procedure does nothing at all.

void resumeDebugger()

Purpose: Resume the debugger.

Calling this in your MaSH program re-enables the debugger to pause your program and update the debugger window.

If the debugger has not been incuded at compile time, this procedure does nothing at all.

Do not call this in methods that run in the program's event dispatch thread (for example, the paintWindow method in the graphics environment).