C/C++ C++: Creating a class for a stopwatch

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Class
AI Thread Summary
The discussion revolves around the creation of a simple stopwatch app for Windows Phone, initiated by the realization that the device lacks a built-in stopwatch feature. The developer outlines a plan to implement the app using a class structure, with a focus on straightforward functionality rather than complex algorithms. Key features of the stopwatch include two buttons for starting/stopping and recording laps, with the app designed to handle three states: unstarted, running, and paused. The developer proposes using a vector to store lap times and seeks advice on programming intuition and best practices. Feedback from other participants emphasizes the importance of intuitive API design, suggesting that the public functions should be named to reflect their real-world counterparts for better usability. Additionally, there are recommendations regarding the use of platform-specific timing libraries, such as DispatchTimer, to ensure high-resolution timing. The discussion highlights the balance between implementing basic functionality and adhering to good programming practices for user experience.
Jamin2112
Messages
973
Reaction score
12
Today I wanted to use my Windows phone to time myself doing a workout. After realizing that my phone doesn't have a stopwatch, I thought, "Oh, I'll make one." I've never made a Windows phone app before, but Microsoft has some documentation that should guide me later: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681687(v=vs.105).aspx.

I'd like to do this as correctly as possible. There will be no fancy algorithms involved, which should make it easier to focus on straightforward implementation.

I'm sure you've all used a stopwatch. A stopwatch has two buttons, a left one and a right one. You push the right one to first get the clock running.

While the clock is running, if you push the right button, you pause the clock, and if you push the left button, you put the stopwatch into the next "lap", i.e. it saves the the time up until now in a previous lap. The number of laps is indefinite.

While the clock is paused, if you push the right button, you start the clock again, and if you push the left button, you've made the current lap your final lap and officially ended your workout/race/whatever.

Now, here's what my programming intuition tells me:

  • This should be a class. A stopwatch, as I've described it, is an object that gets instantiated. So the first thing I should do is write class Stopwatch in Notepad.
    [*]A stopwatch object is in one of 3 states: it's either unstarted, timing a lap or paused on a lap. So I should have a private variable clock_mode cur_mode; where clock_mode is an enumerator enum clock_mode {unstarted, running, paused};
    [*]There should be 2 and only 2 public functions: void start() and void stop(). The inner workings of this object will subsequently be called by those functions.
    [*]The laps need to be stored in a linked list or similar structure. Each new lap gets pushed back onto the structure. This calls for a vector, according to Bjarne Stroustrop: .
    [*]I will be needing to bring in <ctime.h> or whichever library is the one for time.


Any thoughts so far? I need help gaining a better programming intuition.
 
Last edited by a moderator:
Technology news on Phys.org
Hopefully this shows you what my gameplan looks like:

Code:
#include <vector>

class Stopwatch
{
    private:
    enum clock_mode {unstarted, running, paused};
    clock_mode cur_mode; 
    struct lap {int lap_num; double time;};
    int lap_count;
    std::vector<lap> laps_vec;

    public: 
    Stopwatch();
    ~Stopwatch();
    void right_button();
    void left_button();
};

Stopwatch::Stopwatch()
{
    cur_mode = unstarted;
    lap_count = 0;
}

Stopwatch::~Stopwatch()
{
    // trivial destructor
}

void Stopwatch::right_button()
{
     switch (cur_mode)
     {
        case unstarted:
            // start lap 1
            ;
        case running:
            // pause
            ;
        case paused:
            // start running again
            ;

     }
}

void Stopwatch::left_button()
{
    switch (cur_mode)
    {
        case running:
            // advance to the next lap
            ;
        case paused:
            // reset
            ;
    {
}
 
there is a stopwatch class in he .net framewrok. c++ is on the .net framework. system.diagnostics.stopwatch

https://code.google.com/p/cpp-stopwatch/

not to be opprobrious but isn't asking us to code for you kinda against the rules?
 
Superposed_Cat said:
not to be opprobrious but isn't asking us to code for you kinda against the rules?

When did I ask you to code for me?

And what rules are you talking about?
 
What is the difference between "unstarted" and "paused"? Clue: the difference is the value of an unrelated elapsed time counter. So, you need only 2 states in your enum.

Your public functions (API) should represent the buttons. That is what a real stopwatch exposes. So your API should expose 2 functions: leftbutton and rightbutton (usually named start/stop and lap on a real device). Otherwise, your API will be unintuitive and anyone using your code will be sad, and whine on proggit about poor API design in modern software.

I don't know what time library you need for a windows phone. Getting time right is one of those platform-specific things and I don't use the Windows platforms. Whatever you pick, you should ensure it has a high resolution clock. You might time the task directly, or ask the operating system to time it for you. I would recommend the latter.
 
Use DispatchTimer, it has an event handler named Tick.

http://developer.nokia.com/community/wiki/Implement_timers_in_Windows_Phone
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top