Slide 1 - Mathematics & Computing Science

advertisement
Lecture 1: Overview of Programming
Tami Meredith
Computers
 Hardware (Physical devices)
 CPU = Central Processing Unit
 Memory
 Input and Output (I/O) Devices
 Software (Programs)
 Operating System, e.g., Linux, OS-X, Windows
 Applications, e.g., MS Word, Photoshop, Web-Browsers
 Users
 IT Professionals, e.g., Administrators, Programmers
 Casual Users, e.g., everyone else
Digital Systems
 Everything is stored as numbers!
 Different encodings used
 ASCII or Unicode for characters,
 RGB or CMYK for colours
 Layers of encodings, e.g., Unicode in PDF
 Becomes very complicated
 e.g., jpg (pictures), mpg (movies), mp3 (music), pdf
(documents), exe (programs), …
Binary
 All numbers are stored in base 2 (Binary)
 2 digits: 0 and 1
 Easily manipulated with electronics
 0 = no power
 1 = power flowing or present
 Can be added, subtracted, manipulated the same as
decimal (base 10)
 Can represent negative numbers
 Can NOT represent all numbers (e.g., 1/3, PI)
Binary in Action
 Counting:
 Binary: 0, 1, 10, 11, 100, 101, 110, 111, 1000, …
 Decimal: 0, 1, 2, 3,
4,
5,
6,
7,
8, …
Base 10
Base 2
104
103
102
101
100
10000s
1000s
100s
10s
1s
24
23
22
21
20
16s
8s
4s
2s
1s
Memory
 Various kinds
 Volatile = loses contents at power off
 Non-Volatile = always there until “erased,” persistent
 RAM = Random Access Memory
 Volatile
 Main memory where executing programs are kept
The Memory Hierarchy
Registers (inside CPU)
Cache(s): L1, L2
Main Memory (RAM)
Solid State “Disks”
Hard (Magnetic) Disks
CD/DVD (Optical Disks)
Tapes
Memory Terms
 Bit = smallest unit of memory, hold a value of 0 or 1
 Byte = 8 bits of memory (holds values 0 to 255)
 Word = 64 bits = 8 bytes



Typical “number” in a computer
Number of bits a register can hold
Size of the “bus” – number of bits the CPU works with
 KiloByte = 210 Bytes =
1024 Bytes
 MegaByte = 220 Bytes = 1 048 576 Bytes
 GigaByte = 230, TeraByte = 240, PetaByte = 250, ...
Programming
 A “program” is a set of instructions on how to do




something.
Programs are written in a programming language such
as Java, C, Fortran, Visual Basic, Lisp, Pascal, Cobol ...
Computers CAN NOT do anything with Java code
Thus, there is a sequence of events we must follow
before a program can be “executed” or “run"
Must translate the Java code into something the
computer can use
Translation
 Compiling: Done by a compiler (a special program)
 Turns a program into something a computer can execute
 Does the whole program at once (more or less)
 Executes quickly after being compiled
 Creates an executable for a specific platform
 Interpretation: Done by an interpreter
 Takes one line at a time, and executes it
 Slower, but more portable
 Program can be run anywhere the interpreter runs
 Move the interpreter and you move everything it executes
Java
 Java is complicated, uses both a compiler and an
interpreter
1.
2.
Compile to “ByteCode” using javac
Interpret ByteCode using the jvm
 Supposed to get some of the advantages of both but in
reality is inefficient and kind of clunky
 “Write once, run everywhere” is actually, “Write once,
debug everywhere”
 But, its what we are stuck with ...
Translation and Execution
javac
Compiler
JVM Bytecode
Interpreter
USER
• A program written in Java
• myprogram.java
• A compiled program now in ByteCode
• myprogram.class
• An executing program
• User entering data and seeing results
Programming Process (Technical)
JDK
Libraries
User
Text
Editor
Compiler
(javac)
Input
Interpreter
(JVM: java)
Operating
System
(Windows)
Output
What is a Program?
 Programming is just like cooking or assembling a lawn
mower
 Need a procedure and materials
Recipe + Ingredients = NUM NUMS! (usually)
Instructions + Parts = Working Mower
Algorithm + Data = Program
Algorithm






Instructions
The VERBS or actions/commands in a description
Set of steps to accomplish a goal
Always supposed to finish
Will find the solution if one exists
Tells you what to do with the parts/ingredients/data
1. POUR half a cap of shampoo into hand
2. LATHER shampoo into wet hair
3. RINSE with water
Data
 What we manipulate
 The nouns in a description
 In a computer it is all encoded into numbers (digital)
 E.g., ASCII: A=65, B=66, C=67, ...
 Can encode: Letters, numbers, colours, pictures,
music, documents, ... *ANYTHING*
 Encodings can be combined
 We are not going to worry to much about them for
now, except for ... (next slide)
Some Common Data Types
 Strings: Sequences of characters
“hi there”, “Hello, Bob!”, “x”, “RESULT”
 Integers: Whole numbers
... -3, -2, -1, 0, 1, 2, 3, ...
 Floating Point Numbers: Things with a decimal
1.0, 3.14, .333, 100000.0000001
 Booleans: Truth values for tests
True, False (that’s it, just two Boolean values)
 Arrays: Ordered “lists” of things
Will get to these later in the course ...
Syntax and more
 Java is a language with its own lexicon (dictionary of
allowable words) and syntax (grammar)
 Learning Java is NOT learning programming any more
than learning English makes one a writer!
 Algorithms and Data are the Plots and Characters of a
good story writer
 You are now writers! Programming is a creative
endeavour that is more than simply typing something
into a computer
Writing a Story
 Grammar is not everything
 Good grammar does not make a great writer, but it is a
necessary component
 Being good with Java does not make a great programmer,
but it is a necessary component
 Knowing what to write matters
 Good writers create great plots and characters
 Good programmers create great algorithms and data
structures
Programming and writing are similarly creative!
PHP, VisualBasic, C, Java, ...
 Languages come into existence and eventually become
obsolete
 Languages change constantly, stuff is added and removed
or improved
 While its good to know a language well (since you can use
it better) I’m more interested in teaching you to PROGRAM
in Java, than in teaching you to use Java for programming
 You can bring “Cheat Sheets” to the exams!
 Learn to use Java not waste time memorising the details of
Java
 Trying to develop skills that are independent of Java
 Allow you to use other languages in the workplace
Programming
 Translating the solution to some problem into a format




that a computer can use.
Any trained monkey can program! – well, almost ...
Java is just a tool (Java is our Grammar)
Also need to learn plots (algorithms), characters & places
(data), events
We are not going to focus on Java, we are going to focus on
solving problems, designing solutions, implementing the
solution (programming), and testing the solution.
“First to the keyboard = Last to finish!”
Thinking versus Typing
 Good writers have :
 Outlines, plot summaries, character descriptions
 Good programmers have:
 Program architectures, plans, notes, outlines
 Just because there is a computer in front of you, there
is no need to instantly start banging away (often
without direction) on the keyboard
 The computer doesn’t get lonely if you don’t use it right
away, it won’t rust, it won’t cry or be sad ... I promise!
Think first, Type later!
Typing is NOT Programming
Do not go to the lab and just start typing,
This is the surest way to a C, D, or F 
4.
READ the task description
UNDERSTAND what you are supposed to do
THINK and figure out how to do it
DESIGN a solution that should work
5.
Type it in
6.
TEST and EVALUATE, IMPROVE your program
1.
2.
3.
Programming Process (Practical)
1. Identify the problem – Figure out what we need to do
2. Determine the data we have and need
3. Select/design algorithms that manipulate the data in
a manner that we can use
(sometimes we swap 2 and 3 – experience tells us when)
4. Translate the algorithms and data into Java (or any
programming language)
5. Execute the program and evaluate the results
6. If they are not correct, figure out why, fix, and retest
(repeat as often as necessary)
Concepts – We’ll come back to these a lot
 Control Flow
The sequence/order that things happen in
We use this to build algorithms
 Data Management
Knowing where in memory the data we need is stored
 Language Libraries
The set of Routines/Procedures/Functions/Methods provided
by Java for manipulating data
E.g., Find a specific word in a sentence, Square Root, Write
some text to the screen
Building blocks to save us work!
How To Learn Programming
Read a book (bad) ...
2. Take a course (good) ...
3. Find examples of programs (e.g., on the web) and
start to try and figure out what they do (BEST)!
1.
All the great programmers I’ve met learned to program
using method 3.
Its what we’re going to do a lot of ...
It works and its easy!
My First Program: hello.java
// My First Program
// By Tami Meredith
public class hello
{
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
Formatting, Lesson 1
// My First Program
// By Tami Meredith
Everything after // is a comment to the reader
(shown in red) and is ignored by the computer
public class hello
{
public static void main (String[] args)
{
System.out.println("Hello World!");
}
} Curly braces { and } are matched in pairs with
each { being lined up with its matching }
The First Rules
 CASE MATTERS
System.out.println is not system.out.println
 Every character is important
System.out.println("Hello World");
Note: " and not “ or ”
 Every “delimiter” must have a matching delimiter
{ and }, “ and “, ( and ), [ and ], etc.
Successful programming requires extreme
ATTENTION TO DETAIL!
Good Practice
 Comments are good! Use plenty! Computers ignore
them and so they have no impact ever on performance
 Blank lines, indents, are FREE! You don’t get a discount
or prize for having the messy and hard to read code
 CLARITY before efficiency. Make it easy to understand
and maintain. Simple is usually better ... (to a point)
The Value of Style
// My First Program
// By Tami Meredith
public class hello
{
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
--------------------- Versus ----------------------public class h{public static void
main(String[]a){System.out.println("Hello World!");}}
Tradeoffs
 All of computing requires the management of
tradeoffs:
 Complexity vs. Clarity, Simplicity
 Performance vs. Size and Memory Use
 Ease of use vs. Feature Richness
There is no correct balance or solution! Finding the right
mix of elements is an art form
Programmers are like musicians, we can all play an
instrument, but not all of us will be good at it ...
Not EVERYONE is a
Great Programmer




Athletes: NBA, NHL, NFL players, Huskies Football, ...
Academics: Mathematicians, Physicists, Rocket Scientists
Artists: Successful Authors, Painters, Popular Musicians/Singers
Lingerie/Swimsuit Models, Cosmeticians, Ballerinas & Dancers
All have something in common ...
 They require innate skills or qualities that can’t be taught, that
we somehow acquire or are born with -- we can't be all these
things!
 Programming is something that all of you can do, but is not
going to be easy for some of you
 Some of you are not going to be that good at it – THAT’S OK!
This Course
1.
2.
3.
4.
5.
Helps you learn if you are gifted as a programmer
Teaches you important analytical and thinking skills
(which is why it is a substitute for calculus)
Helps you to understand how computers work and
what they can and can’t do
Gives you some insight into what programmers and
technical staff (e.g., system administrators) can do
for you
Teaches you to focus on details and manage multiple
levels of complexity
Exercises
 Programming is learned by repetition and practice
 There will be lots of in-class exercises
 Bring PAPER and PENCIL to class
 Bring your textbook for reference
 Please don't use a laptop for the exercises
 You learn more if you do them on paper
 You get better practice for exams (where you can't use a
laptop)
To Do
 Get the textbook
 Read Chapter 1 (Material from this class), make notes,
write down your questions and bring them to the next
class
 Ignore Object-Oriented Programming (in 1.3)
 Read Chapter 2 to prepare for the next class
 Ignore the Graphics Supplement for all chapters, please
Download