Exercise 5

The exercise week 5, we will work with the following tasks.

      
  1. To slide an array one step to the left means to remove the first element and to move all the other elements to the left with one space. At the end the initial first element is put on the empty spot on the far right side.
        
    1. Define a static method
      public static void shiftLeft1(int[] a)
      
      which shifts the array a with one step to the left.
    2. Define a static method
      public static int[] shiftLeft2(int[] a)
      
      which also shifts a one step to the left but it does not change the argument itself. Instead it creates and returns a new array which is the result of the operation.
    3. How can we use the above two methods in a main method? Write a main that creates two small arrays and invokes both methods.
  2. Questions 1 and 2 from the exam in January 2012.
  3. Many computer games maintain a list of the best results achieved, a high score list. This is a pair consisting of a name of a player, and a score (a positive integer). The class for results should therefore implement the interface:
    public interface Result {
         public String getPlayer();
         public int getScore();
    }
    
        
    1. Write a class with an appropriate constructor that implements this interface.   
    2. Write a class HighScoreList that can store the best results in a game. The class constructor should have an integer parameter that specifies the number of elements to be saved. An instance that is created with argument 10 will thus store maximum 10 results. After the first ten calls the method add should remove the lowest scoring player before adding the new one. In this way only the top ten results are saved.   

      In addition to the constructor, the class offers the following methods:   

      • void add(Result res), which adds a new    results (or better said, adding the new result if it's in the top ten).     
      • int getNrResults(), which returns the number of stored results.     
      • Result getResult(int i) which returns the ith best result, i.e. getResult(0) returns the best result and so on. (Here of course we start counting from 0 instead of from 1. getResult(1) gives the second best result.)   

      Hint: Use an array of results and an integer variable that keeps how much of the array is filled in. Constructors and all methods except add should then be easy to define.

      The add is a lot trickier. A few tips: Hold the results sorted in the array. Upon insertion of a new result (in add), check first if the array is full and if so, which elements do not fit. Start searching from the bottom of the array until the right place for the new result is found.

      Note that we do not have much to gain by using a list from the Java Collections Framework, since add is so special. Using an array and an integer that specifies the number of elements currently in the list is probably just as simple.

    3. Write a simple test program that generates for example 100 random results (let "names" to be just strings like "P1" "P2", "P3", etc.) and put them in a high-score list. Print the list.