Problem Solving and Computing (CSC-103 98S)
Outline of Class 10: Conditionals and Reflection
Held: Thursday, February 19, 1998
- Journal assignment 8 is now
ready.
- I've worked out a set of
resolutions for
problem set 3.
- I've also worked out a set of
resolutions for much
of problem set 4.
- If possible, I will return all the assignments I can find in my
office.
- A review sheet for the
first exam is now available.
- We'll continue to spend time on
computing assignment 5.
- We'll also begin computing
assignment 6.
- Next Tuesday, we'll return to problem solving. In preparation for
that class, please read chapter 3 in Thinking Mathematically.
- I'm thinking about an alternative final project for the class. What
do you think about working together to provide an online guide to
techniques for problem solving?
- Those who are more inclined toward programming can develop programs
that help illustrate issues.
- Those who are more inclined toward problem solving can develop
a number of sample resolutions.
- Those who are more inclined toward reflection can work on descriptive
text and an overall structure.
- Some would also find new problems (to create a reasonable "problem
bank") and research other techniques of problem solving.
- Of course, we'll all be expected to work at least a little bit on
each part.
- To turn in a programming assignment, mail it to me with a command
like
% elm rebelsky < File.java
- At this point, it may be helpful to reflect on Java's perspective
on what a program is, and what issues relate to programs.
- In Java, a program is a collection of cooperating objects.
- The cooperation is governed, in part, by a
main method
that creates an initial set of objects and tells them to do some
basic things.
- In addition to the world of the program, Java also deals with a
"metaworld" (ideal world) in which generic (ideal) objects reside.
You populate your programs with copies of these generic objects.
- The generic objects are called classes.
- You can build your own classes. For now, the main aspect of a class
will be the methods (capabilities) it provides.
- Many classes provide a
main method. This method says
"if I controlled the world, this is what I'd have it do."
- Most methods provide more general utility. For example, the
class
rebelsky.io.SimpleInput provides a
readDouble method that allows you to read values
that the user types.
- Methods take zero or more inputs (parameters) and return zero
or one values.
- Methods contain zero or more variables (containers that hold values)
and zero or more statements (instructions for doing things).
- Variables, parameters, and methods are all typed. That is,
we describe the type of object that the represent (and, therefore,
the capabilities).
- Types are similar to classes.
- Right now, we know a few simple types.
-
double - numbers
-
String - sequences of characters
-
rebelsky.io.SimpleInput - things that can read values
-
rebelsky.io.SimpleOutput - things that can print values
-
void - things that do nothing (used primarily as a
return type from methods that don't return anything).
- Why does Java include methods? Why do we use them when writing
algorithms/programs?
- There are a number of reasons.
- Methods clarify algorithms. Rather than writing some complicated
formula (such as X*9/5 + 32), you can represent them by a more
readable name (such as
centigradeToFahrenheit).
- Methods allow you to divide the work in your programs and to
think at a higher level. Once you have a method for converting
Centigrade to Fahrenheit, you can use it in a variety of situations
- Converting a single value
- Building a table
- Determining the Centigrade value that corresponds to a
particular Fahrenheit value (basically, you can try to run the
method "backwards" by guessing).
- Methods allow you to extract common code. As you write larger and
larger programs, you'll find that you're doing that more and more.
- The
if or conditional statement is one of the
simplest control structures.
- Java provides the two basic forms of the
if statement.
-
if (test) statement executes the
statement only when the test holds.
- Note that the test must return a boolean (truth) value.
- Note that the test must be surrounded by
parentheses.
-
if (test) statement else
statement selects between the two statements. It executes
the first if the test evaluates to true and the second if it
evalutes to false.
- Java does not provide a special facility for nesting
if
statements.
See absolute values,
letter grades,
and grade averaging,
in computing exercise 5
for an example and some exercises.