CSC201Lab5 1516
CSC201Lab5 1516
In the Assignment tab of our Portal page download the file csc201mod5_1516.jar
and itp120mod5deitel.jar. The second one has the examples from the textbook. The first
one is the one we will be looking at in this module. There are a number of packages in
this file. YOU WILL ONLY BE TURNING BACK IN A PACKAGE NAMED
dwolffmod5 (except with your name) with the programs shown at the bottom of this
document.
In the lab module, write your comments in the text box (see the syllabus for
information). Attach your jar file back in the Assignment and submit it back through the
portal.
1. Lets first look at using arrays for collecting items together. Arrays have the
advantage of being very fast and very useful if you know how many items your
collection will hold.
2. Lets first look at arrays of primitive data types. We will be looking at samples in
the mod5wolff01arraysPrimitive package of our jar file.
3. Why do we need collections? Look at Ex1NoArrays and Ex2NeedsArrays.
Read the comments. Arrays store sets of data for later use.
4. Ex3SumArray shows you how to use an initializer for setting values in the array
while you declare it. A nice short hand way to do this if you know the data ahead
of time.
5. Look at Ex4. and Ex5. You need to know how large an array is at creation time,
but you can ask the user at runtime and create the correct size, if that is an option.
If not, you will need to create an array bigger than you ever need (see Ex6.). In
this case, you need to keep a running counter of how many of the entries are valid.
If you fill the array and need more room, you could make the array bigger and
copy the current data into the new array (see Ex7.). Note carefully in this example
how the method call and the method definition handle arrays. You do not give a
size in the method definition but do include the [ ]. In the method call you just
use the array name.
6. Ex8. shows an example of the enhanced for loop discussed in the text.
7. Your book discusses the use of the Arrays class and some methods for searching
and sorting arrays from this class. You should also be familiar with searching and
sorting via the actual algorithms. The last programs in this package demonstrate
this.
8. You often want to have arrays of reference data types. Check out the
BandBooster and Dog examples in the mod5wolff02arraysClasses package.
9. Look at the blueprint for a ExCD. Nothing new here. We have a four private
instance variables, the constructors, toString(), and getters and setters.
10. Now we want to have another blueprint class that represents a collection of CDs.
Look at the ExCDCollection class. The instance variables include a collection of
CD objects, a total cost, and an integer that keeps track of how many CDs are
present. This last instance variable is required if you do not know how many CDs
are present. Note that the constructor creates an array large enough for 100 CDs.
Study the code for the addCD() method and the increaseSize() methods. We not
only have to keep track of how many actual CDs that are present, we also need to
deal with making the array bigger if you collect over 100 CDs. Run the driver
program (ExTunes) and see how the execution proceeds.
11. To do: Problem 7.19 in your textbook (name it Airline)
12. Look at the programs in the package named mod5wolff03SalesArray. This
example revisits the sales that we had in the previous module. But in this case,
we can use arrays to improve the program and store the inventory, customers, and
sales.
13. To do: SALESDRIVER CLASS
SALESDRIVER CLASS
Write a SalesDriver (no blueprint for this) class that has the following methods:
main which creates an array of daily sales values for seven days
of a week. Use the following to do so:
double[] sales = {45600,65000,34750,87450,82345,33440,79000}
a method that calculates the total sales for the week named
total()
a method that calculates the average daily sales average()
a method that determines the maximum sales for any day of the
week named max().
a method that determines the mimimum sales for the week
named min().
a method to determine what day of the week the maximum sales
occurred. Assume we start our day on Sunday. Name it
maxDay().
a method to determine what day of the week the minimum sales
occurred. Assume we start our day on Sunday. Name it
minDay().
The first four methods will need to define the array as a parameter
that is passed in on the method call. So the methods will have a
single parameter which is a double[]. You can design the last two
methods with whatever parameters you might need.
After you create the array in main, call and print the results of
each of the six methods so that they can print their information.
Check out Ex9PrimitiveArrayWithMethods for an example on how to start.
Yours will look similar but will have seven total methods: main()
plus the six above.
Sample output:
The total sales for the week was: $427,585.00
The average daily sales was: $61,083.57
The maximum sales of a day was: $87,450.00 occurring on Wednesday
14. To do:
a. Take your Stock and StockDriver blueprint and driver from your mod 4
answers and copy it into this module. Change the name of the driver to
StcokDriverArray.
b. We are going to create an array of Stock instances in the driver and fill in
data from the keyboard. We do not know how many there are ahead of
time. We have examples of how to handle arrays of unknown size. Check
out DogArrayUnknownSize.java. NOTE: it MUST be an array not an
ArrayList. Lets assume that we will not have over 20 Stock instances so
make your program to handle any number of them up to 20 (you do not
need to write an increaseSize() method).
c. The main loop should just be reading in the data by asking the user for
values for the four fields, creating the instances and placing them in the
array.
d. Create methods for
i. printing the array elements
ii. calculating the total number of shares of Stock purchased
(accumulate the numShares field)
iii. calculating the total value of all of the stock
(NOTE: since these methods are related to multiple invoices, they
must go in the driver class not the blueprint. The only thing that
can go into the blueprint is anything that has to do with ONE Stock
instance.
iv. calculating the net value of all of the stock
e. After your loop in main(), call the four methods in d. above to print out the
data and summaries.
HINT: since we do not know the size of the array, you will need a counter and each
of the three methods above will have parameters of the Stock array and that counter.
Check out the DogArrayListUnknownSize. For similar calculations, also check out
the BandBoosters (except that one is a full array, so you will need to pass in the
counter that indicates how many pieces of data are actually valid).
Sample output at the bottom
You typically then make the array bigger than you would ever need. Then you
must have a variable that keeps track how many pieces of data are really entered
as we did with the CDs.
Arrays are very hard to insert and delete data from, especially for the center
elements. For instance, assume that the array help alphabetized words and you
wanted to add one in the middle or delete one from the center. Think about the
problems this would cause.
Arrays contain no methods. Methods are what make Java classes so powerful!
16. An alternative way to store a set of objects is in an ArrayList. This is part of the
Java Collections and is found in the java.util package. ArrayLists have a rich set
of methods as we saw briefly at the end of the chapter in your text. Watch the
Collaborate movie on ArrayLists.
17. Remember to be able to utilize methods, we need to know a) the name b) the
parameter(s) c) the return type. All of this is obtainable if we just know how to
read APIs.
18. Look at the samples in the mod5wolff04ArrayLists package. Look at
Ex1Grades. ArrayLists are collections if reference data types not primitives! If
I want to maintain an ArrayList of whole numbers, I need to use the Integer
wrapper class (we discussed wrapper classes previously). But with boxing and
unboxing, Java allows implicit casting from int to Integer, double to Double, etc.
With this, we can define the ArrayList to be the wrapper class but add primitive
values to it. Note that this ArrayList is defined as containing Integers but line 16
adds the primitive int input from the keyboard to it.
19. Look at Ex2ALDogs. The example we studied earlier used an array of Dog
instances. This one takes that code and converts it over to an ArrayList. note how
that was done.
20. Also look at the Ex3BookDriverAL that uses the Book blueprint. Keep studying
these blueprints so you can write them in your sleep!
21. To do: We will now write a driver using ArrayLists for our Stock from #13 above.
Make a copy of the StockDriverArray and name it StockDriverArrayList.
Convert this program from an array collection to an ArrayList. You do not need
to make any changes in the Stock program (that one blueprint will work for both
drivers or any type of collection). We want it to use an ArrayList instead of an
array. If you have problems, study the code for Birds, BirdsArray, and the
converted BirdsArrayList.
22. Look at the code for Ex5BaseballPlayers as you look at the APIs. Study the code
and match each method call for the name, parameter(s) and return type.
23. To do: Now add code to Ex5BaseballPlayers to complete the comments in the
code.
24. Check out the mod5wolff05SalesArrayList version of our Sales program using
an ArrayList in place of the array.
25. Lets look at another association that utilizes ArrayLists. Now we will rent some
wrecked cars. Here is the UML diagram for our application (found in the
mod5wolff06rentawreck package.
Your final jar file that you submit to me needs to have 10 programs total in a single
package named dwolffmod5 (except with your name):
Step 11: Airline
Step 13: SalesDriver
Step 14: Stock and StockDriverArray
Step 21: StockDriverArrayList
Step 23: Ex5BaseballPlayers
Step 28: Rentals, Customer, WreckDriver, Auto
Remember to also attach your Sales.html file.
Sample output from Stock and StockDriverArray (same output for StockDriverArrayList)
What is the Customer ID?
123
What is the name of stock #1?
IBM
How many shares did you buy?
400
How much did you pay per share?
350.00
What is the current value per share?
500.00
123 owns 400 shares of IBM worth $500.00 per share. You paid $350.00 per share.
The total cost of the stock is $140,000.00 and the current value is $200,000.00
The net value is $60,000.00
More stocks to calculate? (true or false)
true
What is the Customer ID?
123
What is the name of stock #2?
GOOG
How many shares did you buy?
300
How much did you pay per share?
250
What is the current value per share?
225.00
123 owns 300 shares of GOOG worth $225.00 per share. You paid $250.00 per share.
The total cost of the stock is $75,000.00 and the current value is $67,500.00