-
-
Notifications
You must be signed in to change notification settings - Fork 735
Add split-second-stopwatch exercise #2968 #2977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jagdish-15
merged 11 commits into
exercism:main
from
EmmanuelBerkowicz:#2968-Add-split-second-stopwatch-exercise
Jul 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5d8fe4
Add split-second-stopwatch exercise #2968
EmmanuelBerkowicz 99c8273
Add split-second-stopwatch exercise #2968
EmmanuelBerkowicz 321302a
Add split-second-stopwatch exercise #2968 #2977
EmmanuelBerkowicz adffe45
Add split-second-stopwatch exercise #2968 #2977
EmmanuelBerkowicz c344e9b
Add split-second-stopwatch exercise #2968 #2977
EmmanuelBerkowicz ece9405
Add split-second-stopwatch exercise #2968 #2977
EmmanuelBerkowicz cae42f4
Merge branch 'main' into #2968-Add-split-second-stopwatch-exercise
EmmanuelBerkowicz cc1d927
Update exercises/practice/split-second-stopwatch/src/test/java/SplitS…
EmmanuelBerkowicz fc98968
Update exercises/practice/split-second-stopwatch/.meta/src/reference/…
EmmanuelBerkowicz fa452a5
Add split-second-stopwatch exercise #2968 #2977
EmmanuelBerkowicz 0238629
Merge branch 'main' into #2968-Add-split-second-stopwatch-exercise
jagdish-15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
exercises/practice/split-second-stopwatch/.docs/instructions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Instructions | ||
|
||
Your task is to build a stopwatch to keep precise track of lap times. | ||
|
||
The stopwatch uses four commands (start, stop, lap, and reset) to keep track of: | ||
|
||
1. The current lap's tracked time | ||
2. Previously recorded lap times | ||
|
||
What commands can be used depends on which state the stopwatch is in: | ||
|
||
1. Ready: initial state | ||
2. Running: tracking time | ||
3. Stopped: not tracking time | ||
|
||
| Command | Begin state | End state | Effect | | ||
| ------- | ----------- | --------- | -------------------------------------------------------- | | ||
| Start | Ready | Running | Start tracking time | | ||
| Start | Stopped | Running | Resume tracking time | | ||
| Stop | Running | Stopped | Stop tracking time | | ||
| Lap | Running | Running | Add current lap to previous laps, then reset current lap | | ||
| Reset | Stopped | Ready | Reset current lap and clear previous laps | | ||
Check failure on line 22 in exercises/practice/split-second-stopwatch/.docs/instructions.md
|
6 changes: 6 additions & 0 deletions
6
exercises/practice/split-second-stopwatch/.docs/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Introduction | ||
|
||
You've always run for the thrill of it — no schedules, no timers, just the sound of your feet on the pavement. | ||
But now that you've joined a competitive running crew, things are getting serious. | ||
Training sessions are timed to the second, and every split second counts. | ||
To keep pace, you've picked up the _Split-Second Stopwatch_ — a sleek, high-tech gadget that's about to become your new best friend. | ||
Check failure on line 6 in exercises/practice/split-second-stopwatch/.docs/introduction.md
|
22 changes: 22 additions & 0 deletions
22
exercises/practice/split-second-stopwatch/.meta/config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"authors": [ | ||
"Jagdish Prajapati, Kah Goh & Emmanuel Berkowicz" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/SplitSecondStopwatch.java" | ||
], | ||
"test": [ | ||
"src/test/java/SplitSecondStopwatch.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/SplitSecondStopwatch.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Time to create a new and improved split second stopwatch using your friendly neighbourhood OOP language!.", | ||
"source": "Emmanuel Berkowicz", | ||
"source_url": "https://github.com/exercism/problem-specifications/tree/main/exercises/split-second-stopwatch" | ||
} |
123 changes: 123 additions & 0 deletions
123
exercises/practice/split-second-stopwatch/.meta/src/reference/java/SplitSecondStopwatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SplitSecondStopwatch { | ||
|
||
/** | ||
* A split-second stopwatch that tracks elapsed time with lap functionality. | ||
* Supports start, stop, reset, and lap operations with precise time tracking. | ||
* Times are formatted in HH:MM:SS format with two-digit precision. | ||
* | ||
* @see <a href="https://github.com/exercism/problem-specifications/tree/main/exercises/split-second-stopwatch"> | ||
* Problem Specifications | ||
* </a> | ||
*/ | ||
|
||
private enum State { READY, RUNNING, STOPPED } | ||
|
||
private State state; | ||
private long totalElapsed; | ||
private long currentLapStart; | ||
private long lastStopTime; | ||
private List<String> previousLaps; | ||
private long mockTime; | ||
|
||
public SplitSecondStopwatch() { | ||
this.state = State.READY; | ||
this.totalElapsed = 0; | ||
this.currentLapStart = 0; | ||
this.lastStopTime = 0; | ||
this.previousLaps = new ArrayList<>(); | ||
this.mockTime = 0; | ||
} | ||
|
||
public void start() { | ||
if (state == State.RUNNING) { | ||
throw new IllegalStateException("cannot start an already running stopwatch"); | ||
} | ||
|
||
if (state == State.READY) { | ||
currentLapStart = mockTime; | ||
} else if (state == State.STOPPED) { | ||
long pausedDuration = mockTime - lastStopTime; | ||
currentLapStart += pausedDuration; | ||
} | ||
|
||
state = State.RUNNING; | ||
} | ||
|
||
public void stop() { | ||
if (state != State.RUNNING) { | ||
throw new IllegalStateException("cannot stop a stopwatch that is not running"); | ||
} | ||
|
||
lastStopTime = mockTime; | ||
totalElapsed += mockTime - currentLapStart; | ||
state = State.STOPPED; | ||
} | ||
|
||
public void reset() { | ||
if (state != State.STOPPED) { | ||
throw new IllegalStateException("cannot reset a stopwatch that is not stopped"); | ||
} | ||
|
||
state = State.READY; | ||
totalElapsed = 0; | ||
currentLapStart = 0; | ||
lastStopTime = 0; | ||
previousLaps.clear(); | ||
} | ||
|
||
public void lap() { | ||
if (state != State.RUNNING) { | ||
throw new IllegalStateException("cannot lap a stopwatch that is not running"); | ||
} | ||
|
||
long currentLapTime = mockTime - currentLapStart; | ||
previousLaps.add(formatTime(currentLapTime)); | ||
currentLapStart = mockTime; | ||
} | ||
|
||
public String state() { | ||
return state.name().toLowerCase(); | ||
} | ||
|
||
public String currentLap() { | ||
if (state == State.RUNNING) { | ||
return formatTime(mockTime - currentLapStart); | ||
} else { | ||
return formatTime(lastStopTime - currentLapStart); | ||
} | ||
} | ||
|
||
public String total() { | ||
if (state == State.RUNNING) { | ||
return formatTime(totalElapsed + (mockTime - currentLapStart)); | ||
} else { | ||
return formatTime(totalElapsed); | ||
} | ||
} | ||
|
||
public List<String> previousLaps() { | ||
return new ArrayList<>(previousLaps); | ||
} | ||
|
||
public void advanceTime(String timeString) { | ||
String[] parts = timeString.split(":"); | ||
long hours = Long.parseLong(parts[0]); | ||
long minutes = Long.parseLong(parts[1]); | ||
long seconds = Long.parseLong(parts[2]); | ||
|
||
long milliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000; | ||
mockTime += milliseconds; | ||
} | ||
|
||
private String formatTime(long milliseconds) { | ||
long totalSeconds = milliseconds / 1000; | ||
long hours = totalSeconds / 3600; | ||
long minutes = (totalSeconds % 3600) / 60; | ||
long seconds = totalSeconds % 60; | ||
|
||
return String.format("%02d:%02d:%02d", hours, minutes, seconds); | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id "java" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation platform("org.junit:junit-bom:5.10.0") | ||
testImplementation "org.junit.jupiter:junit-jupiter" | ||
testImplementation "org.assertj:assertj-core:3.25.1" | ||
|
||
testRuntimeOnly "org.junit.platform:junit-platform-launcher" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
|
||
testLogging { | ||
exceptionFormat = "full" | ||
showStandardStreams = true | ||
events = ["passed", "failed", "skipped"] | ||
} | ||
} |
Binary file added
BIN
+42.4 KB
exercises/practice/split-second-stopwatch/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
exercises/practice/split-second-stopwatch/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.