CS253: Software Development with C++

Spring 2022

HW 2

CS253 HW2: TV Schedule                


TV Guide, March 1990

Changes                

Updates to the assignment will be noted here. None yet!                 

Description                

For this assignment, you will write a class called Schedule (capital S) that keeps track of various TV programs. This simple provider shows the same programs at the same time each day.                 

A TV program consists of:

Times                

For this provider, programs always start at a multiple of 15 minutes, and are multiples of 15 minutes long. Programs are restricted to a single day; they do not span the midnight boundary: 11:30ᴘᴍ–12:30ᴀᴍ is not allowed. The starting time of a program is represented as a multiple of 15 minutes since midnight: 7:00ᴀᴍ is 28. Negative starting times, or starting times ≥ 24 hours, are not allowed. The length of a program is represented as a positive (>0) multiple of 15 minutes: noon–1:45ᴘᴍ is 7.                 

Methods                

Schedule must have the following public methods:                

default constructor
The default ctor creates an empty Schedule.
Copy constructor
Assignment operator
Copy all information from another object of the same class.
Destructor
Destroy.
.add(string name, string channel, int start, int length, bool adult)
Add a program to the schedule.
  • start: program start time (multiple of 15 minutes since midnight)
  • length: program length (multiple of 15 minutes)
  • adult: true for adult content, assumed false if not given.
Return −1 if the program can’t be added due to empty name or channel, invalid start or length arguments, or due to programs overlapping on the same channel. Otherwise, return a unique positive integer id, identifying this program.
.size()
Return a size_t indicating the number of programs in the object.
.erase(id)
Remove the program identified by id, which was returned by .add(). Return true iff the id referred to a program in the schedule.
.print(ostream)
Write a description of the entire schedule to the given output stream. Use cout if os is not given. The programs may be occur in any order.
The output format for a given program is:
start end channel name*
The start and end times are represented as HH:MM (hours:minutes) in a 24-hour clock, with leading zeroes. When the schedule contains only a single program, put one space between the fields. When the schedule contains multiple programs, use the minimum amount of padding after the channel so the names line up. There is no space before the *, which indicates an adult program.

The types and names in the method descriptions, above, do not determine the C++ declarations of those methods. They only serve to informally describe what sort of arguments a method might take. You might pass certain arguments by reference, use const, declare return types, etc.                 

Const-correctness, for arguments, methods, and operators, is your job. For example, it must be possible to call .size() on a const object, or to copy a const object to a non-const object.                 

You may define other methods or data, public or private, as you see fit. You may define other classes, as you see fit. However, to use the Schedule class, the user need only #include "Schedule.h", not any other header files.                 

Non-Requirements                

Several things are not specified by this assignment. That means that the answer to these questions is “It’s up to you.”

Debugging                

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Libraries                

libhw2.a is a library file. It contains a number of *.o (object) files. It must contain Schedule.o, but it may also contain whatever other *.o files you need. The CMakeLists.txt shown creates libhw2.a. It does not contain main().                 

To be explicit, the provided CMakeLists.txt does:

The tar file must contain at least all the files required to do this.                 

Testing                

You will have to write a main() function to test your code. Put it in a separate file, and do not make it part of libhw2.a. Particularly, do not put main() in Schedule.h or Schedule.cc. You will also have to create Schedule.h, and put it into hw2.tar. We will test your program by doing something like this:                 

    mkdir a-new-directory
    cd the-new-directory
    tar -x </some/where/else/hw2.tar
    cmake . && make
    cp /some/other/place/test-program.cc .
    g++ -Wall test-program.cc libhw2.a
    ./a.out

We will supply a main program to do the testing that we want. You should do something similar. It’s your choice whether to include your test program in your hw2.tar file. However, cmake . && make must work. If it fails because you didn’t package test.cc, but your CMakeLists.txt requires test.cc, then your build failed, and you get no points. Test your tar file, not just your code.                 

This is the Colorado State University CS253 web page https://cs.colostate.edu/~cs253/Spring22/HW2 fetched by unknown <unknown> with Linux UID 65535 at 2024-06-30T07:00:48 from IP address 18.189.182.108. Registered CSU students are permitted to copy this web page for personal use, but it is forbidden to repost the information from this web page to the internet. Doing so is a violation of the rules in the CS253 syllabus, will be considered cheating, and will get you an F in CS253.

Sample Run                

Here is a sample run, where % is my shell prompt:                 

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw2)

# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$"
   AND NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
    message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()

# Using -Wall is required:
add_compile_options(-Wall)

# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)

# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
    -fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
    -Wctor-dtor-privacy -Wduplicated-cond -Wduplicated-branches
    -Werror -Wextra-semi -Wfatal-errors -Winit-self -Wlogical-op
    -Wold-style-cast -Wshadow -Wunused-const-variable=1
    -Wzero-as-null-pointer-constant)

# add_compile_options must be BEFORE add_executable.

# Create the executable from the source file main.cc:
add_library(${PROJECT_NAME} Schedule.cc)
add_executable(test test.cc)
target_link_libraries(test ${PROJECT_NAME})

# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
    tar -cf ${PROJECT_NAME}.tar *.cc *.h CMakeLists.txt)

% cmake . && make
… cmake output appears here …
… make output appears here …
[ 50%] Built target hw2
[100%] Built target test
% cat test.cc
#include "Schedule.h"
#include <cassert>
#include <string>

using namespace std;

int main() {
    Schedule s;
    s.add("CSU Tonight!", "CSU", 23*4, 4, true);
    int id = s.add("Secret Agent XN-6684671", "Spy Television", 0, 4);
    s.add("Keeping up with the Applins", "FCTV", 9*4, 2);
    s.add("Jeopardy"s, "FCTV", 0, 5);
    s.print();
    cout << "---\n";
    assert(s.size() == 4);
    assert(!s.erase(-17));
    assert(s.size() == 4);
    assert(s.erase(id));
    assert(s.size() == 3);
    s.print(cout);
    return 0;
}
% ./test
00:00-01:00 Spy Television Secret Agent XN-6684671
23:00-24:00 CSU            CSU Tonight!*
09:00-09:30 FCTV           Keeping up with the Applins
00:00-01:15 FCTV           Jeopardy
---
00:00-01:15 FCTV Jeopardy
23:00-24:00 CSU  CSU Tonight!*
09:00-09:30 FCTV Keeping up with the Applins

Requirements                

If you have any questions about the requirements, ask. In the real world, your programming tasks will almost always be vague and incompletely specified. Same here.                 

Tar file                

    cmake . && make

How to submit your work:                

In Canvas, check in the file hw2.tar to the assignment “HW2”. It’s due 11:59ᴘᴍ MT Saturday, with a five-day late period.                 

How to receive negative points:                

Turn in someone else’s work.