[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
COMP2100/2500
Homework 3Write the following program, following the Initial PSP and filling in the Project Plan Summary and a Defect Recording Log.
For the planned Total New and Changed lines of code make an estimate. At this stage it will be little better than a guess, but part of what we're trying to develop is the quality of those guesses. Of course you'll need to read the requirements below carefully before making your guess.
Use the ‘To Date’ and ‘To Date %’ figures from the form you filled in in Lab 2 when you complete the ‘Plan’ column.
Write a Java program to count and calculate the average (or arithmetic mean) of a list of integers. Here is a sample interaction with the program:
[comp2100@karajan]$ average Please enter data values, one per line. > 1 > two Invalid input, try again. > 2 > 2 > Here is that data again: 1 2 2 N = 3 Mean = 1.6666666666666667The program must prompt for each data value by printing the string “> ” at the start of a line. It must read one line of input at a time. If the line is a valid integer, it must add it to the list, otherwise it must ask the user to try again. Data entry is terminated by an empty line. The formula for the mean m of the numbers x1 ... xN is
m = (x1 + ... + xN) / N
The calculated mean should be a double.
Hints
Again I think you can save yourself a bit of work by using a Scanner to read the input. I actually used two, one to read a whole line at a time from the input, and the second to see whether there was an integer on that line. But I'm sure there are other ways.
You may want to store the integers in an array. But be careful, there is nothing in the requirements for this program that puts any limit on the length of the input. You would be better to use a resizable data structure like an ArrayList instead. (Look it up in the API documentation.) If you decide to go this way, try using an Iterator when you need to traverse the list.
You may need to look up some useful methods in class String. I used trim() and length().
Test your program with various inputs including the data from the example above, until you are sure that it is correct. Don't forget to try an empty data set. (Sure enough, my first version crashed on a data set with zero elements.) What other bad data can you think of?
Extension task
Figure out how to print the average rounded to two decimal places. One way to do this is (roughly) to multiply by 100, add 0.5, cast to an integer and then divide by 100 again. But there may be better ways hiding in the standard libraries. Go digging around in there and see what you can find.
Defect Type Table
Here is the table of defect types that you need to use when filling in the ‘Type’ column of your Defect Recording Log.
Type Number Type Name Description 10 Documentation comments, messages 20 Syntax spelling, punctuation, typos, instruction formats, missing reserved words, symbols, sections... 30 Build, package change management, version control, compiling the wrong version of a file 40 Assignment declaration, duplicate names, scope 50 Interface procedure and function calls, wrong types or numbers of arguments, wrong feature name, wrong class, input/output, user formats 60 Checking error messages, inadequate checks, wrong pre- and post-conditions 70 Data structure, content 80 Function logic errors, loops, recursion, computation, function defects - these are the real algorithmic mistakes 90 System configuration, timing, memory - you shouldn't see many of these at this stage 100 Environment design, compile, test and other support systems - you shouldn't see many of these either, although perhaps things like having your program work at home but not in the lab might fit here This table is based on Table 12.1 on page 141 of Introduction to the Personal Software Process by Watts Humphrey (Addison-Wesley, 1997).
[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.2, Thursday, 17 February 2005, 13:30:01 +1100
Feedback & Queries to
comp2100@cs.anu.edu.au