Download Scala - The Computer Science Department

Document related concepts

Computer science wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Manchester Mark 1 wikipedia , lookup

Emulator wikipedia , lookup

Transcript
1320
Principles Of Programming I
Dr. Thomas Hicks
Computer Science Department
Trinity University
1
1
2
Read Course
Outline!
http://carme.cs.trinity.edu/thicks
/2320/outline.pdf
3
http://Carme.cs.trinity.edu
4
Check Schedule Page Daily!
http://carme.cs.trinity.edu/thicks
/1320/schedule.html
We will often, but not always, discuss
upcoming assignments  check the page!
Hints-Reading Assignments-Written
Assignments/Labs!
5
6
Printing!
Use Computer Science Printers Only For
Your “Computer Science Homework!!!!”
Print Only What Is Necessary!
7
Computer
Science
&
Computer
Programming
8
Computer Science
 Computer Science is the study of the
Theoretical Foundations of
 of Information and Computation &
 of Practical Techniques
for their implementation and application in
computer systems.
Computer Programming
9
 Computer Programming is the process of
 Designing,
 Writing,
 Testing,
 Debugging / troubleshooting, &
 Maintaining
the source code of
computer programs.
.
10
Tell Me
About
The
Computer
Scientist?
11
Time Distribution [From S.E. Text]
What Do Computer Scientists Do With Their Time?
20%
Non-productive
activities
30%
Workingalone
50%
Interaction with
other people
Once A Computer Scientist
Learns How To Write Code,
The Computer Scientist Does
Much More Than Just Sit &
Work In Front Of His/Her
Computer(s)!
12
How Does The
TU Computer
Scientist
Learn To
Write Code?
13
TU  4 Courses To Prepare You To Program!
4 Serious Programming Courses In The Major
CSCI 1320 Principles Of Programming I
CSCI 1321 Principles Of Programming II
CSCI 2320 Principles Of Data Abstraction
CSCI 2322 Principles Of Functional Programming
CSCI 1320
Most Of You Should Plan On
Devoting An Average About
10 Hours Per Week Designing,
Writing, Testing, &
Debugging Programs!
14CHARGE  CSCI 1320 Principles Of Programming I
Required For Their Major
15
CSCI 1320 Principles Of Programming I
Understanding Natural Science and Technology or Support Major
16
Why Would
You Care
If You Like
Computer
Science?
17
www1.salary.com  Summer 2011
18
www1.salary.com  Summer 2011
19
Computer Science:
A Young
Science
20
Computer Science Is Often Considered
“A Young Science” – Founding Fathers
Babbage Difference
Engine
[1]
21
Alan Turing & Alonzo Church
22
“Giant Brain”
“US Army Balistic Research Center” in Maryland
[2]
23
Software
24
What is Software
25
BIOS Software
26
Operating System (OS) Software
Operating System – determines how
the user will interact with the
computer.
27
Software   Hardware
28
Hardware
Computer Scientists
Have To Know
More Than Just
How To Write Code
29
Mother Board
ASUS
Rampage II Extreme
For I7 Intel Processor
30
Install The I7 Processor
31
Processor With Cooling Fan
DX58SO Socket 1366
For I7 Intel Processor
32
Machine Language
Programming
33
Processor Cycles
 Each Cycle the computer Loads an Instruction
and Executes It.
 There was a time when Programming was done
by Writing the Binary Instructions that the
machine executes.
 We refer to the language of these binary
instructions as Machine Language.
34
The Stupid Computer Only Understands ‘0’ & ‘1’
 Every Processor, or processor family, has its
Own Machine Code Instruction Set.
 Instructions are Patterns of Bits that by
physical design correspond to different
commands to the machine.
 Bits are 0 & 1
The First Generation Of Programming Was Done
35
With Machine Language Programming
If eight–digit binary codes are used, there are 28
(256) numbered instructions from 00000000 to
11111111.
Machine language instructions for adding two
numbers could be something like the following:
01000011 00111010 00111101 01000001 00101011
Writing a Program That Says “Hello
World” Could Require Thousands Of
Lines Of Code.
36
Processor Instruction Sets
 The Processor Instruction Set is thus Specific
to a class of processors using (much) the same
architecture.
 Successor, or Derivative Processor Designs,
often include all the instructions of a predecessor
and may add additional instructions
37
Computer Voltage
Modern Computers work by Regulating the Flow
of Electricity through Wires.
In Today’s Electronics, the electric flow has been
reduced to tiny elements that have been etched
into silicon (tens of nanometers across)
Voltage, on the wires, is used to indicate the State
of a bit.
Voltage is either ON (1) or OFF (0)
38
Transistors
The wires connect up Transistors that are laid out
in a way that allow logical processing.
Modern Computer Processor  will include literally
hundreds of millions of transistors.
39
von Neumann Architecture
Most Modern Computers are built on a variation
of the von Neumann Architecture
Memory stores both Data & Programs
Bus is the interstate for instructions and data
40
Assemblers
&
Assembly Language
Programming
41 The Second Generation Of Programming Was
Done With Assembly Language Programming
Assembly Instructions are also Processor
Dependent
 LDA 15
LDB 20
ADD,B,A
• Individual Instructions that computers can do
are typically  very simple.
• Computers get their speed  instructions can
be done very quickly.
Assembly Language is more readable
than Machine Language
 An Assembler, is a program
which Translates the Assembly
Instructions into Binary Code
42
About Assembly Instructions
 When using the early Assembly Languages,
One Line of Assembly Code (1 Instruction)
was translated into One Line of Machine Code.
 Macros were added to
Assembly Language
 One Line Assembly code might generate
hundreds of lines of Machine Code.
Operating Systems, Micro Controllers,
Computer Games, Video Cards, etc.
43
Billions Of Instructions Per Second
Computers Instruction Cycle  a few gigahertz.
Computers Run  a few billion instructions every
second.
44
Compilers
&
High-Level
Languages
45
High Level Programming Languages
 Many High Level Programming Languages, such
as C, C++, Java, Scala, etc. have been created.
 A Compiler is a program which translates High
Level Programming Instructions into Machine Level
Instructions.
 println (“Hello World”)
46
About Compiler Instructions
 println (“Hello World”)
 The Scala instruction above is translated into
thousands of lines of Machine Code.
 The Scala instruction above is the same for all
processors & all operating systems  MUCH
FASTER PROGRAMMING!
47
The Scala Language
“Scala is a very High Level language that allows
you to communicate ideas to the computer in a
rather concise way and gives you access to a large
number of existing libraries to help you write
programs that are fun or interesting”.
48 Compile Once  Execute Binary Many Times
1 Byte = 8 Binary Bits
449,024 x 8 = 3,592,192 Bits To Say Hello World!
49
Interpreters
&
Programming
Languages
50
Scala Interpreters
 Scala can be run in an Interpretive Environment.
 An Interpreter takes one instruction line 
translates it into binary  executes that binary 
and continues to the next instruction in the program.
 Get next order  would be converted to
binary each time it is executed
(inefficient, but works)
 Interpreted code generally slower than
compiled code
 Interpreted Programs easier to debug
51
Scala Portability
 Scala can be compiled as well.
 Scala code is Very Portable Scala runs on Java
Virtual machine (JVM) this ensures the portability to
all the underlying platforms
52
Scala Is
Multi-Platform
53
Creator of Scala
Scala Created By Martin Odersky
54
Multi-Platform
 Scala Was Released in 2004
 Official Website is www.scala-lang.org
 Scala Is Multi-Platform
1. Windows
2. Linux/Unix
3. Mac
4. Java Virtual Machine
5. .NET initiative underway!
55
Scala Is
Open Source
56
Open Source  Free
 Scala Is Open Source
 Many Contributors
57
Commercial Backing
58 Install A Copy Of Scala On Your Computer
59
Full Blown
Programming
Languages
60
Scala Is A Powerful Language
 There are many full-blown, multi-purpose
Programming Languages  C, C++, & Java are
among the best known Powerful!
 Scala is a new full-blown programming language.
61
Programming
Paradigms
62
Imperative Programming  Scala
 There are many and a number of approaches
(paradigms) to programming.
Imperative Programming
1. the programmer specifically states how to do
things
2. the values stored by the
computer are readily altered
during the computation.
63
Functional Programming  Scala
Functional Programming was born out of the
mathematical notions of lambda calculus (Church)
Functional Programming
1. no mutable state  you have
functions that take values and
return values, but don't change
anything else along the way
64 Object Oriented Programming (OOP)  Scala
Object Oriented Programming is the new
paradigm
Object Oriented Programming
1. the Data and the Functions that operate on the
data should be bundled together into things
called Objects  called ENCAPSULATION
Scala is purely Object Oriented
65
Scala Is A New Hybrid Language
Scala can be programmed exclusively using the
Imperative Paradigm!
Scala can be programmed exclusively using the
Functional Paradigm!
Scala is purely Object Oriented & can make
heavy usage of the OOP Paradigm.
66
(If In Windows)
How To Start
The Command Line
67
Scala Interpreter Run In Windows Command Line
Hold Down The Windows
Start Button
Enter cmd
Hit The Enter/Return Key!
68
Start REPL Interpreter
Read-Evaluate-Print Loop
69
Scala
70
Scala Interpreter Run In Windows Command Line
71
Using
Scala As A
Calculator
Basic Integer Operations
72
 Addition (+), Subtraction(-), Multiplication(*),
Division(/)
 scala>
Integers = 0, +/- 1, +/- 2, …
73
Integer Division
 Integer Division(/) Discards The Remainder
 Comment Symbol (//)
74
Scala Has No Exponentiation Operator
 We can use the Power (pow) function from the
math library.
Real Number Division
75
 The Decimal Point makes a number a real
number.
 If any value is in an expression is real, the result
is real. Double  8 byte real number.
76




Order Of Operations  Correct!
Innermost Parentheses First
Exponentiation (Right To Left)
Multiplication & Division (Left To Right)
Addition & Subtraction (Left To Right)
77
MOD
 Modulus (%) returns the Remainder
78
REPL
Read-Evaluate-Print
Loop
“Under The Hood”
79
REPL





Scala supports Multiple Modes of Interaction.
Scala can be run in an Interpretive Environment.
REPL  Read-Evaluate-Print Loop
All Scala Data Members are Objects
Scala Statement  end with semicolan (;)
 semicolon inference  inserts (;) for you
REPL Expression Evaluation
 res0  short for result0
80
 5  Numeric Literal
 The results of the expression, entered into Scala,
are (1) it gave it a name (res0), evaluated it (2+3=5)
and gave it back(res0: Int = 5)
 Int is the type name that Scala uses for basic
numeric integers.
81
REPL Expression Evaluation
 Colons(:) are used to separate things from their
datatypes.
 Note that we can print the value stored in the
named container (res0)
82
Creating String
Variables & Constants
83
Naming String Objects  String Variables var
 String Variables are symbolic names for containers
that hold character data.
 Strings are delimited with double quotes.
 val first:String = “tom”  creates a string variable,
called first, and fills the container with tom
When You Use var, you
must initialize the variable
with a value!
You Can Force The Scala
Interpreter To Select A
DataType! (generally)
84
About String Variables var
 All Variables can be changed (String too!)
When You Use var, you
must initialize the variable
with a value!
You Can Force The Scala
Interpreter To Select A
DataType!
85
String Constants val
 val course:String = “CSCI-2320”  creates a string
constant, called course, and fills the container with
CSCI-2320
 Constants (Course) may not be changed
during program execution.
86
Naming
Variables
87
Naming Objects
 Select Meaningful Variable/Constant Names
 Object Names Are Case Sensitive
 Conventions:
 payRate (camel naming convention)
 pay_rate
 PayRate
88
String
Concatenation
89
Concatenation
 String Concatenate (+) is the process of
combining strings
90
println
91
Concatenation & println
 String Concatenate (+) can be used in functions
print & println
92
Creating
Integer
Variables & Constants
93Naming Integer Objects  Int Variables var
 Int Variables are symbolic names for containers that
hold Integer type data.
 -2,147,483,648 <= Int <= 2,147,483,647 (~+/- 2 billion)
 val age:Int = 21  creates an integer variable,
called age, and fills the container with 21
When You Use var, you
must initialize the variable
with a value!
You Can Force The Scala
Interpreter To Select A
DataType! (generally)
94
About Int Variables var
 All Variables can be changed (Int too!)
 age = age + 1  “Incrementing age”
-2,147,483,648 <= Int <= 2,147,483,647
Later We Shall Examine How Positive
& Negative Integers Are Stored
Later WeWhen
Shall You
Discuss
Use Why
var, you
noItems
must
Becomes
initialize
Negative
the variable
with a value!
You Can Force The Scala
Interpreter To Select A
DataType!
95
Int Constants val
 val N:Int = 5000  creates an Int constant, called N,
and fills the container with 5000
 Constants (N) may not be changed during
program execution.
96
Creating
Double
Variables & Constants
97
Naming Real Objects  Double Variables var
 Double Variables are symbolic names for containers
that hold real type data.
 val payRate:Double = 24.85  creates an double
variable, called payRate, and fills the container with
24.85
Computer Representation
Of Real Numbers (Doubles)
Is Not Exact!
Scientific Notation
98
About Int Variables var
 All Variables can be changed (Int too!)
 age = age + 1  “Incrementing age”
-2,147,483,648 <= Int <= 2,147,483,647
Later We Shall Examine How Positive
& Negative Integers Are Stored
Later WeWhen
Shall You
Discuss
Use Why
var, you
noItems
must
Becomes
initialize
Negative
the variable
with a value!
You Can Force The Scala
Interpreter To Select A
DataType!
99
Principles Of Programming I
CSCI 1320
Dr. Thomas E. Hicks
Computer Science Department
Trinity University
Textbook: An Introduction to Programming with Scala
By Dr. Mark Lewis
Special Thanks To Dr. Mark Lewis For Providing Some Of Text For
Use In This Presentation.