Source Code due: Friday, December 5th by 11:59PM
Syntax checking- if it does not run without errors or warnings, it will not be graded
Program Correctness |
75 | |
Gets data from user | ||
Classes are defined correctly | ||
stopWatch class method start correct | ||
stopWatch class method stop correct | ||
stopWatch class method getTimeElapsed correct | ||
randomArray class constructor correct | ||
randomArray class randomize method correct | ||
randomArray class BubbleSort method correct | ||
randomArray class ArraySort method correct | ||
randomArray class isSorted method correct | Program Design | 15 |
Individual design /15 | ||
Design and source code are NOT the same thing (10 point penalty!) | ||
Style and documentation |
10 | |
Prolog Comment with Name, Section, Email, Date, Purpose | ||
Meaningful Variable Names | ||
Documentation of Code | ||
Source code Indentation/ formatting/ whitespace |
||
Total | 100 | |
Late Penalty |
I have the authority to deduct points for other items which do not follow the program specifications, as given in the assignment page, or the documentation or other standards stated on the class web page.
Educational Goals: The educational goals of this program are that the student should get practice with:
Problem Description:
Many problems in computer science can be solved using different algorithms, some more efficient than others. Such is the case with array sorting algorithms. In class, we discussed the bubble sort algorithm, named due to the fact that it compares adjacent numbers (or bubbles) and swaps them if not in order. Programmers want to minimize algorithm execution time (in other words, make them faster). In order to measure speed, one can run the algorithm on a given problem and measure the amount of time the algorithm took to solve it.
In this program, you will use object oriented programming techniques to design a stop watch class that will be used to measure the speed of two sorting algorithms: bubble sort (that you have to implement) and Java Standard Library's Arrays.sort() method.
You have to design the following two classes:
The stopWatch class has two private data fields: startTime and stopTime of type long. This class has a no-arg constructor that initializes startTime and stopTime to 0. It also has three methods:
The randomArray class has one private data field, an array of integers. The constructor has one parameter, an integer specifying the size of the array. The constructor initializes the array and calls the method randomize() (discussed below). The randomArray class has the following methods:
The main class (program5) is given and you CANNOT change it.
public class program5 { public static void main(String[] args) { Scanner s = new Scanner(System.in); // declare and instantiate scanner object System.out.print("Please enter number of elements in array: "); // prompt user for nr of elements int nrElements = s.nextInt(); System.out.println("Now creating the random array object..."); randomArray r = new randomArray(nrElements); // declare and instantiate randomArray object System.out.println("The return value of RandomArray's .isSorted() method now is: " + r.isSorted()); System.out.println("Now sorting using bubble sort..."); long bsTime = r.BubbleSort(); // run BubbleSort algorithm System.out.println("The return value of RandomArray's .isSorted() method now is: " + r.isSorted()); System.out.println("Bubble sort algorithm took: " + bsTime + " milliseconds to complete"); System.out.println("Randomizing array..."); r.randomize(); System.out.println("The return value of RandomArray's .isSorted() method now is: " + r.isSorted()); System.out.println("Now sorting using Java Standard Libraries .ArraySort() method ..."); long asTime = r.ArraySort(); // run Java's ArraySort algorithm System.out.println("The return value of RandomArray's .isSorted() method now is: " + r.isSorted()); System.out.println("Arrays.sort algorithm took: " + asTime + " milliseconds to complete"); } }
Sample interaction with the program (please note that your execution times may vary due to computer hardware and other processes open):
Sample run 1: Please enter number of elements in array: 1000 Now creating the random array object... The return value of RandomArray's .isSorted() method now is: false Now sorting using bubble sort... The return value of RandomArray's .isSorted() method now is: true Bubble sort algorithm took: 9 milliseconds to complete Randomizing array... The return value of RandomArray's .isSorted() method now is: false Now sorting using Java Standard Libraries .ArraySort() method ... The return value of RandomArray's .isSorted() method now is: true Arrays.sort algorithm took: 1 milliseconds to complete Sample run 2: Please enter number of elements in array: 10000 Now creating the random array object... The return value of RandomArray's .isSorted() method now is: false Now sorting using bubble sort... The return value of RandomArray's .isSorted() method now is: true Bubble sort algorithm took: 128 milliseconds to complete Randomizing array... The return value of RandomArray's .isSorted() method now is: false Now sorting using Java Standard Libraries .ArraySort() method ... The return value of RandomArray's .isSorted() method now is: true Arrays.sort algorithm took: 3 milliseconds to complete Sample run 3: Please enter number of elements in array: 100000 Now creating the random array object... The return value of RandomArray's .isSorted() method now is: false Now sorting using bubble sort... The return value of RandomArray's .isSorted() method now is: true Bubble sort algorithm took: 16468 milliseconds to complete Randomizing array... The return value of RandomArray's .isSorted() method now is: false Now sorting using Java Standard Libraries .ArraySort() method ... The return value of RandomArray's .isSorted() method now is: true Arrays.sort algorithm took: 10 milliseconds to complete
Notes:
Implement the design
Write a Java program to implement your design.
Make a copy of the Java file you have that has
the design in it and write your Java code between
the commented lines of the design.
Make sure you eliminate any syntax and semantics errors.
Here is where test cases come in handy!
Submit your individual source code with the link here.
Choose the menu choices of "Code" and "Program 4".
This is due by Friday, November 14th, 11:59PM.
There are several specifications about how your program should be written.
Please read the documentation standard on the class web page. As you can see from looking at the grading criteria, we will be looking to see how you meet these standards. Note particularly that we require a prolog!
Remember that the program must run with NO errors or warnings. If it does not, it will not be graded!