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.
The basic foundation of any program is the IPO structure.
Why Have an IPO?
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.
Things To Do First
For example, are they displayed on the screen, printed with a printer, or saved to a file?
A program calculates the area of a room. Possible inputs, processing, and outputs might be:
- Inputs: length of room, width of room
- Processing: area = length * width
- Output: the area of the room
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.
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