Home > IB Computer Science 2016 > Array algorithms framework

Array algorithms framework

September 22, 2014 Leave a comment Go to comments

This exercise will help us to implement some of the algorithms we have been considering for processing arrays. I have provided an skeleton Java class for you to write your code into.

  1. Create a new project in Netbeans called ArrayAlgorithms.
  2. Copy the code from the bottom of this post and paste into over the Netbeans-generated code.
  3. Save.
  4. Read the instructions and the comments carefully.
  5. Create the methods specified.
  6. Test them.

You have three basic methods to implement:

  • printArray()
  • getSum()
  • getMax()

And two more advanced methods to implement:

  • insertOrdered()
  • bubbleSort()

Key ideas to consider during this exercise:

  • Passing parameters to a method
  • Return values from a method
  • Variable scope
  • Preconditions and postconditions
  • Debugging
  • Good programming practice

Check your understanding:

  • The createArray() method has a precondition that m <= n. What if this precondition had not been specified? How would it affect the way you programmed the method?
  • See if  you can find other preconditions that have not been specified. How can you as the programmer deal with situations that may arise?
  • “It is the responsibility of the programmer of the code that calls a method to ensure that the method’s preconditions are met, not the programmer of the method itself.” Discuss.
  • One way that a programmer of a method can try to ensure that its preconditions have been met is by using an assertion statement. Explain what it means to “assert” something, and evaluate the use of assertion statements in computer programs.
  • Explain what is meant in computing by the term “abstraction”. Explain how preconditions and postconditions help programmers to make use of abstraction in their code.

Further work:

  1. Create a method to generate a randomised 2-d array, together with a method to print it.
  2. Write insertionSort() and selectionSort() methods. Use the example on the class corkboard, don’t just Google them.

Skeleton Java class:

package arrayalgorithms;

import java.util.Random;

public class ArrayAlgorithms {

    int[] arry;
    int numElements = 0;

    public static void main(String[] args) {
        // Instantiate your ArrayAlgorithms object here
        ArrayAlgorithms a = new ArrayAlgorithms();

        // Create an array to work with
        a.createArray(120, 200, 9999);

        // Test your methods here

    }

    // This method I have created for you. You can use it to generate an
    // array to work with.
    public void createArray(int m, int n, int max) {
        // Initialises arry with n elements of which m
        // are populated with random integers from 0 to max.
        // PRECONDITIONS: arry is withing scope.
        // PRECONDITIONS: m &lt;= n
        // POSTCONDITIONS: arry points to a new array object created
        // according to the parameters.
        Random r = new Random();
        arry = new int[n];
        for (int i = 0; i &lt; m; i++) {
            arry[i] = r.nextInt(max + 1);
        }
    }

    // BASIC methods to implement:
    
    // For each of these you need to provided the method header, preconditions
    // and postconditions, and the method body.
    // 1. printArray() prints out the array to the standard output stream.
    // 2. getSum() returns the sum of the array contents.
    // 3. getMax() returns the max of the array contents.
    
    
    // More ADVANCED methods for which I have provided the method header
    // together with preconditions and postconditions
    
    public void bubbleSort() {
        // Sorts the array in ascending order.
        // PRECONDITIONS: arry is in scope.
        // POSTCONDITIONS: The array is sorted in ascending order
    }

    public void insertOrdered(int n) {
        // Inserts n into the correct place in the array.
        // PRECONDITION: The array is sorted in ascending order.
        // PRECONDITION: The array is not full.
        // POSTCONDITIONS: n is inserted into the array at the correct position.
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s