Sie sind auf Seite 1von 4

COMP 1020

Material covered:
Please ensure that you:
Part A: Shopping ArrayList
Part B: Scatterplot

MATERIAL COVERED:

Use an ArrayList to store a collection in Part A

Process multi-dimensional arrays in Part B

top

PLEASE ENSURE THAT YOU:

solve all problems with programs written in the Java programming language;

follow the programming standards;

submit a separate complete program for each part that can be saved in a single Java source
code file, and compiled and executed without any other Java files; and

submit your solution to both parts of the assignment by the assignment due date.

If you submit a solution that does not meet these requirements (for example, if the program does not
run), you will lose some or all marks.

top

PART A: SHOPPING ARRAYLIST


A classic application program is the shopping list: a way of keeping track of which groceries or other
items need to be bought, and which of them have already been purchased. Your answer to this
question will implement such a shopping list, and simulate a user's changes to the list with data read
in from a text file.

First, you will need a class to represent items in the list. Objects of this class will contain two instance
variables, a String nameof the desired item, and an int quantity that needs to be purchased.
Secondly, you will need two ArrayLists in your program. The first list will keep track of items that still
need to be bought (call this the shopping list), and the quantity needed. The second will keep track of
the items purchased, and the quantity that has been purchased (call this the purchase list).
The data file used as input to the program will consist of lines containing one of three commands. The
first of these is add, which is followed by a quantity and an item name, separated by commas. This
will add to the shopping list; if an item with that name already exists, increase the quantity desired by
the given number. Otherwise, add a new item to the list with that name and quantity. The second
command is buy, which is also followed by a quantity and an item name. This will add to the
purchase list in a similar fashion. Also, if an item with that name is already in the shopping
list, reduce the quantity desired by that number. If the quantity of that item reaches zero (or less),
remove it from the shopping list. Finally, the list command should print out both lists, showing both
the quantity and the item name, one item per line. For example, the data file:
add,3,loaf of bread
add,2,jug of milk
list
buy,2,loaf of bread
add,4,loaf of bread
buy,3,jug of milk
buy,1,stick of butter
list
will display something similar to the following:
==============
Shopping List:
3 - loaf of bread
2 - jug of milk
Purchase List:
==============
Shopping List:
5 - loaf of bread
Purchase List:
2 - loaf of bread
3 - jug of milk
1 - stick of butter
Use the data file a3a.txt to test your program. Hand in your Java source code and a copy of the
output produced by running your program on that file.

top

PART B: SCATTERPLOT
Write a complete Java program that draws a scatterplot by reading in points of data from a file and
displaying them. The input data file consists of a pair of integers representing a point on each line of
the file; the first integer is the x coordinate, and the second is the y coordinate of the point. It should
then plot a least squares regression line on top of the points.
You may assume that all valid points have x-coordinates in the range [0, 40] and y-coordinates in the
range [1, 20]. The least-squares line is calculated using the following formula:

Your program should be able to deal with errors in the data file (by catching NumberFormatException
when converting text to integers). Ignore lines containing invalid data or points with coordinates out of
range, but do not stop reading the file. For example, given the input file:
20 10
01
40 20
13 17
13 12
10 ?
the x coordinates represent time
15 0
10 20
lines 6 to 8 would be ignored, but the last point would be plotted.

Your program should plot the points using text (for example, with "X"s representing the points, "-"s the
regression line segments, and "*"s where a line segment and a point are located at the same spot).
You can either display the output on the screen, or write it to an output file; the data file above would
produce the following output:

|
X
---X
|
--|
--|
X
--|
--|
--|
--|
---|
-*|
--|
--X
| --|--|
|
|
|
|
|
|X
+----------------------------------------Use a two-dimensional array of characters to store the plot.
Your program should print two scatterplots, using the input files a3plot1.txt and a3plot2.txt.

top

Copyright University of Manitoba, Distance and Online Education.


Distance and Online Education uses Google Analytics