Need Help !!

0 mahmood attia · March 22, 2015
I have an assignment which states :

Design a new class called “CodeTimer”.  This class should contain the following data fields:
A private long value named startTime
A private long value named endTime
Get methods for both startTime and endTime (i.e. getStartTime() and getEndTime()
A no argument constructor that initializes startTime with the current time (hint: use System.currentTimeMillis() )
A method named start() that resets that 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 between startTime and endTime


Next, create a new class called “CodeTimerTest”.  
This class will be used to compare the performance of the Selection Sort algorithm to the built in Java array sort algorithm.  
Here’s how your program should work:
Prompt the user to enter in a number of items to sort 
(an integer)
Create two arrays that are of the specified size.
  Fill both arrays with random numbers between 0 and
 the number specified (i.e. if the user enters 1000 you
 should create two arrays with 1000 elements each. 
 Each array should contain a random number between 0 and 1000) 
 — note that for comparison purposes each array should contain
 the SAME random number in the same position (i.e. if you put 
the random # 56 into element #1 in one array you should also
 put 56 in the second array at the same position)
Create an instance of your CodeTimer class and measure how long 
it takes to create the arrays.  
Report this to the user.

Next, use your CodeTimer instance to track how long it takes to run 
the selection sort algorithm on one of the arrays. You will need
 to write this algorithm 
(http://www.algolist.net/Algorithms/Sorting/Selection_sort).  
Report this to the user.

Finally, use your CodeTimer instance to track how long it takes to run 
the built in java.util.Arrays.sort() method on the second array.  
Report this to the user.

Here is a sample running of your program:
Sort Timer!

Enter a number of random items to sort (i.e. 100000): 100000
Took: 0.012 seconds to create arrays
Took: 2.591 seconds to sort array using the Selection Sort algorithm
Took: 0.027 seconds to sort array using the java.util.Arrays.sort() method
And here’s a sample running with a huge # of elements – note how inefficient the Selection Sort algoritm compares to the built in array sort algoritm!
Sort Timer!

Enter a number of random items to sort (i.e. 100000): 900000
Took: 0.042 seconds to create arrays
Took: 275.55 seconds to sort array using the Selection Sort algorithm
Took: 0.117 seconds to sort array using the java.util.Arrays.sort

Im stuck on how to measure the time it takes to create the arrays.  
this is the code I got to 

public class CodeTimer {

private long startTime;
private long endTime;

public long getStartTime() {
return startTime;
}

public long getEndTime() {
return endTime;
}

public CodeTimer() {
startTime = System.currentTimeMillis();

}
public void start(){
startTime = System.currentTimeMillis();
 
}
public void stop(){
endTime = System.currentTimeMillis();
}
public long getelapsedTime(){

return  (endTime - startTime) ; 

}

}



import java.util.Scanner; 
public class CodeTimerTest {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number of random items to sort (i.e. 100000):");
int x = scan.nextInt();
int array  [] = new int [x];
 
int array2  [] = new int [x];
 
CodeTimer ct = new CodeTimer ();




}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 22, 2015
Start the timer after you scan the next int. End the timer after the second array is created. I would change the implementation slighlty by removing the initial seed. Just think about it. You provide initial overhead when you create the timer. Additionally, it doesn't make sense to use more than one timer. 

Record the time before you made the call. Then record the time after it finishes the function call. Don't think too hard. It's just a timer. 

Actually, are you also supposed to measure the time it took to populate the arrays with random values as part of the creation time?

I.e.

int x = scan.nextInt();
CodeTimer ct=new CodeTimer();
ct.start();
int[] array = new int [x];
int[] array2 = new int [x];
ct.end();
long arrayCreationTime=ct.getElapsedTime();
  • 1

Java / Android Development

106,927 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator