CIS 542 - Spring 2011

Lab Assignment #1

Thurs, Mar 31 and Tues, Apr 5

Introduction
In this assignment, you will implement some simple algorithms in C and invoke them from Java using JNI. You will then compare the performance of the native code and the Java code, and investigate tuning parameters that affect performance.

You may work with ONE other student on this assignment, but you must each submit your own writeup, as described below.

If you need help, ask a member of the instruction staff, or consult one of these online resources:


Part 1 (10 points)
BubbleSort.java is a program that takes an int called "SIZE" as command-line input, creates a double array of that size, and then randomly populates it with data. It then uses a method "bubbleSortJ" to use an O(n^2) algorithm to sort the elements of the array.

Modify BubbleSort.java and implement a JNI method "bubbleSortC" that takes an array of doubles and sorts them using bubble sort. You do NOT have to return the array at the end. Please name the files "BubbleSort.h" and "BubbleSort.c", and test your code to make sure it works correctly.

Use System.nanoTime to add some timing code to BubbleSort.java to determine how long it takes for the bubbleSortJ and bubbleSortC methods to run (note: you do NOT have to include the time it takes to load the C library), then answer the following questions:

Part 2 (20 points)
AverageAge.java is a program that takes an int called "SIZE" as command-line input, creates an array of Person objects of that size, and then populates it with Person objects that have randomly generated ages. It then uses a method "averageJ" to determine the average age of Person objects in the array.

Modify AverageAge.java and implement a JNI method "averageC" that takes an array of Person objects and calculates their average age. Please name the files "AverageAge.h" and "AverageAge.c", and test your code to make sure it works correctly.

Use System.nanoTime to add some timing code to AverageAge.java to determine how long it takes for the averageJ and averageC methods to run (note: you do NOT have to include the time it takes to load the C library), then answer the following questions:

Extra Credit! (15 points)
Repeat Part 1 from above but run the code on an actual Android device (not the emulator), and measure the performance of the Java and JNI versions of bubble sort. You will need to create an Activity class to invoke the functions, and follow the instructions on the Android developer's site and/or this tutorial to get it working. Submit your entire Eclipse project, along with your .c and .h files, and a writeup explaining your observations.


Submission
It is expected that you will complete this assignment during the lab period. However, if you need extra time, you may submit the assignment before 11:59pm on Tuesday, April 5. After that, late submissions will be subject to a 10% per day deduction.

For this lab assignment, you should submit a .java file, a .c file, and a .h file for both Parts 1 and 2 (six source code files in total). You should also submit a plain-text or PDF writeup answering the questions for each of those parts (you can combine the writeups for each part into a single document).

Although it is okay to work with one other student to develop the source code and discuss the solutions, you must each do your own writeups separately, indicating the student with whom you worked, if applicable.

Each student, regardless of whether he or she is working alone or with someone else, must submit the source code and a write up via Blackboard. See the general instructions on the Course Overview page.

Please be sure that you are only submitting one zip or tar file that includes your source code and your writeup.