1001ICT Introduction To Programming Lecture Notes
School of Information and Communication Technology Griffith University
Semester 2, 2015
3 A First MaSH Program
In this section we will describe a very simple Making Stuff Happen (MaSH) program and show how to compile and run it.
Recall this simple Java program.
public class Hello {
public static void main(String[] args) { System.out.println("Hello, World!");
} }
Only the blue bit actually makes stuff happen.
Here is the simplest MaSH program that does the same thing.
import console;
println("Hello, World!");
The blue bit stays exactly the same.
All of the stuff that is about encapsulation is gone.
The red bit has been added.
We will now show how to run it and explain every part of it.
3.1 How to run a console MaSH program
A console program is one that inputs and outputs only plain text and runs in a command line (or console) environment, such as: the Command Prompt on Windows; or the Terminal or xterm on Mac OS X, Unix or Linux.
Example console programs are the MaSH and Java compilers.
A compiler is a program that reads a high level programming language and translatesit into some other (usually lower level) programming language.
See appendix A of the lecture notes for an explanation of the way the following command examples are displayed.
3.1.1 Step 1: Create the program file
Use your favourite text editor to create a text containing the text of the program.
The file must have a name that ends with the filename extension .mash.
The file name must start with an upper case letter.
The rest of the file name must consist only of letters, digits and underscores (_).
Our example program might be saved in a file named Hello.mash.
Also good: Hello_2_You.mash.
Bad: Hello, World!.mash.
3.1.2 Step 2: Compile with mashc
Programming language compilers usually have boring names formed by adding “c” to the end of the name of the language; e.g. cc.
Compile a MaSH program to Java with a command like:
$ mashc Program.mash
For the program Hello.mash, the command will be:
$ mashc Hello.mash
If the program has no errors that the compiler (mashc) can find, it will create an output file: Hello.java.
3.1.3 Step 3: Compile with javac
Compile a Java program with a command like:
$ javac Program.java
For the program Hello.java, the command will be:
$ javac Hello.java
If the program has no errors that the compiler (javac) can find, it will create an output file: Hello.class.
javacis a translator from the Java language to Java Java bytecode.
Java bytecode is the machine code for the Java virtual machine (JVM).
3.1.4 Step 4: Run with java
Run a Java program with a command like:
$ java Program
For the program Hello, the command will be:
$ java Hello
javais the Java virtual machine for your computer.
It interprets the Java bytecode, which means it executes it.
Compilers translate. Interpreters execute.
3.1.5 All steps (Windows): mash
The file mash.bat is installed on the lab PCs.
It is a DOS batch file.
Batch files are scripts containing DOS commands to run in sequence.
To run all the steps (mashc, javac, java) in one command, type:
> mash Program
For the program Hello, the command will be:
> mash Hello
3.1.6 All steps (Mac and Linux): mc The file mc is installed on the lab Macs.
It is a bash shell script, the Unix equivalent to a Windows batch file.
To run all the steps (mashc, javac, java) in one command, type:
] mc Program
For the program Hello, the command will be:
] mc Hello
3.1.7 The MaSH judge
For console programs only, there is an online service that can test whether a program is the correct solution to one of a list of problems.
It can be used from any device with a web browser at:
http://www.ict.griffith.edu.au/arock/MaSH/mash.cgi?
problems
or from the command line with a mashj command like this:
$ mashj problem-id Program.mash
where problem-id is a string of characters that identifies the problem being solved.
For example, let’s submit Hello.mash for judging.
$ mashj 0001-hello Hello.mash Judging problem "0001-hello".
Success! Your program is correct.
Additional feedback:
Congratulations!
Runtime = 0.256942 seconds
$
In this case our submission is correct. Other outcomes could include, wrong answer, or format error.
If you get a format error, check the capitalisation, punctuation and spelling
3.1.8 MaSH Commands Summary
Here is a summary of the MaSH and java commands you will use most often for console programs.
Compiling and running programs
I want to... on Windows on Mac OS X / Linux
compile and run a pro- gram.
> mash Program ] mc Program
Sending programs to the judge
$ mashj problem-id Program.mash
There is a more complete version of this summary at the front of the A5 format lecture notes.
3.2 Understanding a console MaSH program
3.2.1 The import directive import console;
println("Hello, World!");
The red bit is called an import directive.
It makes stuff happen at compile time.
It tells mashc to look for a file called console.mashenv.
The file console.mashenv contains instructions to mashc as to how to translate a program intended to run in the console environment.
We will use a different environment nxt.mashenv for programming a Lego Mindstorms robot.
Java also has an import directive.
Most Java programs require several.
The Hello program we started with could have had this at the top:
import java.lang.System;
It is not required, however, as that part of the Java programming environ- ment is always imported by default.
MaSH is simpler.
Every MaSH program imports just one environment.
3.2.2 The statement import console;
println("Hello, World!");
The blue bit is a statement.
It makes stuff happen at run time. It is a direct command to do something.
Like most statements (and import directives) it ends with a semicolon (;).
There are many kinds of statements in Java.
This kind is a procedure call.
printlnis the name of a method.
A method is a chunk of program code given a name.
This one is defined somewhere else (in the console environment).
The println method carries out an action, so it is the kind of method called a procedure.
Always, after a method’s name come parentheses, ( and ).
Between the parentheses comes a value that is being passed to the method to operate on.
Values passed to methods are called parameters or arguments.
This value is a string of characters to print. The double quotes delimit the string.
Some questions that you should be asking:
• How do you learn all the rules about how to write statements?
• How do you find out what methods there are and what they do?
3.3 Section summary
This section covered:
• how to save a MaSH program in a text file with the right kind of name;
• how to use mashc to translate a MaSH program into a Java program;
• how to use javac to translate a Java program into a bytecode pro- gram;
• how to use java to interpret the bytecode program;
• the mash shortcut to do them all in sequence;
• the MaSH import directive;
• a MaSH statement consisting of a procedure call.
3.4 End of section feedback questions
Send us your answers to these questions any time you like by clicking on them.
• What was the most useful topic in this section?
• What was the least useful topic in this section?
• What was the least clear topic in this section?
• What topic in this section would you like to know more about?
• Did you find an error in this section?
3.5 Self-practice exercises
Do these in your own time.
1. Write any MaSH program that compiles, and submit it to the MaSH Online judge, as practice using the judge. (MaSH Online: 0000- compile. This is the problem-id for the online MaSH judge, and links to the problem on the web site.)
2. Write a help program that prints a summary of the batch file or script commands used to compile and run/upload MaSH programs.
3. Write a program that prints this example of ASCII art. (MaSH On- line: 0002-Mona.)
o8%8888, o88%8888888.
8’- -:8888b
8’ 8888
d8.-=. ,==-.:888b
>8 ‘˜‘ :‘˜’ d8888
88 ,88888
88b. ‘-˜ ’:88888 888b ˜==˜ .:88888 88888o--:’:::8888
‘88888| :::’ 8888b
8888ˆˆ’ 8888b
d888 ,%888b.
d88% %%%8--’-.
/88:.__ , _%-’ --- -
’’’::===..-’ = --.