Download pdf

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Library (computing) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Go (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Program optimization wikipedia , lookup

Scala (programming language) wikipedia , lookup

Compiler wikipedia , lookup

Name mangling wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Agenda
• Course introduction
• Programming basics
• Java programs
CS 99: Fundamentals of
Programming
Summer 2000
Michael Clarkson
Lecture 1: Programming Basics
6/26/00
Course Introduction
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
2
Programming Basics
• Syllabus
• Questionnaire
• To do by tomorrow’s lab:
• Writing programs
• Running programs
– Pick up NetIDs (or by Thursday at the latest!)
– Acquire at least 4 floppy disks, or one ZIP-100
disk
– Read the assigned sections
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
3
6/26/00
Writing Programs
•
•
•
•
•
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
4
Programs
• Code executed by a computer
• Code: instructions in a programming
language (e.g., Java)
• Examples: Microsoft Word, Eudora
• Writing programs is problem solving
What are programs?
Algorithms
Pseudocode
Functions
Style
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
– Primary task: break into simpler problems
5
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
6
1
Example Java Program
Algorithms
• Algorithm: “A finite set of rules that gives a
sequence of operations for solving a specific
type of problem.” – Donald Knuth
• Standard simile: like a recipe
class Example {
public static void main(String[] args) {
System.out.println(“This is a simple example.”);
}
}
– Has inputs (ingredients), outputs (prepared food)
– Tells you what to do, what order to do it in
• Programs are composed of algorithms
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
7
6/26/00
Pseudocode
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
8
Functions
• Language-independent method of expressing
algorithms
• Great for talking about how to do something
without getting caught up in the details of how to
program it
• Example:
SortCards
• In almost any language, programs are divided into functions
• Function: code written to perform one (small) well-defined
task
• Building blocks of programs
• Libraries of functions exist so that programmers don’t have
to keep “reinventing the wheel”
• Synonyms: procedure, method
• Java programs are divided first into classes, then into methods
Pick lowest card
Put in hand
Repeat
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
9
6/26/00
Style
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
10
Running Programs
• Writing in a programming language requires
good style
• Just as writing in a natural language (e.g.,
English) requires good style
• Handout
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
• Computer architecture (what)
• Compiling/interpreting (how)
11
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
12
2
Computer Architecture
Compiling
Computer
Source
code
Compiler
Executable
program
Program
in memory
Loader
Input
Processor
Memory
Processor
runs program
Output
Diagram from Computer Organization and Design by Patterson & Hennessy
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
13
6/26/00
Compiling
Interpreting
Source
code
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
14
Java Programs
Compiler
Executable
program
Program
in memory
Loader
• Anatomy
• Compiling and executing
Interpreter
Processor
runs program
(slower)
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
15
6/26/00
Anatomy of a Java
Program
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
16
Java Punctuation
In a file called Example.java:
class Example {
public static void main(String[] args) {
System.out.println(“This is a simple example.”);
}
}
• Every class and method has a body that is
enclosed in braces: { … }
• Every method is a series of statements, each
of which is terminated by a semicolon: …;
• Methods always involve parentheses: (…)
Things to notice: a class called Example, a method called main, the
name of the file
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
17
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
18
3
Compiling Java
Executing Java
• A Java source code file always ends with the
extension .java (e.g., Example.java)
• The Java compiler translates Java source code
into Java bytecode
• Bytecode files always have an extension of
.class (e.g., Example.class)
• Java bytecode can be executed in two ways:
6/26/00
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
19
– Interpreted by a Java Virtual Machine (JVM)
– Compiled by a Just-In Time compiler (JIT)
• Why be so complicated?
– Bytecode (.class files) is independent of real
machines
– Portability: code can be written on one platform
(e.g., Windows) and run on another (e.g., Mac)
without any changes
Executing Java [2]
Source
code
(.java)
public static void main(String[] args) {
…
}
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
20
Executing Java [3]
• Execution always starts with the method
called main
• The main method must look exactly like:
6/26/00
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
21
6/26/00
Java
compiler
Bytecode
(.class)
JVM interprets
bytecode
Processor runs JVM
Processor runs
compiled code
JIT compiles
bytecode
CS 99 ▪ Summer 2000 ▪ Michael Clarkson ▪ Lecture 1
Java loader
Program
in memory
22
4