Output Variables DataTypes Week20part1 Block A
Output Variables DataTypes Week20part1 Block A
Computer Programming
1
Objectives
Introduce the module and Scala platform
2
What is a computer program?
A list of instructions to tell the computer what to do.
4
Why Scala?
Programming languages fall into different paradigms –
Scala is described as a hybrid language as it is both
object-oriented and functional.
5
What we need to run Scala programs
Scala runs on the Java Virtual Machine (JVM), which
can run on Windows, Linux or Mac.
6
Developing our own Scala programs
You will need an editor, within which you can write your
programming code.
7
Notepad and the Command Prompt
On Windows the simplest way to build and run your
own programs is with Notepad and the Command
Prompt.
8
Compiling and Running a program
Change to directory where program exists:
Use command cd (change directory), e.g. cd C:\progs\
scalac MyProg.scala
MyProg.class
MyProg$.class
MyProg.tasty
scala MyProg
10
IntelliJ all-in-one IDE
It currently seems like a lot of effort to build, compile
and run a simple Scala program.
11
IntelliJ structure
We can choose a single project providing us with a
storage location for all our Scala programs.
12
IntelliJ key features
Project tool window – modules, programs and libraries.
14
First Scala Program
object HelloWorld {
def main(args: Array[String]): Unit = {
It reads an expression, evaluates it, prints it, and reads the next
expression. read-eval-print-loop (REPL)
You can run a REPL within IntelliJ from the Tools menu. You may
even run a selection of code from your program in the interpreter.
20
Variables
Almost every program needs to store data – we can
store data in our programs with variables
22
The Assignment Operator
The assignment operator uses the "equals sign" = to
assign values to a variable – this may be used for:
Initialisation of a variable
Overwriting the existing value stored in a variable
99 4 bytes
0x001000
myVar2
'e' 2 bytes
0x003050
Variable Naming syntax rules
The identifier you give your variables has to conform to
a set of syntax rules:
May begin with a letter, $ or _ but latter two are discouraged.
Subsequent characters may be letters, digits, $ or _
Names are case sensitive and white space is not allowed.
Cannot use any reserved keywords, e.g. "if", "val", "var", etc.
It is possible to use certain symbolic operators in names, but
this is only encouraged in very specific circumstances.
total5 //OK 5total //not OK total 5 //not OK
25
Cannot use as identifiers for variables:
26
Variable Naming conventions
The identifier you give your variables should conform to
a common conventions of good practice:
Start with lower case letters.
Use full words where possible or sensible abbreviations.
Equally, don't make names too long – you’ll have to repeat this!
If more than one word, capitalise first letter of each subsequent
word – i.e. "Camel case".
name //OK firstName //OK firstname //discouraged
27
Basic Data Types
Commonly used value types are:
Int – (4 bytes) integer type, whole numbers -2147483648 to
+2147483647 (i.e. -231 .. +231-1)
Double – (8 bytes) holds real numbers such as 3.142
Boolean – holds one of two values, true or false
Char – (2 bytes) holds letters, punctuation and control
characters, as Unicode, an extension of ASCII code
String – A sequence of characters
28
Example declarations
var name: String = "fred"
Textbooks:
Programming in Scala (3rd edition) by Martin Odersky
An Introduction to programming and problem-solving using
Scala (2nd edition) by Mark C. Lewis and Lisa L. Lacher
31