Lab 1: HTML Holidays (4 Points)
Chris Tralie
Overview / Logistics
The purpose of this lab is for you to brush up on your knowledge of methods and how they fit together while you also get some experience with C++ and makefiles. You will be converting data from one format (parallel arrays) to another format (HTML). Though it is often tedious, automating such data conversions is a common task in software engineering, so this is a valuable skill.
Furthermore, you will get experience making code that writes code!. This is also a common task, as we might have a higher level language with more context (like C++ in our example) that writes tons of code in a simpler language (assembly language) that would be very tedious to write out by hand.
Compared to 173, you will notice that the design of your methods is left a little bit more up to you. This is intentional. Students are moving more towards autonomy in their design in this course, so you should spend a little bit more time up front sketching out a plan on a piece of paper before you start coding. Of course, I am also happy to discuss your ideas! But just be sure to think before you code.
Click here to download the skeleton code for this assignment, which you can open and edit with Visual Studio Code. You will be editing holidays.cpp
, dateutils.h
, and dateutils.cpp
.
Learning Objectives
- Access elements in parallel arrays using loops
- Use existing methods with provided documentation
- Write methods that work together in concert to accomplish a task
- Write a complete program with very little code in the
main
function - Programmatically convert data from one format to another
What to submit
When you are finished, you submit a .zip file with all of your code to canvas. Also, answer the two discussion questions below. We will return to these in the next unit
- What do you feel is advantageous about storing data in parallel arrays?
- What feels clunky about storing this kind of data in parallel arrays? (For instance, what is the process you would have to go through if you wanted to add a holiday?)
Background: HTML Tables
Modern web pages are formatted in a scripting language known as Hypertext Markup Language, or HTML for short. This code can be written up front and fixed for static web pages (like the one you're looking at now), but we very often want to generate it programmatically so we don't have to write it from scratch every time a small amount of data changes. This is the basis of server-side web languages such as PHP.
Every type of formatting occurs in what's known as a "tag." The web site w3schools.com has some nice tutorials on different HTML tags, but we will be just focusing on one set of tags today for HTML tables. You do not need any prior HTML language to do this lab. Just follow the rules I'm giving you here. You can read more about them at this link, but the example covers everything you need to know for this lab. In particular,
-
Every table tag begins with
<table>
and ends with</table>
-
Within the table, every row tag begins with
<tr>
and ends with</tr>
-
Within each row, every column tag begins with
<td>
and ends with</td>
-
The data for the table goes within the
<td> ... </td>
tags.
For example, the code below generates a table that looks like this
Course Title | Course Name | Prerequisites |
CS 173 | Intro To Computer Science | None |
CS 174 | Object Oriented Programming | CS 173 |
CS 476 | Computer Graphics | CS 271 or CS 274 |
Lab 1 Task
In this lab, you will programmatically print out text containing all of code for a small HTML web page with a table of some major holidays in 2021/2022 (obtained from this link), and then you will pipe this output string to an HTML file in the command line, which you can open with a browser. You will not be writing any HTML code directly! Rather, you will be automatically creating HTML code using C++.
Data Format
The information about the holidays is provided in four pieces in the holidays.cpp
file. The first is constant array of string
values with the name of the holidays, as shown below:
Then, the months of each holiday are provided in an array of ints
and there are similar arrays HOLIDAY_DAYS
and HOLIDAY_YEARS
for days and years, respectively. These arrays are parallel, which means that corresponding indices refer to the same item. For instance
HOLIDAY_NAMES[1]
is the string "Rosh Hashanah (New Year)"
HOLIDAY_MONTHS[1]
is the number 9
So, the complete information for entry 1
is the name HOLIDAY_NAMES[1]
, the month HOLIDAY_MONTHS[1]
, the day HOLIDAY_DAYS[1]
, and the year HOLIDAY_YEARS[1]
.
Formatting Specification
To generate the complete HTML web page, you will need to
- Print a string with the HTML header and the start table tag
- Loop through all of the elements in these arrays to print each row of the HTML table
- Print the end table tag and the code to end the HTML document
For each row, you should print out HTML code that creates 3 columns
- The name of the holiday
- The weekday of the holiday
- The full date of the holiday, in the format Month Day, Year
If all goes well, when you put all of the rows together and write them out to holidays.html
, you should see a table with the following information:
Holiday | Weekday | Date |
Labor Day | Monday | September 5, 2022 |
Rosh Hashanah (New Year) | Sunday | September 25, 2022 |
Navratri/Dussehra | Monday | September 26, 2022 |
Yom Kippur (Day of Atonement) | Tuesday | October 4, 2022 |
Mawlid al-Nabi (birthday of Mohammad) | Friday | October 7, 2022 |
Sukkot (Feast of Tabernacles) | Sunday | October 9, 2022 |
Indigenous Peoples Day | Monday | October 10, 2022 |
Shemini Atzeret | Sunday | October 16, 2022 |
Simchat Torah | Monday | October 17, 2022 |
Diwali | Monday | October 24, 2022 |
Veterans Day | Friday | November 11, 2022 |
Thanksgiving Day | Thursday | November 24, 2022 |
Native American Heritage Day | Friday | November 25, 2022 |
Hanukkah (Chanukah) | Sunday | December 18, 2022 |
Christmas Day | Sunday | December 25, 2022 |
New Years Day | Sunday | January 1, 2023 |
Orthodox Christmas | Saturday | January 7, 2023 |
Martin Luther King Day | Monday | January 16, 2023 |
Lunar New Year | Sunday | January 22, 2023 |
Maha Shivaratri | Saturday | February 18, 2023 |
Presidents' Day | Monday | February 20, 2023 |
Ash Wednesday | Wednesday | February 22, 2023 |
Purim | Monday | March 6, 2023 |
Holi | Wednesday | March 8, 2023 |
Beginning of Ramadan | Wednesday | March 22, 2023 |
Rama Navami | Thursday | March 30, 2023 |
April Fool's | Saturday | April 1, 2023 |
Passover (Pesach) | Wednesday | April 5, 2023 |
Good Friday | Friday | April 7, 2023 |
Easter | Sunday | April 9, 2023 |
Orthodox Good Friday | Friday | April 14, 2023 |
Orthodox Easter | Sunday | April 16, 2023 |
Lailat al-Qadr | Tuesday | April 18, 2023 |
Eid al-Fitr (End of Ramadan) | Friday | April 21, 2023 |
Shavuot | Thursday | May 25, 2023 |
Memorial Day | Monday | May 29, 2023 |
Juneteenth | Monday | June 19, 2023 |
Eid al-Adha | Wednesday | June 28, 2023 |
Independence Day | Tuesday | July 4, 2023 |
Muharram (Al Hijrah New Year) | Saturday | July 29, 2023 |
Janmashtami | Wednesday | September 6, 2023 |
Provided Helper Methods
The following methods are provided in holidays.cpp
to help you out. You should look at the documentation in comments above each method
getHTMLStart()
: This method prints out a string that should go at the beginning of your HTML filegetHTMLEnd()
: This method prints out a string that should go at the end of your HTML file
The following method is provided in dateutils.h
(and implemented in dateutils.cpp
)
string getWeekday(int year, int month, int day)
: Use this method to return the weekday as a string
C++ code you need to write
You should write two methods in holidays.cpp
that work together to accomplish the task
- A method that outputs a string corresponding to a particular row of the table.
- A method that pieces the full HTML string together and then writes all of the HTML code to a file
You should also create a helper method in dateutils.cpp
and dateutils.h
that converts a month number into a string representing that month. HINT: Your code will be much shorter if you use an array to store and reference the month names instead of having a bunch of if statements or a switch statement.
Running the code
A makefile has been provided to automatically compile everything. To build the project, type make
in your terminal in the project directory. To run the program and pipe the output text to an HTML file, type
Then, you should be able to open the file holidays.html
in your browser.
Extra Credit (Up To +1)
Here are a few things you can do that are above and beyond- If all of the tags are there, the HTML will work properly, even if there is no whitespace in between the tags. But the code will be a big mess. Format the HTML code nicely with line breaks and tabs where appropriate
- Stylize your table using CSS
Don't know where to start?
The easiest place to start would be to define the first method that takes in a number for the month and prints a string corresponding to that month.
Note also: To get your program to do anything, you'll have to have statements in the main. By themselves, the functions may work perfectly, but if you don't have code that runs in the main that eventually calls them, nothing will happen when you run your code.