IPO
So far, we've written programs that display output on the console.
If we needed data in our program, we typed it in the source code.
It is more likely that most of your inputs will come from other
source, such as a file or from the user. You'll work with files
in the next Java course, but in this course we can look at a few
different ways to get user input.
IPO Structure
The basic foundation of any program is the IPO structure. IPO
stands for Input-Processing-Output:
- Input consists of the data that goes into the
program. Most programs need to have data to work with,
so as a programmer, you need to decide how the input
gets into the program. Is it typed in by the user?
Is it read from a file?
- Processing is what happens to the data inside
the program. This is where a lot of your code will
be involved, especially in larger programs. Sometimes
there are many different kinds of processing tasks
going on in the same program. Such tasks include
calculating, manipulating text or numeric data,
searching, sorting, and comparing.
- Output is the end result of your program, or
the results of the processing. The inputs are
transformed into outputs by the processing tasks
that have been performed. The programmer also needs
to decide what form the outputs will take. For
example, are they displayed on the screen, printed
with a printer, or saved to a file?
Example:
A program calculates the area of a room.
Possible inputs, processing, and outputs might be:
- Inputs: length of the room, width of the room
- Processing: calculate the area using
length and width
- Output: the area of the room
IPO Charts
An IPO Chart is one of several tools that programmers use when
designing a program (before coding it!). An IPO chart
has areas for Input, Processing, and Output, and allows
you to plan out what your program needs to do.
When completing an IPO chart for a program, you actually
do it in this order:
- Output: What is the goal of the
program, what information should it produce?
Why do we do this first? If you were going away
on a trip, first you'd have to decide where you were
going! So when designing a program, first you have
to decide what it's supposed to do, what it
needs to produce or show the user.
- Inputs: Next, decide what inputs
you need in order to produce the desired output(s).
What piece(s) of data are needed?
- Processing: We do this last, once
we know the outputs and the inputs needed to
determine those outputs. Here is where we decide
how we create each output by transforming the
input data. Typically we'd write a note about
how each output is obtained.
Example
Inputs | length of room width of
room |
Processing | area = length * width |
Outputs | area of the room |
Exercises
1. Define the inputs, processing, and outputs for a program that
gives the surface area and volume of a cylinder.
(hint: http://math.about.com/library/blmeasurement.htm
or http://www.1728.com/diamform.htm)
2. Define the inputs, processing, and outputs for a program that
displays the future value of an investment for a
principal P at a rate R compounded yearly for n years. The formula
for compound interest is final amount = P(1+R/100)n.
[solutions]
Exercises
The following exercises will help us put together everything
we've learned so far about writing Java programs.
For each of the programming questions below, draw up an
IPO chart outlining the inputs, processing, and outputs
each program solution requires.
- Develop a solution for a program that converts
a temperature from Fahrenheit to Celsius. (Celsius
temperature = 5.0/9.0 * (Fahrenheit temperature - 32.0).
- Develop a solution for a program that calculates
a person's target heart rate (a target heart rate is the
heart rate you should aim to maintain while you're working
out). Use the following formula: Target Heart Rate = .7
* (220 - your age) + .3 * Resting Heart Rate (your resting
heart rate is your heart rate while you're not participating
in any physical activity).
- Write a tip calculator that allows a restaurant or
bar customer to calculate the amount of a tip to give their
wait person. The program should allow the customer to
specify the bill amount and the percentage of tip they
would like to give.
- The three ingredients required to make a bucket
of popcorn in a movie theatre are popcorn, butter, and a
bucket. Write a program that requests the cost of these
three items and the price of a serving of popcorn (assume
we have only one size). The program should then display
the profit.
- You would like to be able to calculate your
average grade for all the courses you've taken in first
semester. Write a program that prompts you for the grades
for the five courses you would have completed in first
term. After recording all five grades, display the
average grade on the console.
Sample output using sample data:
You entered your five grades as: 85.6 78.9 75.5 90.0 86.5
Your average grade: 83.3
[solutions]