Occasional Lecture Notes
last change: Thursday, Feb. 17, 2005
Normally, lecture notes are not formally distributed in this course.
On the other hand, viewgraphs that present material not covered in the
course textbook will appear, from time to time, in this file. New notes
will simply be added to the top of the file. Watch the date at the top
to see if anything has been changed.
Friday, Feb. 4, 2005
How are programming languages different from natural (human) languages?
-
Human languages evolve naturally; programming languages are carefully
designed
-
Computer languages have very fussy rules, but these are easier to learn
because they have fewer exceptions than human languages do
-
A compiler's function is to examine a program in a higher-level
programming
language, then - if the program follows all the rules - translate it to
machine code. Here are the phases:
-
lexical analysis: classify every symbol according to its "part of
speech"
-
syntactic analysis: check for proper "sentence structure" - do all the
parts of speech occur in the proper order?
-
semantic analysis: determine the meaning of the program - does every
symbol
have a well-defined meaning?
-
translation into machine code
Syntactic and semantic analysis
-
Syntactic analysis compares statements in a language to the grammar, or
set of rules, for writing a well-formed statement in that language.
-
Semantic analysis analyzes well-formed statements of a language to
determine
whether they are meaningful.
-
Monkey ate banana.
-
Monkey ate the banana.
-
The monkey ate the banana.
-
George ate the banana.
-
The banana ate the monkey.
-
The banana ate George.
-
Time flies like an arrow; fruit flies like a banana.
-
“We'll see,” said the blind man, as he picked up his hammer and saw.
-
Outside of a dog, a person's best friend is a book. Inside of a dog,
it's
too dark to read.
Frday, Jan. 28, 2005
Example: Average Age of Professors
Problem Specification
You're a very curious person. One thing you're curious about is the
ages of three of your professors. But how can you find out how old they
are? Some people are offended if you ask their age.
Luckily, the GW Undergraduate Bulletin has a list of (most of) the
professors in the back of the book. Each professor's listing gives
his/her degrees, showing the college or university and year of each
degree.
If we assume the typical student is 21 when (s)he earns a bachelors
degree, we can estimate a professor's age just by knowing the current
calendar year and that professor's graduation year.
You're also curious about the mean (average) age of the profs.
Analysis
To solve this problem, we must be given the current year, and the
graduation years of the three profs. The estimated age (to the nearest
year) of a prof can be computed from these values. The mean age will be
the sum of the ages divided by the number of profs (3).
Data Requirements and Formulas
Problem Inputs:
thisYear - the current calendar year, an integer
year - the graduation year of each prof in turn, an integer
Problem Outputs:
age1, age2, age3 - the ages of the three professors (integers)
meanAge - the mean age of the professors
Relevant Formulas
Estimated age = current year - graduation year + 21
Initial Algorithm
- Prompt the user and read in each professor's
graduation
year, then calculate and display that professor's estimated age.
- Prompt
the user and read in
the current year.
- Compute
the mean age.
- Display the mean age.
Algorithm Refinements
Step 2 can be refined:
2.1 Prompt the user and read in 1st
professor's graduation year, then
calculate and display that professor's estimated age.
2.2 Prompt the user and read in 2nd professor's graduation year, then
calculate and display that professor's estimated age.
2.3 Prompt the user and read in 3rd professor's graduation year, then
calculate and display that professor's estimated age.
Test Plan
Let's develop one right here!
Implementation
Start with the template - it's unnecessary to start with a blank page
mfeldman@hobbes.seas.gwu.edu:15:
cp
programs53/ProjectTemplate.java .
mfeldman@hobbes.seas.gwu.edu:16:
cat ProjectTemplate.java
//--------------------------------------------------------------
//| ProjectTemplate.java <--
change to your project filename
//| Use this as a basis for your
own programs
//| Author: <your name>,
The George Washington University
//| Last Modified: <date>
//--------------------------------------------------------------
import cs1.Keyboard;
public class ProjectTemplate
<-- change to your project name
{
public static void main
(String[] args)
{
// put your code here
}
}
Declare the variables and copy the main algorithm steps
as comments: framework file.
//--------------------------------------------------------------
//| MeanAge.java
//| Finds mean estimated age of
three professors
//| Author: M.B. Feldman, The
George Washington University
//| Last Modified: September 9,
2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class MeanAge
{
public static void main
(String[] args)
{
int thisYear;
// input - the current calendar year
int year; //
input - year of a prof's bachelors deg.
int age1; //
output - 1st prof's estimated age
int age2; //
output - 1st prof's estimated age
int age3; // output
- 1st prof's estimated age
int meanAge; //
output - mean of 3 ages
// Prompt the user
and read in the current calendar year
// Prompt the user
and read in 1st prof's degree year
// and compute and
display that prof's estimated age
// Prompt the user
and read in 2nd prof's degree year
// and compute and
display that prof's estimated age
// Prompt the user
and read in 3rd prof's degree year
// and compute and
display that prof's estimated age
// Compute the mean
age
// Display the mean
age
}
}
Compile the framework; fix any errors. Then fill in the
rest of the program statements.
See MeanAge.java in
the program distribution.
Improved Algorithm
We can improve the algorithm a bit by realizing that we don't need 3
separate age variables. We can use one variable over and over, as long
as we accumulate the sum of the ages as we go along. We need a sum
variable; let's call it ageSum.
This gives a new refinement of Step 2:
2.1 Prompt the user and read in 1st professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
2.2 Prompt the user and read in 2nd professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
2.3 Prompt the user and read in 3rd professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
See MeanAge2.java in
the program distribution.
Wednesday, Jan. 26, 2005
Example: Average Speed on Trip
Problem Specification
Next week, you're driving to Long Island to visit your family. When
you
drive, you'd like to know what your average end-to-end speed, in miles
per hour, was for the trip.
You're not very good at arithmetic. Luckily, you'll have your trusty
laptop with you, so you can develop a little program that will compute
your average speed from the distance you've driven and the number of
hours the trip took.
Unfortunately, your laptop is a bit lame-brained, so it's able to
caculate only with "whole numbers" (integers).
Analysis
To solve this problem, we must be given the number of miles (to the
nearest mile) and the number of hours (to the nearest hour). The
average speed (to the nearest MPH) will be the quotient of the number
of miles divided by the number of hours.
Data Requirements and Formulas
Problem Inputs:
miles - the number of miles, an integer
hours - the number of hours, an integer
Problem Outputs:
mph - the average speed, to the nearest integer value
Relevant Formulas
mph = miles ÷ hours
Initial Algorithm
- Prompt the user and read in the number of miles.
- Prompt the user and read in the number of hours.
- Compute the average speed.
- Display the average speed.
Algorithm Refinements
This is a very simple algorithm and needs no refinements.
Test Plan
Let's develop one right here! (Here is what we developed in class.)
| Test # |
Miles |
Hours |
Reason |
Expected Result |
Actual Result |
| 1 |
15 |
3 |
normal |
5 |
| 2 |
10 |
0 |
bad hours |
error |
| 3 |
10 |
15 |
hours > miles |
0 |
| 4 |
A |
5 |
nonnumeric value |
error |
| 5 |
0 |
5 |
miles = 0 |
0 |
| 6 |
-10 |
5 |
miles < 0 |
-2 |
Implementation
Start with the template - it's unnecessary to start with a blank page
mfeldman@hobbes.seas.gwu.edu:15:
mfeldman@hobbes.seas.gwu.edu:16:
cat ProjectTemplate.java
//--------------------------------------------------------------
//| ProjectTemplate.java <--
change to your project filename
//| Use this as a basis for your
own programs
//| Author: <your name> The
George Washington University
//| Last Modified: <date>
//--------------------------------------------------------------
import cs1.Keyboard;
public class ProjectTemplate
<-- change to your project name
{
public static void main
(String[] args)
{
// put your
code here
}
}
Declare the variables and copy the main algorithm steps as comments
//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a
trip.java
//| Author: M.B. Feldman The
George Washington University
//| Last Modified: September 5
2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
public static void main
(String[] args)
{
int
miles; //
input - number of miles driven
int
hours; //
input - number of hours driven
int
mph;
// output - miles per hour
// Prompt the
user and read in the number of miles
// Prompt the
user and read in the number of hours
// Compute the
average speed
// Display the
average speed
}
}
Compile the framework; fix any errors. Then fill in the rest of the
program statements.
//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a
trip.java
//| Author: M.B. Feldman The
George Washington University
//| Last Modified: September 5
2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
public static void main
(String[] args)
{
int
miles; //
input - number of miles driven
int
hours; //
input - number of hours driven
int
mph;
// output - miles per hour
// Prompt the
user and read in the number of miles
System.out.print
("How many miles did you drive (to the
nearest mile)? ");
miles =
Keyboard.readInt();
// Prompt the
user and read in the number of hours
System.out.print
("How many hours did you drive (to the
nearest hour)? ");
hours =
Keyboard.readInt();
// Compute the
average speed
mph = miles /
hours;
// Display the
average speed
System.out.print ("Your average speed
was ");
System.out.println (mph + " miles per hour.");
}
}
(end of document)