Hi,
I've though that the problem with this task was that I had implemented it by using System.currentTimeMillis() which was not validated. After reading the help section I have modified it tby using the Date and again it didn'tt pass the validation. Now I've no idea was wrong.
Thanks in advance for any advice.
package com.codegym.task.task16.task1605;
/*
Let's talk music
*/
import java.util.Date;
public class Solution {
public static int delay = 1000;
public static void main(String[] args) {
Thread violin = new Thread(new Violin());
violin.start();
}
public static void sleepNSeconds(int n) {
try {
Thread.sleep(n * delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface MusicalInstrument extends Runnable {
Date startPlaying();
Date stopPlaying();
}
public static class Violin implements MusicalInstrument {
//private String owner;
/*
public Violin(String owner) {
this.owner = owner;
}
*/
public Date startPlaying() {
//System.out.println(this.owner + " is starting to play");
return new Date();
}
public Date stopPlaying() {
//System.out.println(this.owner + " is stopping playing");
return new Date();
}
@Override
public void run() {
long startPlaying = startPlaying().getTime();
sleepNSeconds(1);
long stopPlaying = stopPlaying().getTime();
System.out.println("Played for " + (stopPlaying-startPlaying) + " ms");
}
}
}
a task for the thread.Use the MusicalInstrument interface and 2 - Implement the required method in the Violin thread. The implementation should be as follows: 2.1. Determine the time when you start playing - the startPlaying() method. 2.2. Wait 1 second - the sleepNSeconds(int n) method, where n is the number of seconds. 2.3. Determine the time when you stop playing - the stopPlaying() method. 2.4. Display how long you played the instrument. Use the methods in Items 2.1 and 2.3. In the above code WAAAY more than those two conditions are being done and all that extra is causing failure.