CS253: Software Development with C++

Spring 2022

HW 4

CS253 HW4: Operations!                

Changes                

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

Description                

For this assignment, you will write a two classes, one called Show (capital S) and one called Schedule (capital S) that keep track of various TV programs.                 

The class Show represents a single TV program, and the class Schedule represents several TV programs. This simple provider shows the same programs at the same time each day.                 

A TV program consists of:

Methods & operations                

Show must have the following public methods & operators:                

default constructor
No default constructor is allowed. Show s; must fail to compile.
Show(string name, string channel, int start, int length, bool adult)
  • 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.
Throw a runtime_error if the object can’t be created due to empty name or channel, negative starting time, starting time ≥ 24 hours, or spanning midnight: 11:30ᴘᴍ–12:30ᴀᴍ is not allowed.
Construction must store a unique positive int id. You may assume that no more than a million Show objects will be created.
Copy constructor
Assignment operator
Copy all information, including the id, from another object of the same class.
.name()
.channel()
.start()
.length()
.adult()
.id()
Return the corresponding attribute. The starting time returned is in minutes since midnight. The length returned is in minutes.

Schedule must have the following public methods & operators:                

default constructor
The default ctor creates an empty Schedule.
Copy constructor
Assignment operator
Copy all information from another object of the same class.
Schedule += Show
Add this Show to the Schedule. Throw a runtime_error if the Show can’t be added due to overlapping times with another Show in this Schedule on the same channel. In an error is thrown, the Schedule must not be modified.
Schedule += Schedule
Add all the Shows from the right-hand side to the left-hand side. Throw a runtime_error if any Show can’t be added due to overlapping times with another Show in this Schedule on the same channel. In an error is thrown, the left-hand argument must not be modified. This operation must not modify the right-hand argument.
Schedule + Schedule
Return another Schedule containing all the Shows in either argument. Throw a runtime_error if any of the Shows overlap on the same channel. This operation must not modify either argument.
Schedule -= int
The right-hand side represents the id of a Show. Remove that Show from the Schedule. Throw a runtime_error if no such Show is in the Schedule.
.size()
Return a size_t indicating the number of programs in the object.
Schedule[size_t]
Return the corresponding Show from this Schedule. The first Show is index zero. If the argument is out of range, throw a range_error which mentions both the desired index, and the number of available Shows.
ostream << Schedule
Write a description of the entire schedule to the given output stream. The programs may be occur in any order. Each line of output ends with a newline, including the last one.
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 class Schedule or class Show, 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                

libhw4.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 libhw4.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 libhw4.a. Particularly, do not put main() in Schedule.h or Schedule.cc. You will also have to create Schedule.h, and put it into hw4.tar. We will test your program by doing something like this:                 

    mkdir a-new-directory
    cd the-new-directory
    tar -x </some/where/else/hw4.tar
    cmake . && make
    cp /some/other/place/test-program.cc .
    g++ -Wall test-program.cc libhw4.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 hw4.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/HW4 fetched by unknown <unknown> with Linux UID 65535 at 2024-06-26T09:58:08 from IP address 18.226.4.235. 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(hw4)

# 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
    -Wconversion -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} Show.cc 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 …
Consolidate compiler generated dependencies of target hw4
… make output appears here …
Consolidate compiler generated dependencies of target test
[100%] Built target test
% cat test.cc
#include "Schedule.h"
#include <cassert>
#include <string>

using namespace std;

int main() {
    Schedule s;
    Show a("Secret Agent XN-12320767", "Spy Television", 0, 7);
    const Schedule s2(s);
    const Show b("War War War", "War", 7, 24*4-7, true);
    assert(b.name() == "War War War");
    assert(b.channel() == "War");
    assert(b.start() == 105);
    assert(b.length() == 1335);
    assert(!a.adult());
    assert(b.adult());
    assert(!(a.id() < 0));
    assert(!(b.id() < 0));
    assert(a.id() != b.id());
    s += a;
    s += b;
    cout << s << "---\n";
    assert(s.size() == 2);
    bool caught = false;
    try {
        s -= -16;
    }
    catch (const runtime_error &) {
        caught = true;
    }
    assert(caught);
    assert(s.size() == 2);
    s -= a.id();
    assert(s.size() == 1);
    cout << s << "---\n";
    s -= b.id();
    assert(s.size() == 0);
    cout << s << "---\n";

    Schedule love;
    love += Show("Love Isthmus", "LUV", 0, 7, true);
    Schedule war;
    war += Show("Trek Wars: Spock vs. Vader", "SyFi", 0, 7, false);
    Schedule life;
    life = s2;          // will soon be overwritten
    life += a;          // will soon be overwritten
    life = love+war;
    assert(life.size()==2);
    cout << life << "---\n";
    assert(life[0].channel() == "LUV"  || life[1].channel() == "LUV");
    assert(life[0].channel() == "SyFi" || life[1].channel() == "SyFi");

    caught=false;
    try {
        cout << life[987].name();
    }
    catch (const range_error &r) {
        caught = true;
        const string message = r.what();
        assert(message.find("987") != string::npos);    // bad index
        assert(message.find("2") != string::npos);      // current size
    }
    assert(caught);
    return 0;
}
% ./test
01:45-24:00 War            War War War*
00:00-01:45 Spy Television Secret Agent XN-12320767
---
01:45-24:00 War War War War*
---
---
00:00-01:45 SyFi Trek Wars: Spock vs. Vader
00:00-01:45 LUV  Love Isthmus*
---

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 hw4.tar to the assignment “HW4”. It’s due 11:59ᴘᴍ MT Saturday, with a five-day late period.                 

How to receive negative points:                

Turn in someone else’s work.