1974: DoD is using 450 different programming languages, spending $3 billion/year on software maintenance ($14 billion in 2012 dollars). A major part of "software maintenance" is fixing BUGS!. A search begins for an existing language most suited to DoD requirements ...
1977: No existing programming language is suitable. DoD solicits proposals for a language appropriate for embedded computer applications (i.e., command and control, communications, avionics, shipboard, test equipment, software development and maintenance, and support applications). ["Steelman"]
1980: DoD completes specification of the "ADA" language and subsequently mandates its use.
Here's ADA code essentially equivalent to prompt( "Hello, World!" );
with Text_IO; use Text_IO;
procedure Hello is
begin
Put_Line("Hello, World!");
end Hello;
1996: DoD now using only 37 different programming languages. Much new code is being written in ADA, which is easier (thus less costly) to maintain: ADA was designed to be less prone to the kind of bugs that make software vulnerable to attack.
Today: ADA code is executing on US Navy systems such as the Aegis Weapon System, SSN-21 AN/BSY-2 Submarine Combat Control System, V-22 Osprey, AGM-114 Hellfire missile, CH46 Cockpit Control System, Tomahawk missiles, MK 41 Vertical Launch System.
alert( ) and prompt( )alert( ) pops up a window with a message given by
whatever string you put between the ( )s. Try entering
alert("Hello World!"); in our Javascript
interpreter. Because this doesn't rely on having some kind of
interpreter window, alert( ) is a good way to
communicate information to the user in a program. To get values
from the user, Javascript has a function called prompt( ).
You give it a message indicating to the user what information is
supposed to be provided, and prompt( ) pops up a
window with the message and an input line. Whatever string gets entered
on that line gets returned as the value of the prompt( )
function.
//
until the next newline character is ignored by the interpreter,
and so we use // to write comments.
Below is a simple program that gets the users height and weight and returns to the user what his weight would be if he were 25 feet tall. Play with it, understand it, and see if you can modify it so that it asks the user for a target height (in feet) rather than always using 25.
total keeps a running total of the cost, which
gets updated as the user orders a certain number of burgers, then a
certain number of fries, then a certain number of drinks.
An interesting twist is to ask for the program to repeat the user's
order before giving the total. We can accomplish this by keeping a
running list of what gets ordered. The variable total is a
number, and is initally zero to indicate that, at the beginning, the
user hasn't yet ordered anything. To keep a running list of what's
been ordered, we need a variable (we'll call it order) of
type string. Initially, since nothing has been ordered,
that string will be the empty string, i.e. "".
In this second version of the program, all three variables
— total, num and order
— are constantly being assigned new values.

ex0.html shown below:
|
← |
The important thing to notice is that all we really need
to do is take the javascript program and surround it by:
<html> <body> <script type="text/javascript"> </script> </body> </html>In fact, that's all we ever really have to do. |
file:// followed by ... the full path to
your file. In fact, you could type that into the URL and skip
the control-o thing!
ex0.html in the browser. For the remainder of
this lesson, let's use google-chrome ex0.html.
When chrome opens the file, it'll run the program, and the
prompt and alert windows will pop up. Press
refresh
ex0.html by replacing
toTemp = (fromTemp - 32)*5/9;with
toTemp = (fromTemp -* 32)*5/9;which will, of course, cause problems. Now refresh chrome, and what happens? A whole lotta nothing! Nothing happened, which means that there's a problem, but we're not given much of a clue as to where the problem lies!
The reason I'm asking you to use chrome for this exercise is that it has a nice Javascript debugger built in. Click on the wrench icon in the top right corner and choose Tools / Developer Tools from the resulting menu and submenu. Click on the Console tab and chrome's debugger tells you what the error is and where! Moreover, if you click on the linked line number, it'll show you the Javascript code and precisely where the error is. This kind of error, i.e. when the browser won't even try running your program because it has some kind of structural problem, is called a syntax error.
Go ahead and fix ex0.html by taking out the
spurious *, and save. Now when you refresh, the program will
run normally. Assuming you still have Developer Tools open,
choose the Sources tab, click on the triangle in
the upper-left corner of the tab window, select ex0.html, and
click on the line number 8, the line you just fixed, in the
source code that pops up. You
should see a blue arrow at that line number. Now refresh the
page. The program will start, but stop executing at exactly
that line. You've added what's called a breakpoint.
If you click on the console button
you'll pop up a Javascript interpreter. You can type
expressions using the existing variables and find out their
values. In fact, you can enter any Javascript you want here!
This can be a very helpful tool when your program doesn't do
what you want. By clicking the Step over next function call
button
, you can
continue on in your program a statement at a time.
ex0.html. Suppose we
want to let the user enter either F or C as units and then
convert to the other unit. We might start off like this:
// This program converts between Fahrenheit and Celsius
var fromTemp = prompt('Enter temperature');
var fromUnits = prompt('Enter units');
var toTemp;
var toUnits;
The problem is, what we do next depends on
whether fromUnits is "C" or "F".
We can test whether fromUnits is "F" using the
==, which returns true if what's on
its left and right are equal, and false otherwise.
So, fromUnits == "F" is true if and
only if fromUnits is "F". Next we need a way to
say "if this thing is true do one thing, else the other thing".
There is a special kind of statement, the if statement
that does exactly that:if (fromUnits == "F")
{
toTemp = (fromTemp - 32)*5/9;
toUnits = "C";
}
else
{
toTemp = fromTemp*9/5 + 32;
toUnits = "F"
}
The { }'s demarcate what are called blocks. You can
stick as many statements in the block as you like —
including other if-statements! Now we can finish
off the whole program!
if statement is well understood, and having the ability for operators to define system behavior through the use of programming languages is a key part of adaptable and configurable systems. One of the key strengths of the Aegis Combat System is the ability for operators to define system behavior through if statements. Operators use if statements to identify threats, alert operators to take action, and control engagements.
ex1.html |
<html>
<body>
<script type="text/javascript">
// This program converts between Fahrenheit and Celsius
var fromTemp = prompt('Enter temperature');
var fromUnits = prompt('Enter units');
var toTemp;
var toUnits;
// Do the conversion
if (fromUnits == "F")
{
toTemp = (fromTemp - 32)*5/9;
toUnits = "C";
}
else
{
toTemp = fromTemp*9/5 + 32;
toUnits = "F"
}
// Display result
alert(fromTemp + fromUnits + " = " + toTemp + toUnits);
</script>
</body>
</html>
|
Modify this program so that units in the answer are written out in their full names: Celsius and Fahrenheit.
== is an example of a comparison
operator. It means "equals", and its
cousin != means "does not equal". These operators
work with any two values, regardless of their type, and more or
less do what you'd expect. Note, however, that automatic type
conversions take place before the comparison, so
0 == "0" gives true.
The other comparison operators are:
< ← less than,
> ← greater than,
<= ← less than or equal to, and
>= ← greater than or equal to.
On numbers they operate just like you'd expect. Perhaps
surprisingly, they also operate on strings. If variables
foo and bar contain strings,
foo < bar is true if and only if the string
foo comes before bar in alphabetical
order. The same logic goes for the other operators. So, for
example:
"ab" < "ba" → true "ab" < "ac" → true "ab" < "aa" → false "ab" > "ab" → false "ab" >= "ab" → true
The typeof a comparison expression
is Boolean, i.e. true or false.
ex2.html |
<html>
<body>
<script type="text/javascript">
// This program compares input values
var inputNum = prompt('Enter a number');
var specialNum = 4;
// Do the comparison
if (inputNum < specialNum)
{
alert("You guessed too low.");
}
else if (inputNum > specialNum)
{
alert("You guessed too high.");
}
else
{
alert("You are a winner!");
}
</script>
</body>
</html>
|
It's very hard to write programs that deal gracefully with unanticipated input. The author of the above program didn't anticipate that some user might not enter a number, so he didn't make sure the program would do something sensible in that case. Many security problems boil down to exactly this: an attacker presents a program with strange input that the programmer didn't anticipate and which, as a result, the program doesn't handle safely. In this example it allowed an attacker to win a game without actually guessing the right number. In many cases the consequences of similar bugs have been a lot greater.
We can fix this particular problem with the program, but it makes the program more complicated. Here's one possibility. It makes use of the fact that if s is a string that can't be reasonably interpreted as a number,Number(s)
returns a special value NaN, which means "not a number".
The isNaN( ) function allows us to test whether an
object is in fact NaN.
// This program compares input values
var inputNum = prompt('Enter a number');
var specialNum = 4;
if (isNaN(Number(inputNum)))
{
alert("Enter a number, doofus, not '" + inputNum + "'!");
}
else
{
// Do the comparison
if (inputNum < specialNum)
{
alert("You guessed too low.");
}
else
{
if (inputNum > specialNum)
{
alert("You guessed too high.");
}
else
{
alert("You are a winner!");
}
}
&&, ||, !fromTemp is in Fahrenheit if
the user enters "F" or "f" as units. We need a way to express
"or". Javascript has boolean operators, i.e. operators
that operate on values of type Boolean. They are
|| — the expression a || b
is true unless both a and b are false.&& — the expression
a && b is true only if
both a and b are true ! — the expression !a is
false if a is true, and true if a
is false.
||, we can rewrite the "if" from ex0.html as
fromUnits == "F" || "f". This doesn't do at all what you think. You see, you'd be asking for the
|| of two strings, not
two booleans, and what that would mean is something
we haven't talked about ... and won't!
if (fromUnits == "F" || fromUnits == "f")
{
toTemp = (fromTemp - 32)*5/9;
toUnits = "C";
}
else
{
toTemp = fromT*9/5 + 32;
toUnits = "F"
}
Question: suppose you had a
variable x. How would you test whether 0 ≤ x ≤ 1?
click for answer.
| Directions | ex3.html |
Number(miles)If it gives NaN, the user didn't enter a proper number. Except that Number("") = 0, so you have to check that specially. Whew! Can you see why so many programs fail to check for every possible kind of bad input! | <html>
<body>
<script type="text/javascript">
// Get miles and gallons from user
var miles = prompt("Enter miles travelled");
var gallons = prompt("Enter gallons used");
// Compute mpg
var mpg = miles/gallons;
// Display result
alert("mpg = " + mpg);
</script>
</body>
</html>
|
| Directions | ex4.html |
|
<html>
<body>
<script type="text/javascript">
// Get ASCII value from user
var a = prompt("Enter ASCII number");
// Get the letter from the number
var s = String.fromCharCode(a);
// Display result
alert("ASCII value " + a + " gives character '" + s + "'");
</script>
</body>
</html> |