Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design a public class named StopWatch. The class contains: long data fields startTime and endTime. A no-argument constructor that initializes startTime and endTime with the

Design a public class named StopWatch. The class contains: long data fields startTime and endTime. A no-argument constructor that initializes startTime and endTime with the current time. A method named start() that resets the startTime to the current time. A method named stop() that sets the endTime to the current time. A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds (endTime - startTime). Use the System Class method currentTimeMillis() to obtain the current time. NOTE: Please define the public StopWatch class in it's own Java file separate from the test program as shown in Lesson #6. Design a test program to test the StopWatch class. To test its operation, a large array of 100,000 double numbers in the range of 0 to 100,000 is generated using the random method from the Math class. Then, using a StopWatch object, the time is measured to sort this array. To make it more interesting, the performance of three sorting algorithms will be compared: 1. The bubble sort algorithm 2. The selection sort algorithm 3. The dual-pivot quicksort algorithm (this is the sort algorithm used in the sort method of the Arrays class as demonstrated in class). You should discover that the bubble sort algorithm is very slow (about 20 seconds), the selection sort algorithm is about 10 times faster, and the dual-pivot quicksort algorithm is the fastest by a significant margin. You will have to implement the bubble sort and selection sort algorithm yourself (search on-line for the algorithms - this will test your resourcefulness). Here are the suggested method headers for both of these algorithms: public static void bubbleSort (double[] list) - Sort the list array in ascending order public static void selectionSort (double[] list) - Sort the list array in ascending order The dual-pivot quicksort algorithm is already implemented for you through the sort method in the Arrays class as was demonstrated in class.

NOTE: when you create the unsorted array of 100,000 double numbers, you will have to make two more copies of that array before you test any sorting. These copies are required because the sorting algorithms will sort the list array and so you cannot use it again as it is now sorted. Also, you want to test all three algorithms with the same unsorted data you generated to have a fair comparison. You can use the copyOf method from the Arrays class to perform this copy. Remember: For each problem, include several test runs of the program to show successful runs of your program, with both valid and invalid data (to show that you are checking for invalid data, if applicable) Please ensure the program is well designed and follows accepted style guidelines (e.g. variable naming, indentation, spacing). Please ensure the program is well documented, including the overall purpose of the program and documenting all the major sections of the code. NEW: Please ensure your output includes a line that identifies you. For example: System.out.println("Written by Warren Edwards, INFO 2312 S12");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the implementation of the StopWatch class and the test program in Java StopWatchjava public class StopWatch private long startTime private long ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Java Programming And Data Structures Comprehensive Version

Authors: Y. Daniel Liang

12th Edition

0136520235, 978-0136520238

More Books

Students also viewed these Programming questions