Clock class code: /** * Clock class */ public class Clock{ private int hours,min
ID: 3724263 • Letter: C
Question
Clock class code:
/**
* Clock class
*/
public class Clock{
private int hours,minutes,seconds;
public Clock(){
hours = minutes = seconds = 0;
}
public Clock(int h, int m, int s){
validate(h,m,s);
}
public void setHours(int h){
validate(h,minutes,seconds);
}
public void setMinutes(int m){
validate(hours,m,seconds);
}
public void setSeconds(int s){
validate(hours,minutes,s);
}
public int getHours(){ return hours;}
public int getMinutes(){ return minutes;}
public int getSeconds(){ return seconds;}
public void tick(){
seconds++;
if (seconds > 59){
seconds=0;
minutes++;
}
if (minutes > 59){
minutes=0;
hours++;
}
if (hours > 23)
hours = 0;
}
public boolean equals(Object obj){
if (obj instanceof Clock){
Clock c = (Clock)obj;
if (this.getHours()==c.getHours() && this.getMinutes()==c.getMinutes() &&
this.getSeconds()==c.getSeconds())
return true;
}
return false;
}
public String toString(){
return hours+":"+minutes+":"+seconds;
}
private void validate(int h, int m, int s){
if (h>=0 && h<=23)
hours = h;
if(m>=0 && m<=59)
minutes = m;
if (s>=0 && s<=59)
seconds = s;
}
}
Please show work and final display output.
Objective: Interfaces and Algorithm Performance 1. Consider the Clock class, demonstrated previously in lecture (and available on D2L) that allows the representation of a time with hours(0-23), minutes(0-59) and seconds(0-59) components that are provided through an appropriate constructor Have this class implement the Comparable interface. When comparing Clock objects, you must use chronological order 2. Create a Driver class that implements the following methods with the headers below: ublic static void insertionSort (Comparablel c) public static void selectionSort (Comparable[ c) 3. Have the main method in your Driver class create an array of Clock objects that has a "large" size. You should randomly generate the three components for each Clock object but reject those combinations where any component is not within its specified range 4. Then have the main method in your Driver class sort this array of Clock objects using both the insertionSort and selectionSort methods you wrote, timing how long each run takes. You will have to insert appropriate timing code into your program (use System.nanoTime() or System.currentTimeMillis()) and you will also have to experiment with the "large" array size to see any meaningful results when sorting. 5. Specifically, run your code on the teaching lab computers and answer the following a. What is the largest array size that can be sorted in less than 2 seconds with b. What is the largest array size that can be sorted in less than 2 seconds with questions, putting your answers in COMMENTS at the top of your Driver class code insertion sort? selection sort?Explanation / Answer
//TestClock.ajva
import java.util.Random;
public class TestClock {
public static void main(String[] args) {
//set size
int size=10000;
Clock [] clock=new Clock[size];
//call fillClock to fill clock object with random hours,minutes and seconds
fillClock(clock);
//calling insertionSort method
long timetaken=insertionSort(clock);
System.out.printf("%-15s%-15s%-15s ","Sorting","Size","Time(ms)");
System.out.printf("%-15s%-15d%-15d ","InsertionSort",size,timetaken);
fillClock(clock);
//calling selectionSort method
timetaken=selectionSort(clock);
System.out.printf("%-15s%-15d%-15d ","SelectionSort",size,timetaken);
}
/**Method to fill clock array with random values for
* hours,minutes and seconds*/
public static void fillClock(Clock clock[]){
Random r=new Random();
for (int i = 0; i < clock.length; i++) {
int sec=r.nextInt(60);
int min=r.nextInt(60);
int hrs=r.nextInt(60);
clock[i]=new Clock(hrs, min, sec);
}
}
/*Function to sort array using insertion sort*/
public static long selectionSort(Comparable[] c)
{
int n = c.length;
long start=System.currentTimeMillis();
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (c[j].compareTo(c[min_idx])<0)
min_idx = j;
Comparable temp = c[min_idx];
c[min_idx] = c[i];
c[i] = temp;
}
long end=System.currentTimeMillis();
return (end-start);
}
/**Method insertion sort */
public static long insertionSort(Comparable[] c)
{
int n = c.length;
long start=System.currentTimeMillis();
for (int i=1; i<n; ++i)
{
Comparable key = c[i];
int j = i-1;
while (j>=0 && c[j] .compareTo(key)>0)
{
c[j+1] = c[j];
j = j-1;
}
c[j+1] = key;
}
long end=System.currentTimeMillis();
return (end-start);
}
}
--------------------------------------------------------------------------------------------------------------------------------
/**
* Clock class
*/
//Clock.java
public class Clock implements Comparable<Clock>{
private int hours,minutes,seconds;
public Clock(){
hours = minutes = seconds = 0;
}
public Clock(int h, int m, int s){
validate(h,m,s);
}
public void setHours(int h){
validate(h,minutes,seconds);
}
public void setMinutes(int m){
validate(hours,m,seconds);
}
public void setSeconds(int s){
validate(hours,minutes,s);
}
public int getHours(){ return hours;}
public int getMinutes(){ return minutes;}
public int getSeconds(){ return seconds;}
public void tick(){
seconds++;
if (seconds > 59){
seconds=0;
minutes++;
}
if (minutes > 59){
minutes=0;
hours++;
}
if (hours > 23)
hours = 0;
}
public boolean equals(Object obj){
if (obj instanceof Clock){
Clock c = (Clock)obj;
if (this.getHours()==c.getHours() && this.getMinutes()==c.getMinutes() &&
this.getSeconds()==c.getSeconds())
return true;
}
return false;
}
public String toString(){
return hours+":"+minutes+":"+seconds;
}
private void validate(int h, int m, int s){
if (h>=0 && h<=23)
hours = h;
if(m>=0 && m<=59)
minutes = m;
if (s>=0 && s<=59)
seconds = s;
}
/**Override the compareTo method that sorts by
* hours, then minutes and then seconds*/
public int compareTo(Clock other) {
if (hours > other.hours) {
return 1;
} else if (hours < other.hours) {
return -1;
} else if (minutes > other.minutes) {
return 1;
} else if (minutes < other.minutes) {
return -1;
}
else
return seconds - other.seconds;
}
}
--------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Sorting Size Time(ms)
InsertionSort 10000 367
SelectionSort 10000 936
Note :Conclusion selection sort takes more time than than insertion sort.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.