Primitive Arrays


Primitive Arrays

ArrayExamples.java

Deitel 7.1-7.4, 7.6, 7.8-7.10

 


Classwork

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


Part A

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.

We will create three arrays to hold the three kinds of information about the cars -- type, cost, and availability.  We will assume that each index across the three 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 a project, and give it a class with a main; all of this code will be in the main:

Create an array of 10 doubles for the prices of each car.  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 minus $10 (statement should work no matter what the price of the last car was and would still work even if the array changed size).

☑ 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). 

Create an array of 10 booleans to keep track of which cars are currently available.  Use special initialization and make sure some of each type are available.

These will function as parallel arrays, so you should assume in the rest of the code that they always have the same length.


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).

Using a for loop, print out a numbered list of all the car types from the first array.  Users like numbers starting at 1 instead of 0, so make it look something like  (hint: what number do you already have that is almost the right number for each line?)

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 (think about how to print things on the same line or not).  So the output from the loop ends up looking something like

1. SUV ($68.0 / day)
2. van ($237.0 / day) [unavailable]
3. compact ($53.0 / day) 

and so on

[EC]  In a new 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 to do this.  After the loop print something like

There are currently 3 compacts available to rent.

hint: you can always add more local variables.

[EC]  Using another for loop, find the index (the position in the array) of the cheapest vehicle.   Using the index you found, after the loop print information about that cheapest car, something like (in this case you can use CS number for the index rather than human number).  (hint: the first vehicle we see is the cheapest automatically)  (You happen to know the prices in your array, but write this so it would work no matter what prices they had!)

The cheapest vehicle is at index 6, a compact which costs $54.33 per day.