Answered Essay: It represents all possible times in a 24-hour period, including hours, minutes and seconds. An i

Implement a simple C++ class named Time.

It represents all possible times in a 24-hour period, including hours, minutes and seconds. An immediate representation issue is how to handle morning (am) and afternoon (pm) times. We could have a separate bool indicating whether the time is am or pm. It is easier, however, to represent the hours in military time. This means that the hours of the day are numbered from 0 to 23, with 13 being 1 pm, 14 being 2 pm, etc.

Checkpoint 1 In the first checkpoint you will get started by implementing the initial class design, several member functions, and a simple main program to test your class. The instructions below describe how to build your executable using from the command line using g++ or clang++ using Cygwin or UNIX terminal. Even if you plan to use Visual Studio or another IDE for the bulk of your work this semester, you are required to also show that you can successfully build and run this lab using g++ from a terminal on your own machine.

Make a subfolder inside of your Data Structures labs directory for lab 2. You’ll need to create Create 2 new empty code files named time.h and time.cpp. Note that in C++ the name of the header and implementation file are not required to exactly match the name of the class, but it is good coding style to do so. • Begin work on time.h. Within the file, declare a class called Time. Follow the form and syntax of the Date class from Lecture 3. Read the syntax carefully (such as the semi-colon at the end of the class declaration). Add private member variables for the hour, minute and second. In the public area of the class, declare two constructors: one, the default constructor, should initialize each of the member variables to 0; the other, having three arguments, accepts initial values for the hour, minute and second as function call arguments. Declare member functions to access the values of the hour, the minute and the second (three different member functions). It will be crucial for Checkpoint 3 to make these const. (Recall: a const member function can not change the member variables.) Don’t write the body of any of the functions in the time.h file. Save all the implementation for the time.cpp file. • Review the provided main.cpp. Note that we must #include “time.h” in addition to including #include . (Note: We use angle brackets for standard library includes and double quotes for our custom header files in the working directory.) The main program creates multiple Time objects, using the two different constructors and uses the functions that access the values of hour, minute and second by printing the two times. Note: There is a common confusion when creating a new variable using the default constructor: Time t1(5,30,59); // calls the non-default constructor w/ 3 integer arguments Time t2(); // COMPILE ERROR – a buggy attempt to call the default constuctor Time t3; // the *correct* way to call the default constructor • Now implement all of the class constructors and member functions in the file time.cpp. Don’t forget to add the line to #include “time.h”. Any file that uses or implements Time functionality must include the Time class header file. • Now, compile your program and remove errors. Here’s where the difference between compiling and linking matters. When compiling using g++ on the command line, the two separate command lines: g++ -c main.cpp -Wall g++ -c time.cpp -Wall compile the source code to create two object code files called main.o and time.o separately. The -c means “compile only”. Compiler errors will appear at this point. If there are errors in main.cpp (or time.cpp), then the files main.o (or time.o) will not be created. Use the ls command to check. Important Note: We only compile .cpp files. We do not directly compile header files. Header files are compiled only indirectly when included in a .cpp file.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

#include "time.h"

int main() {

  std::cout << "testing constructors" << std::endl;
  Time a;
  Time b(13,24,39);
  Time c(2,5,4);

  std::cout << "testing accessors" << std::endl;
  std::cout << "a: " << a.getHour() << " " << a.getMinute() << " " << a.getSecond() << std::endl;
  std::cout << "b: " << b.getHour() << " " << b.getMinute() << " " << b.getSecond() << std::endl;
  std::cout << "c: " << c.getHour() << " " << c.getMinute() << " " << c.getSecond() << std::endl;
  std::cout << std::endl;
  assert (b.getHour() == 13);
  assert (c.getMinute() == 5);
  assert (c.getSecond() == 4);

  // UNCOMMENT THESE TESTS AS YOU WORK THROUGH CHECKPOINT 2
  
  /*
  std::cout << "testing print" << std::endl;
  a.PrintAMPM();
  b.PrintAMPM();
  c.PrintAMPM();
  std::cout << std::endl;
  */

  /*
  std::cout << "testing modifiers" << std::endl;
  a.setHour(4);
  a.setMinute(32);
  a.setSecond(1);
  std::cout << "a: " << a.getHour() << " " << a.getMinute() << " " << a.getSecond() << std::endl;
  assert (a.getHour() == 4);
  assert (a.getMinute() == 32);
  assert (a.getSecond() == 1);
  a.PrintAMPM();
  std::cout << std::endl;
  */

  /*
  std::cout << "more testing print" << std::endl;
  Time noon(12,0,0);
  Time midnight(0,0,0);
  Time midnight2(0,0,0);
  std::cout << "noon      ";
  noon.PrintAMPM();
  std::cout << "midnight  ";
  midnight.PrintAMPM();
  std::cout << "midnight2 ";
  midnight2.PrintAMPM();
  std::cout << std::endl;
  */


  /*
  std::cout << "testing sort" << std::endl;
  std::vector<Time> times;

  times.push_back(Time(0,0,1));
  times.push_back(Time(12,59,59));
  times.push_back(Time(23,59,59));
  times.push_back(Time(1,1,1));
  times.push_back(Time(22,22,22));
  times.push_back(Time(12,0,1));
  times.push_back(Time(11,59,59));
  times.push_back(Time(12,0,0));
  times.push_back(Time(23,23,23));
  times.push_back(Time(0,0,0));
  times.push_back(Time(13,13,13));
  times.push_back(Time(10,10,10));
  times.push_back(Time(2,2,2));
  times.push_back(Time(14,14,14));
  times.push_back(Time(3,3,3));
  
  // also try some duplicates
  // times.push_back(Time(2,2,2));
  // times.push_back(Time(23,23,23));

  sort(times.begin(),times.end(),IsEarlierThan);
  for (int i = 0; i < times.size(); i++) {
    times[i].PrintAMPM();
  }
  */
  

Expert Answer

 

main.cpp
———————————————-
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include “time.h”

int main() {

std::cout << “testing constructors” << std::endl;
Time a;
Time b(13,24,39);
Time c(2,5,4);

std::cout << “testing accessors” << std::endl;
std::cout << “a: ” << a.getHour() << ” ” << a.getMinute() << ” ” << a.getSecond() << std::endl;
std::cout << “b: ” << b.getHour() << ” ” << b.getMinute() << ” ” << b.getSecond() << std::endl;
std::cout << “c: ” << c.getHour() << ” ” << c.getMinute() << ” ” << c.getSecond() << std::endl;
std::cout << std::endl;
assert (b.getHour() == 13);
assert (c.getMinute() == 5);
assert (c.getSecond() == 4);

// UNCOMMENT THESE TESTS AS YOU WORK THROUGH CHECKPOINT 2

std::cout << “testing print” << std::endl;
a.PrintAmPm();
b.PrintAmPm();
c.PrintAmPm();
std::cout << std::endl;

std::cout << “testing modifiers” << std::endl;
a.setHour(4);
a.setMinute(32);
a.setSecond(1);
std::cout << “a: ” << a.getHour() << ” ” << a.getMinute() << ” ” << a.getSecond() << std::endl;
assert (a.getHour() == 4);
assert (a.getMinute() == 32);
assert (a.getSecond() == 1);
a.PrintAmPm();
std::cout << std::endl;

std::cout << “more testing print” << std::endl;
Time noon(12,0,0);
Time midnight(0,0,0);
Time midnight2(0,0,0);
std::cout << “noon      “;
noon.PrintAmPm();
std::cout << “midnight “;
midnight.PrintAmPm();
std::cout << “midnight2 “;
midnight2.PrintAmPm();
std::cout << std::endl;

std::cout << “testing sort” << std::endl;
std::vector<Time> times;
times.push_back(Time(0,0,1));
times.push_back(Time(12,59,59));
times.push_back(Time(23,59,59));
times.push_back(Time(1,1,1));
times.push_back(Time(22,22,22));
times.push_back(Time(12,0,1));
times.push_back(Time(11,59,59));
times.push_back(Time(12,0,0));
times.push_back(Time(23,23,23));
times.push_back(Time(0,0,0));
times.push_back(Time(13,13,13));
times.push_back(Time(10,10,10));
times.push_back(Time(2,2,2));
times.push_back(Time(14,14,14));
times.push_back(Time(3,3,3));

// also try some duplicates
// times.push_back(Time(2,2,2));
// times.push_back(Time(23,23,23));
sort(times.begin(),times.end(),isEarlierThan);
for (int i = 0; i < times.size(); i++) {
times[i].PrintAmPm();
}

}
—————————————————————————–
Time.cpp
—————————————-
#include <iostream>
#include “Time.h”

Time::Time() {
hour = 0;
minute = 0;
second = 0;
}

Time::Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}

int Time::getHour() const {
return hour;
}

int Time::getMinute() const {
return minute;
}

int Time::getSecond() const {
return second;
}

void Time::PrintAmPm() const {
std::string ampm;
if (getHour() >= 12) {
ampm = ” pm”;
} else {
ampm = ” am”;
}

if (getHour() == 0 || getHour() == 12) {
std::cout << 12 << “:”;
} else {
std::cout << getHour() % 12 << “:”;
}

if (getMinute() < 10) {
std::cout << “0”;
}
std::cout << getMinute() << “:”;
if (getSecond() < 10) {
std::cout << “0”;
}
std::cout << getSecond() << ampm << std::endl;

}

void Time::setHour(int h) {
hour = h;
}

void Time::setMinute(int m) {
minute = m;
}

void Time::setSecond(int s) {
second = s;
}

bool isEarlierThan(const Time & t1, const Time & t2) {
if (t1.getHour() < t2.getHour()) {
return true;
} else if (t1.getHour() > t2.getHour()) {
return false;
}

if (t1.getMinute() < t2.getMinute()) {
return true;
} else if (t1.getMinute() > t2.getMinute()) {
return false;
}

if (t1.getSecond() < t2.getSecond()) {
return true;
}

return false;
}

———————————————————————
Time.h
—————————–
class Time{

public:
Time();
Time(int h, int m, int s);
//accessors
int getHour() const;
int getMinute() const;
int getSecond() const;
void PrintAmPm() const;

//modifiers
void setHour(int h);
void setMinute(int m);
void setSecond(int s);

private:
int hour;
int minute;
int second;
};

bool isEarlierThan(const Time & t1, const Time & t2);

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.