Occasional Lecture Notes
last change: Friday, November 8, 2002
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.
Thursday, Nov. 7, 2002
More on Classes and Methods
Let's look at classes which are instantiated to produce objects. What are
some of the common characteristics of such classes and objects?
-
An object has state (a set of data values, sometimes called fields
or attributes) and behavior (a set of methods). The methods
act on the data values, which changes the overall state.
-
The data values are usually declared to be private, so that a calling
program must call methods to modify them and can't modify them directly.
-
The methods are usually declared to be public, so a calling program
can use them. Sometimes there are also private methods; why would we want
these?
Common kinds of methods
-
Constructor: creates and returns object from a class. Usually has
a list of parameters, and uses these to set the object's data values. The
constructor's name is always that of the class; it has no return statement.
-
"Setter": usually has parameters, which it uses to modify one or
more of an existing object's data values.
-
"Getter": usually has no parameters; it just returns one of the
object's data values.
-
input: reads an objects data values from the keyboard or from a
file.
-
toString: collects (some of) an object's data values together into a string,
suitable for displaying or writing to a file.
Aggregation
An aggregate object is an object that contains references to other objects
For example, an Account object contains a reference to a String object
(the owner's name)
An aggregate object represents a has-a relationship
A bank account has a name
Likewise, a student may have one or more addresses
Thursday, Oct. 24, 2002
Objectives: robust input; writing your own methods
Robust Input
If a user enters an invalid input, the program does not quit, but continues
to let the user try again.
Use loops and boolean flag variables to implement this.
See programs53/ShowInputLoop.java.
Writing Your Own Classes and Methods
-
Sets of reusable classes which provide generally usable methods are the
basis for all modern software development, regardless of language or operating
system.
-
There are a number of kinds of methods:
-
A public method is available
to any program that imports the class; a private
method is available only to other methods within the class. At this time
we'll consider only public ones.
-
A static method is associated
with the class; a non-static
method is associated with each object. At this time we'll consider only
static ones.
-
A value-returning method
is one that returns a value to the calling program; a void
method is one that does not return a value. At this time we'll consider
both value-returning and void methods.
See these files in programs53:Screen.java, Smiley.java, MinMax.java, MinMaxThree.Java.
Tuesday, Oct. 15, 2002
Objectives
Continue general discussion of algorithms
Let's revisit the pizza problem
Find the best value among an arbitrary number of pizzas (not just
3 anymore).
Initial Algorithm for Improved Solution to Pizza Problem
-
For each size of pizza, read in the pizza size and price and compute unit
cost. Compare the unit cost just computed with the previous unit costs
and save the size and price of the pizza whose unit cost is the smallest
so far.
-
Display the size and price of the pizza with the smallest unit cost.
The purpose of step 1 of the algorithm is to perform the cost computation
for each individual pizza and somehow save the size and price of the pizza
whose unit cost was the smallest. After all costs are computed, step 2
displays the size and price of the pizza that is the best buy.
Step 1 Refinement
1 Repeat the following steps for each size of pizza:
1.1. Read in the next pizza size and price.
1.2. Compute the unit price.
1.3. If the new unit price is the smallest one so far, then save
this pizza’s size, price, and unit price.
Step 1 specifies the repetition of a group of steps: step 1.1 (read),
step 1.2 (compute), and step 1.3 (compare). We will repeat these steps
as many times as necessary until all unit prices are computed.
Each time we compute a new unit price, step 1.3 compares it to the others,
and the current pizza’s size and price are saved if its unit price is smaller
than any others computed so far. If the unit price is not the smallest
so far, the current pizza’s size and price are not saved.
Step 1.3 is a selection step because it selects between the two possible
outcomes: (a) save the pizza’s data and (b) do not save the pizza’s data.
Algorithms: Repetitive Control Structures (loops)
There are 3 main kinds of loops:
-
count-controlled
-
sentinel-controlled
-
flag-controlled
-
count-controlled: before starting the repetition, calculate (or
query the user about) how many iterations there will be
-
sentintel-controlled: start, and continue, the repetition until
a problem-specific value appears in the input data
-
flag-controlled: after each iteration, ask the user whether to quit
or continue
How could we use each of these in solving the pizza problem?
How can we initialize the minimum value?
-
set it to 0 before beginning the repetition
-
make the first pizza a special case: read its data before beginning the
repetition; this is called a priming read
Let's generalize the pizza problem so the range of values can be negative
(say, minimum and maximum temperatures)
How do we initialize the min and max?
priming read?
set the min to the maximum possible value; set the max to the minimum
possible value.
Java provides Integer.MIN_VALUE, Integer.MAX_VALUE,
Float.MIN_VALUE,Float.MAX_VALUE, etc.
Thursday, Oct. 10, 2002
Decision ("if") Constructs
Problem: Prof. Feldman is sometimes a bit absentminded and could
use a bit of help in remembering his schedule on the days he's at GW. This
schedule is:
-
Tuesday: Office hours 2-5:30 PM, CSci 53 7:30-8:45 PM
-
Wednesday: Office hours 3-5:30 PM
-
Thursday: Office hours 2-5:30 PM, CSci 53 7:30-8:45 PM
Develop a program to provide this help
Basic Algorithm
-
Prompt user to enter a day of the week.
-
Display the schedule items for that day
Step 2 refinement
if day is Monday, display "work at home"
if day is Tuesday, display Tuesday's items
if day is Wednesday, display Wednesday's items
if day is Thursday, display Thursday's items
if day is Friday, display "work at home"
if day is Saturday, display "have a nice weekend"
if day is Sunday, display "have a nice weekend"
if (day == 1)
// Monday
{
System.out.println ("Work at home today");
}
if (day == 2)
// Tuesday
{
System.out.println ("Office hours, 2-5:30 PM");
System.out.println ("CSci 53, 7:30-8:45 PM");
}
if (day == 3)
// Wednesday
{
System.out.println ("Office hours, 3-5:30 PM");
}
if (day == 4)
// Thursday
{
System.out.println ("Office hours, 2-5:30 PM");
System.out.println ("CSci 53, 7:30-8:45 PM");
}
if (day == 5)
// Friday
{
System.out.println ("Work at home today");
}
if (day == 6)
// Saturday
{
System.out.println ("Have a nice weekend");
}
if (day == 7)
// Sunday
{
System.out.println("Have a nice weekend");
}
if ((day == 1) || (day == 5))
// Monday, Friday
{
System.out.println ("Work at home today");
}
if ((day == 2) || (day == 4))
// Tuesday, Thursday
{
System.out.println ("Office hours, 2-5:30 PM");
System.out.println ("CSci 53, 7:30-8:45 PM");
}
if (day == 3)
// Wednesday
{
System.out.println ("Office hours, 3-5:30 PM");
}
if ((day == 6) || (day == 7))
// Saturday, Sunday
{
System.out.println ("Have a nice weekend");
}
Nested if Statement
if ((day == 1) || (day == 5))
// Monday, Friday
{
System.out.println ("Work at home today");
}
else
{
if ((day == 2) || (day == 4))
// Tuesday, Thursday
{
System.out.println("Office hours, 2-5:30 PM");
System.out.println("CSci 53, 7:30-8:45 PM");
}
else
{
if (day == 3)
// Wednesday
{
System.out.println("Office hours,
2-5:30 PM");
}
else
{
if ((day == 6) || (day == 7))
// Saturday, Sunday
{
System.out.println("Have
a nice weekend");
}
else
// out of range
{
System.out.println("Bad
input");
}
}
}
}
Simplifying Nested if Statements
if ((day == 1) || (day == 5))
// Monday, Friday
{
System.out.println ("Work at home today");
}
else if ((day == 2) || (day == 4))
// Tuesday, Thursday
{
System.out.println("Office hours, 2-5:30 PM");
System.out.println("CSci 53, 7:30-8:45 PM");
}
else if (day == 3)
// Wednesday
{
System.out.println("Office hours, 2-5:30 PM");
}
else if ((day == 6) || (day == 7)) // Saturday, Sunday
{
System.out.println("Have a nice weekend");
}
else
// out of range
{
System.out.println("Bad input");
}
Switch Statements
switch(day)
{
case 1:
case 5:
// Monday, Friday
System.out.println ("Work at home today");
break;
case 2:
case 4:
// Tuesday, Thursday
System.out.println("Office hours, 2-5:30 PM");
System.out.println("CSci 53, 7:30-8:45 PM");
break;
case 3:
// Wednesday
System.out.println("Office hours, 2-5:30 PM");
break;
case 6:
case 7:
// Saturday, Sunday
System.out.println("Have a nice weekend");
break;
default:
System.out.println("Bad input");
}
Thursday, Sept. 26, 2002
Algorithms: the Foundation of Systematic Problem-Solving
"algorithm, a finite collection of simple instructions that can be performed
by a computer and is guaranteed to halt in a finite amount of time."
source: the Analytical Engine, p. 26
QUESTION: Suppose a program is designed not to halt in a finite amount
of time? Is it implementing an algorithm?
Algorithms:the Foundation of Systematic Problem-Solving
"algorithm A rigid mathematical (or logical) relationship which has only
one starting point and one finishing point. ... Each algorithm must have
a finite list of executable instructions which must eventually come to
an end.
"The British mathematician Alan Turing proved [1936] that an algorithmic
approach can be used to solve any mathematical or logical problem for which
a solution is known to exist. This means that one can deduce that any solvable
problem can be handled by a computer, provided the correct algorithm is
used.
"In computing, an algorithm is the plan, routine, or process of defining
a set of instructions so that a computer program can be written to find
the answer / solution to a given problem or task."
source: Prentice-Hall's Illustrated Dictionary of Computing (3rd
ed.) (Prentice-Hall, 1998), p. 19
The Power of Algorithms
Algorithms can be written to solve a multitude of (sometimes very complex)
problems
Yet they are made up of careful combinations of very simple operations
Simple operations are combined in the following ways
-
sequence - one operation (or group of operations) directly follows another
-
condition (selection) - an operation (or group of operations) is executed,
or not executed, based on the results of a yes/no (true/false) decision
-
repetition (loop) - an operation (or group of operations) is executed repeatedly,
a given number of times, or until a given condition is true
Believe it or not, that's all! All algorithms can be constructed this way!
That this is so was proved in 1966 (Böhm-Jacopini).
Thursday, Sept. 19, 2002
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.
(end of document)