RHS   Allan   BasicProgramming => Exercises => ToursiticRental3


 
Touristic Rental 3

In this exercise we're going to perform some calculations on an array of Bicycle's (a mixture of CityBike's and SportsBike's).

The following application creates an array, coll, with room for 10 Bicycle's. It initialises the array using the method init().

Then it prints the result from 5 methods - you are to implement these methods.

In the following you can assume that ALL Bicycles have been rented out for 4 hours (you need to pass the number of hours to the method that calculates the rent for the individual bicycles)

The first method should calculate the total rent for the CityBike's.
The second method should calculate the total rent for the SportsBike's.
The third method should calculate the total rent for all the Bicycle's.
The fourth method should calculate the number of bicycles that is equipped with a baby-seat.
The fifth method should calculate the average number of gears for the SportsBike's.

Below is a template for the application - only the body for the 5 methods are missing. You can download the template here: TouristicRental3.java

  import java.util.*;

public class TouristicRental3 {
  private static Bicycle[] coll = new Bicycle[10];
  //in real life you'd probably use ArrayList or other List-type

  public static void main(String[] args) {
    TouristicRental3 app = new TouristicRental3();

    app.init();
    System.out.println(
      app.cityRent(coll) + "\n" +
      app.sportsRent(coll) + "\n" +
      app.totalRent(coll) + "\n" +
      app.noWithSeat(coll) + "\n" +
      app.averageGears(coll)
    );
  }//main(String[])

  public double cityRent(Bicycle[] list) {
  }

  public double sportsRent(Bicycle[] list) {
  }

  public double totalRent(Bicycle[] list) {
  }

  public int noWithSeat(Bicycle[] list) {
  }

  public double averageGears(Bicycle[] list) {
  }

  public static void init() {
    coll[0] = new CityBike(1, "CB", 4.0, false);
    coll[1] = new SportsBike(1, "SB", 2.0, 3);
    coll[2] = new CityBike(2, "CB", 7.0, true);
    coll[3] = new SportsBike(7, "SB", 6.0, 3);
    coll[4] = new SportsBike(8, "SB", 12.2, 7);
    coll[5] = new CityBike(3, "CB", 5.5, false);
    coll[6] = new CityBike(4, "CB", 4.9, false);
    coll[7] = new CityBike(5, "CB", 7.4, true);
    coll[8] = new SportsBike(9, "SB", 11.2, 6);
    coll[9] = new CityBike(6, "CB", 14.1, true);
  }//init()
}//TouristicRental3

   

Question 5
Implement the 5 methods.

Question 6
What if we declared an array more : private static SportsBike[] sports = new SportsBike[2];
Then we put two objects of the type SportsBike in the array.

Would we be able to activate the 5 methods using the array sports as argument (e.g. double x = totalRent(sports); )?
Answer before you adapt the code and try. Then adapt and try. Did you give the correct answer?


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04