CS201j: Engineering Software
University of Virginia
Computer Science
David Evans http://www.cs.virginia.edu/evans
26 November 2002 CS 201J Fall 2002 2
“abstract”
“design”
“impossible”, “challenge”, “a lot of work”
“thought provoking”, “spine-tingling”
“changing the world”
“smart safe secure coding class”
“thorough program analysis”
“efficient software development”
26 November 2002 CS 201J Fall 2002 3
“how to program if you aren’t a lazy bastard”
“programming with the knowledge that errors will happen and using that knowledge to survive such errors”
“learning to write code that other people can actually use”
“a safe playground for imagination”
“not your everyday computer science course”
“an exercise in humility”, “frustration by specification”
“Small Hall”, “language tricks overview”
“explores techniques and tools for constructing robust, complex and secure software”
“adaptable tools for software programming and design”
“secure programming”
26 November 2002 CS 201J Fall 2002 4
26 November 2002 CS 201J Fall 2002 5
26 November 2002
From Gerard Holzmann’s FSE Slides
CS 201J Fall 2002 6
• Abstraction by Specification
– Abstract away how details by saying what a procedure does
• Data Abstraction
– Abstract away representation details by specifying what you can do with something
• Subtyping
– Abstract away actual type details by allowing many types to be used in the same way (?)
• Concurrency
– Abstract away when details (?)
26 November 2002 CS 201J Fall 2002 7
26 November 2002 CS 201J Fall 2002 8
26 November 2002 CS 201J Fall 2002 9
Let AB and CD be the two given numbers not relatively prime. It is required to find the greatest common measure of AB and CD .
If now CD measures AB , since it also measures itself, then CD is a common measure of CD and
AB . And it is manifest that it is also the greatest, for no greater number than CD measures CD . But, if
CD does not measure AB , then, when the less of the numbers AB and CD being continually subtracted from the greater, some number is left which measures the one before it.
Euclid’s Elements, Book VII, Proposition 2 (300BC)
26 November 2002 CS 201J Fall 2002 10
By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe.
Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.
26 November 2002
Ada, Countess of Lovelace, around 1830
CS 201J Fall 2002 11
26 November 2002
Bill Gates (at Microsoft’s antitrust trial)
CS 201J Fall 2002 12
• Geometry (mathematics) is about declarative knowledge: “what is”
If now CD measures AB , since it also measures itself, then CD is a common measure of CD and AB
• Computer Science is about imperative knowledge: “how to”
Computer Science has nothing to do with beige (or translucent blue) boxes called “computers” and is not a science.
26 November 2002 CS 201J Fall 2002 13
“How to” knowledge:
• Ways of describing and carrying out imperative processes (computations)
• Ways of reasoning about (predicting) what imperative processes will do
26 November 2002
About 50% of CS201J is about these things… but nearly all of CS200 is!
CS 201J Fall 2002 14
Spring 2003
From Ada and
Euclid to Quantum
Computers and the
World Wide Web www.cs.virginia.edu/cs200
Meetings: Mondays, Wednesdays and Fridays at 2:00-2:50 pm
CS201J graduates can take (SEAS tech elective credit), but no prior background is expected.
26 November 2002 CS 201J Fall 2002 15
26 November 2002 CS 201J Fall 2002 16
X O
X X O
O X
26 November 2002 CS 201J Fall 2002 17
1
X O
2
4
X
7
O
5
8
3
X O
6
9
X
X
1
X
2
3
X
…
X
O
2
3
26 November 2002 CS 201J Fall 2002 18
X
• Goal: get three of your marks in a line before your opponent does
• Sub-goal: get to a position where it is your move, and you have a line with 2 of your marks and one empty square
• Sub 2 goal: get to a position where it is your opponent’s move, and no matter what she does, after her move sub 1 goal is satisfied
• Sub 3 -goal: get to a position where it is your move, and you can make a move that satisfies sub 2 -goal is satisfied
• Sub 4 goal: get to a position where it is your opponent’s move, and no matter what she does, after her move sub 3 goal is satisfied
• …
• Sub n -goal: from the start, get to a position where no matter how your opponent moves, you can select moves that get to the Goal
26 November 2002 CS 201J Fall 2002 19
• Evaluate a game situation based on how good it is for you
Score (tic-tac-toe board) =
-1 if opponent has 3 in line
0 if neither has 3 in line
1 if I have 3 in line
• Try to maximize your score, assuming opponent will try to minimize your score
(that is, maximize her score)
26 November 2002 CS 201J Fall 2002 20
Score (tic-tac board) =
-1 if opponent has 2 in vertical line
0 if neither has 2 in vertical line
1 if I have 2 in vertical line
1
Max: 0
X
2
X
3
X
Score (b) = 0 0 0
Min: 0
Score (b) = 1
Max: 1
X
X
3
O
Score (b) = 1
26 November 2002
4
X
0
X 2
O
3
X
4
X
X
O
0
X
2
0
O
O
X
1
Max: 0
O
X
4
O
0
X
CS 201J Fall 2002
4
0
X
21
• Total tic-tac-toe games to try =
9 * 8 * 7 * 6 * … * 1 = 9! = 362 880
• Minimax tells you that if both players pick the best move, it is always a draw
• What about chess?
– ~10 1050 possible chess games
– After 4 moves, there are (at least) 809 896 possible positions!
26 November 2002 CS 201J Fall 2002 22
• Evaluates 400m moves/second
• Need complex scoring functions for positions
– Special purpose hardware for scoring chess positions quickly
• Database of openings, special situations, etc.
26 November 2002 CS 201J Fall 2002 23
26 November 2002 CS 201J Fall 2002 24
Snakes Server
int port = …;
Server Socket ss = new ServerSocket (port);
ServerSocket listens to network port for incoming connections…
Socket client = ss.accept ();
A Socket gives you an input and output stream abstraction for dealing with network communication.
26 November 2002 CS 201J Fall 2002 25
import java.io.*; import java.net.*;
public class MySnake extends SnakeInterface { private ClientDisplay display; private final int FRAME_WIDTH = 400; private final int FRAME_HEIGHT = 400; public MySnake (Socket s) throws IOException { super (s);
… // set up the display
}
// The SnakeInterface run method will call this whenever the state of
// the world has changed.
public void updateState () { if (display != null) { display.updateGrid ();
}
}
26 November 2002 CS 201J Fall 2002 26
static public void main (String args[]) { if (args.length != 2) {
System.err.println ("Usage: java MySnake <server> <port>");
System.exit (1);
} else {
String server = args [0]; int port = Integer.valueOf (args[1]).intValue (); try {
Socket s = new Socket (server, port);
MySnake ms = new MySnake (s);
} catch … { // Need to catch exceptions if server is not reachable
}
}
}
Hint: You will want to do something similar in your PS7 code!
26 November 2002 CS 201J Fall 2002 27
26 November 2002 CS 201J Fall 2002 28
26 November 2002 CS 201J Fall 2002 29
A group of three or more connected communicating entities
26 November 2002 CS 201J Fall 2002 30
Thus, from some far-away beleaguered island, where all day long the men have fought a desperate battle from their city walls, the smoke goes up to heaven; but no sooner has the sun gone down than the light from the line of beacons blazes up and shoots into the sky to warn the neighbouring islanders and bring them to the rescue in their ships.
Iliad , Homer, 700 BC
Chain of beacon’s signaled Agammemnon’s return (~1200BC), spread on Greek peaks over 600km.
26 November 2002 CS 201J Fall 2002 31
First Line (Paris to Lille), 1794
26 November 2002
Mobile Semaphore Telegraph
Used in the Crimean War 1853-1856
CS 201J Fall 2002 32
• Latency
Time from sending a bit until it arrives seconds (or seconds per geographic distance)
• Bandwidth
How much information can you transmit per time unit bits per second
26 November 2002 CS 201J Fall 2002 33
• Napoleon’s Network (Paris to Toulon, 475 miles)
• Latency: 13 minutes (1.6s per mile)
– What is the delay at each signaling station, how many stations to reach destination
– At this rate, it would take ~1 hour to get a bit from
California
• Bandwidth: 2 symbols per minute (98 possible symbols, so that is ~13 bits per minute
– How fast can signalers make symbols
– At this rate, it would take you 3 ½ years to download today’s slides!
26 November 2002 CS 201J Fall 2002 34
• Less transfer points
– Longer distances between transfer points
– Semaphores: how far can you see clearly
• Telescopes can help, but curvature of Earth is hard to overcome
– Use wires (electrical telegraphs, 1837)
• Faster travel
– Hard to beat speed of light (semaphore network)
– Electrons in copper travel about 1/3 rd speed of light
• Faster transfers
– Replace humans with machines
26 November 2002 CS 201J Fall 2002 35
• Faster transmission
– Train signalers to move semaphore flags faster
– Use something less physically demanding to transmit
• Bigger pipes
– Have multiple signalers transmit every other letter at the same time
• Better encoding
– Figure out how to code more than 98 symbols with semaphore signal
– Morse code (1840s)
26 November 2002 CS 201J Fall 2002 36
A collection of multiple networks connected together, so messages can be transmitted between nodes on different networks.
37 26 November 2002 CS 201J Fall 2002
who
26 November 2002 CS 201J Fall 2002 38
• 1800: Sweden and Denmark worried about
Britain invading
• Edelcrantz proposes link across strait separating Sweden and Denmark to connect their (signaling) telegraph networks
• 1801: British attack Copenhagen, network transmit message to Sweden, but they don’t help.
• Denmark signs treaty with Britain, and stops communications with Sweden
26 November 2002 CS 201J Fall 2002 39
• October 1969: First packets on the
ARPANet from UCLA to Stanford. Starts to send "LOGIN", but it crashes on the G.
• 20 July 1969:
Live video (b/w) and audio transmitted from moon to Earth, and to several hundred televisions worldwide.
26 November 2002 CS 201J Fall 2002 40
• Packet Switching: Leonard Kleinrock (UCLA) thinks he did, Donald Davies and Paul Baran,
Edelcrantz’s signalling network (1809) sort of did it
• Internet Protocol: Vint Cerf, Bob Kahn
• Vision, Funding: J.C.R. Licklider, Bob Taylor
• Government: Al Gore
(first politician to promote
Internet, 1986; act to connect government networks to form “Interagency Network”)
26 November 2002 CS 201J Fall 2002 41
• PS7 is due week from tomorrow!
• New ps7.zip will be available later today:
– Grid that uses cartesian coordinates
– Better server
– Small changes to SnakeInterface
• If you have made enough progress that any of these changes cause problems, email me for restitution.
• Enjoy your Thanksgiving!
– Remember if you eat too many apples, you will grow and it will be harder to avoid running into your tail!
26 November 2002 CS 201J Fall 2002 42