itp © ron poet lecture 4 1 program development. itp © ron poet lecture 4 2 preparation cannot just...
TRANSCRIPT
ITP © Ron PoetLecture 4
1
Program Development
ITP © Ron PoetLecture 4
2
Preparation
Cannot just start programming, must prepare first.
Decide what the program will do.Then how it will do it.Testing strategy – does it work?
ITP © Ron PoetLecture 4
3
English Description
Write down what the program will do.Normally given to you on this course.PSD will teach techniques for finding this
out.We want a program to tell us when to put
the meat in the oven so that it will be done on time.
ITP © Ron PoetLecture 4
4
Scope of the Program
Next, work out what the program will do and what it will not do.
Again, you will be told this on this course. Cooking time varies for different types of meat but
has a general formula, so many minutes per kilo plus a fixed extra time. We will expect the user to look this up in a cookbook and enter it in the program.
So our program will not ‘know’ the cooking times for all types of meat.
ITP © Ron PoetLecture 4
5
User Interaction
Next, work out how the user will interact with the program.What information will they give the program.What information will be returned.
What form will this interaction take?The user will interact via a Console
window. They will be prompted for input and then given the desired start time.
ITP © Ron PoetLecture 4
6
Sample Data
Prepare an actual example of how the program will be used.Normal use – happy day scenario.
The user wishes to cook a 2 kilo joint of meat, with formula 40 minutes per kilo + an extra 50 minutes. The want the meat to be ready at 730 pm.
ITP © Ron PoetLecture 4
7
Expected Output
Calculate the expected output by hand.Choose ‘easy’ numbers if possible.Cooking time is 2 * 40 + 50 = 130 minutesStart time is 730 – 130 minutes = 520 pm.
ITP © Ron PoetLecture 4
8
Sequence of Activities
Now work out what our program has to do as a sequence of activities.
Write it in English, being guided by the sample data.
Get cooking formula.Get finish time.Calculate cooking time.Output oven switch on time.
ITP © Ron PoetLecture 4
9
Other Sequences Appear the Same
The following sequence look the same to the user.
It does not matter which one we choose.Get cooking formula.Calculate cooking time.Get finish time.Output oven switch on time.
ITP © Ron PoetLecture 4
10
Other Sequences Solve the Same Problem Differently
We could get the required information from the user in a different order.
The user interaction is different, but the same problem is solved.
Get finish time.Get cooking formula.Calculate cooking time.Output switch on time.
ITP © Ron PoetLecture 4
11
Special Algorithms
How do we do calculations with time? Convert times from hours and minutes to total
minutes.Times like 730 are integers.Hours are time / 100, minutes are time % 100.Total minutes are 60 * hours + minutes.
Do arithmetic in total minutes. Convert back to hours and minutes.
Hours are total minutes / 60.Minutes are total minutes % 60.
ITP © Ron PoetLecture 4
12
Problem Scenarios
Note things that might go wrong. Total cook time of 2 hours with finish time of 130
pm.Start time should be 1130am.
How likely is this.Very likely for Sunday lunch.
SolutionUse a 24 hour clock for times.Assume users will not be cooking over midnight.
ITP © Ron PoetLecture 4
13
Assign Work to Objects
Go back to the sequence of activities and allocate objects for each activity.
Get cooking formula. ConsoleGet finish time. ConsoleCalculate cooking time. mainOutput switch on time. Console
ITP © Ron PoetLecture 4
14
Write a Skeleton Program
The first version of our program should have comments for each activity.As well as defining the main object.
public class CookTime{public static void main(String[] arg){
// input cooking time parameters// input finish time// calculate cooking time// output start time
}}
ITP © Ron PoetLecture 4
15
Step by Step Development
Add activities one by one and run the program, making sure it works at each step.
Add trace statements if necessary to check the values of key variables.System.err.println();
Check the accuracy with hand calculations.
ITP © Ron PoetLecture 4
16
input cooking time parameters
Console con = new Console("Cooking Helper");
con.print("Enter mins per kg: ");
int minsPerKg = con.readInt();
con.print("Enter extra mins: ");
int extraMins = con.readInt();
con.print("Enter meat weight in kg: ");
double weight = con.readDouble();
ITP © Ron PoetLecture 4
17
input finish time
con.print("Enter finish time using 24 hour clock: ");
int time = con.readInt();
ITP © Ron PoetLecture 4
18
calculate cooking time
int cookTime = (int) (minsPerKg * weight + extraMins);
int endHours = time / 100;
int endMins = time % 100;
int endTotalMins = 60 * endHours + endMins;
int startTotalMins = endTotalMins - cookTime;
int startHours = startTotalMins / 60;
int startMins = startTotalMins % 60;
ITP © Ron PoetLecture 4
19
output start time
String out = String.format(“%02d%02d”,
startHours, startMins);
con.print("Put meat in oven at “ + out);
ITP © Ron PoetLecture 4
20
Run The Program
ITP © Ron PoetLecture 4
21
Recap
English description of TaskScope of the program.Interaction with users.Sample data.Sequence of activities.Expected output.Special algorithms.
ITP © Ron PoetLecture 4
22
Recap (2)
Problem scenarios and their resolution.Assign work to objects.Write skeleton program.Step by step development.
Proverb – There is never time to do it right, but always time to do it over.
ITP © Ron PoetLecture 4
23
Example 2
A bank account is a permanent record thatRemembers how much money you have.Allows you to update the amount.
Write a program that uses a file to store a bank account. The program shouldRead the current amount from the account.Get a transaction (adding or subtracting money) from
the user.Write the updated amount back to the file.