Primitive Arrays


ArrayExamples.java
 

Classwork

When example code is given, you may use it as a model when doing assignments, unless I say otherwise.


Part A

Create a project, and give it a class with a main.

At a small car rental company they have 10 cars.  Some are compacts, some are SUVs, some are vans.  Each car costs a certain amount  per day to rent.  Some are currently taken and some are available. 

In your main, create an array of 10 Strings that represents the ten cars and says which type ("compact", "SUV", or "van") each is.  Use the special initialization to set up the array (make up which cars there are using the three types, so there are some of each).  After this, add a line of code that changes the type of element #5 in the array to something different than you originally chose.

We will assume that each index across the first array you created and the next two arrays represents one car.  So if index 3 in the first array holds "van" and index 3 in the second array holds 35 and index 3 in the third array holds true, that represents a van which costs $35/day to rent and is currently available. (These are called parallel arrays.)

Create another array of 10 doubles for the prices of each car.  Instead of special initialization, use a for loop, to set the prices so that the 0th car costs $20, the next $25, the next $30 and so on.  After the loop add a statement that changes the price of the first car in the array to the price of the last car - $10 (statement should work even if the price of the last car were changed).

Create an array of 10 booleans to keep track of which cars are currently available.  In a for loop, set the values in the array so that every 3rd car is unavailable, and the rest available.  If necessary, then add individual lines of code so that at least one car of each of the three types is available.


Part B

Write the following code so that it would still work if we went back and added or removed cars (i.e. if the arrays changed size).  For some of this, it will be useful to add extra variables.

Using a for loop, print out a numbered list of all the car types from the first array.  Number starting at 1, so it looks something like

1. SUV 
2. van 
3. compact  

and so on

Then, changing the same loop, add the price information from the second array to what is printed.  Then add on a check of the third array, and add a note if the car is unavailable.  So the output from the first loop ends up looking something like

1. SUV ($75.0 / day)
2. van ($25.0 / day) [unavailable]
3. compact ($30.0 / day) 

and so on

Next, using a second for loop, print the car types out again, but in reverse order, so the one at array index 9 prints first and the one at array index 0 prints last.

Next, in another for loop, print a similar numbered list to the first one, but only of available cars, so if we had the same arrays as before we'd get the following, with the unavailable van left out:

1. SUV ($75.0 / day)
2. compact ($30.0 / day) 

etc.

Next, in another for loop, count how many compacts there are that are currently available.  You will need to check if the current car is a compact and if it is available, so you need to look at information from two arrays.  After the loop print something like

There are currently 3 compacts available to rent.

Next, using another for loop, find the index (the position in the array) of the cheapest available vehicle.  

Using the index you found, after the loop print something like

The cheapest available vehicle is a compact which costs $54.33 per day.

Next, create a new array of doubles with the same length as the prices array.  Using another for loop, set the values in the new array to twice the values from the prices array.  Then print the new array to check it is getting the right values.


Homework

Part A

In the following, write all loops so they would still work if the sizes of the arrays were changed.

In a project, in a main, create an array of doubles of length 10, and give it values using special initialization. 

Then write a loop that goes through the array, and after the loop report the largest and smallest number in the array, and the average of all the numbers.

In the same main, create an array of 10 ints called howManyTimes and and array of 10 strings called words, both using special initalization.

Then go through these two arrays and print each element of words the number of times given in howManyTimes.  So if howManyTimes[3] is 9 and words[3] is "hello" you would print "hello" 9 times. To do this you will need nested loops: an outer loop to move through the arrays, and an inner loop to print the words the right number of times.  You DO NOT need a nested loop for each array.

In the same main, create an array of 10 strings.  Using nested loops, fill it with values so that each element is a string containing a number of x's based on the index, so the 0th element is "", the 1st element is "x" the 2nd element is "xx", and the 9th element is "xxxxxxxxx".  (hint, one loop will move you through the array, the other will add the right number of x's to the current element).

Part B

Create a new package in the same project by right-clicking "Source Packages" and choosing Java Package under New...  Name your package cards.

In this package, create classes Card, DeckOfCards, and DeckOfCardsTest and enter the code from the text into these (figures 7.9-7.11).  (You need not include their comments) 

Play with these classes and try to understand how they work, particularly their use of arrays.