Schedule‎ > ‎

Practice Final Answers

Answers to Practice Final

clock.h

/**
  CS-11M
  Name: clock.h
  Purpose: Provides the interface for the Clock Class

  @author Sharon Strelitz
  @version 1.0  5/3/2017
*/
#ifndef CLOCK_H
#define CLOCK_H

#include "Arduino.h"
#include <ArduinoSTL.h>
using namespace std;

class Clock {
  public:
    Clock(); // starts at midnight
    Clock(int hours, int minutes, int seconds);
    // Set seconds, minutes, hours as elements 0, 1, 2 respectively.
    void setTime(int time[]);
    int getHours() const;
    int getMinutes() const;
    int getSeconds() const;
    // Return current hours, minutes, seconds as
    // elements 0, 1, 2 respectively.
    vector<int> getTimeVector() const;
    String toString() const;
    void tick();

  private:
    int _hours;
    int _minutes;
    int _seconds;
};
#endif
clock.cpp
/**
  CS-11M
  Name: clock.cpp
  Purpose: Provides the implementation of the Clock Class

  @author Sharon Strelitz
  @version 1.0   5/3/2017
*/
#include "clock.h"

Clock::Clock() {
  _hours = 0;
  _minutes = 0;
  _seconds = 0;
}
Clock::Clock(int hours, int minutes, int seconds) {
  int t[] = { seconds, minutes, hours };
  setTime(t);
}
void Clock::setTime(int time[]) {
  _hours = time[2];
  _minutes = time[1];
  _seconds = time[0] - 1;
  tick();
}
int Clock::getHours() const {
  return _hours;
}
int Clock::getMinutes() const {
  return _minutes;
}
int Clock::getSeconds() const {
  return _seconds;
}
vector<int> Clock::getTimeVector() const {
  vector<int> tv;
  tv.push_back(_seconds);
  tv.push_back(_minutes);
  tv.push_back(_hours);
  //vector<int> tv(3);
  //tv[0] = _seconds;
  //tv[1] = _minutes;
  //tv[2] = _hours;
  return tv;
}
String Clock::toString() const {
  String timeStr = String("") + getHours() + ":";
  int min = getMinutes();
  if (min < 10) timeStr += "0";
  timeStr += min;
  timeStr += ":";
  int sec = getSeconds();
  if (sec < 10) timeStr += "0";
  timeStr += sec;
  return timeStr;
}
void Clock::tick() {
  _seconds++;
  if (_seconds >= 60) {
    _seconds = 0;
    _minutes++;
  }
  if (_minutes >= 60) {
    _minutes = 0;
    _hours++;
  }
  if (_hours >= 24) {
    _hours = 0;
  }
}
practice.ino
#include <ArduinoSTL.h>
#include "Clock.h"
using namespace std;

Clock clk;

void setup() {
  Serial.begin(9600);
  clk = setClock();
 
  vector<int> myVector = clk.getTimeVector();
  cout << "myVector.size() is " << myVector.size() << endl;
  cout << "time vector is: ";
  for (int i = 0; i < myVector.size(); i++)  {
    cout << myVector[i] << " ";
  }
  cout << endl;
 
  cout << "Enter string you want reversed:  ";
  String myString;
  while (!Serial.available()); //wait till string available
  myString = Serial.readString();
  Serial.println(myString);
  showBackwards(myString);
  cout << endl;
 
}
void loop() {
  clk.tick();
  Serial.println(clk.toString());
  delay(1000);
}
/**
  setClock:  sets the time of the clock from user input
  @param no parameters
  @return Clock object
*/
Clock setClock() {
  Serial.println("Enter hours, minutes and seconds separated by spaces");
  int hours = 0, min = 0, sec = 0;
  cin >> hours >> min >> sec;
  while (Serial.available())  {  //put this after your cin statement
    Serial.read();
  }
  Clock clk(hours, min, sec);
  return clk;
}
/**
    Displays the string backwards.

    @param str The string to display in reverse.
    @return void
*/
void showBackwards(String str)  {
  if (str.length() == 0)  {
    cout << "done" << endl;
  }  else  {
    String last = str.substring(0, 1);
    String newString = str.substring(1, str.length());
    showBackwards(newString);
    Serial.print(last);
  }
}


Comments