Lab 8: Classes

Due: Wednesday, August 18, 2021 (11:59 PM PDT)

Introduction

The assignment for this week will involve working with C++ classes.

We will also be grading for meeting requirements, using “class legal” code, and plagiarism. So, it is not enough for your lab to just pass the Gradescope autograder! Please read the instructions herein carefully.

It is highly recommended that you develop the algorithms for each program first and then develop the C++ code for it.

Step 1: Getting ready

Either through a graphical file system explorer or through the terminal:

  1. Navigate to your cs16 directory:

    $ cd cs16
  2. Create and navigate to the lab8 directory:

    $ mkdir lab8
    $ cd lab8
  3. Download the starter files to your lab8 directory. Either:

    1. Download these starter files from the course website next to the Lab 8 instructions link: http://sites.cs.ucsb.edu/~zsisco/cs16/#labs

    2. Or, if you are using CSIL, run the following commands from your lab8 directory:

    $ cp ~zsisco/public_html/cs16/lab8-files.zip . 
    
    $ unzip lab8-files.zip

Step 2: Create and edit your C++ programs

This week, you will need to create 3 files: headers.h, functions.cpp and newanagram.cpp. These correspond to the one exercise in this lab, described below. It is worth 100 points and everything (all 3 files) must be submitted for full assignment credit.

Important Note: We will take major points off if you use C++ instructions/libraries/code that either (a) was not covered in class, or (b) was found to be copied from outside sources (or each other) without proper citation.

newanagram.cpp

This program is a repeat of the anagram.cpp exercise you did in Lab 5, but with a twist: You will implement the program using a user-defined class called AString! If you recall, Lab 5’s exercise was a program that determines if 2 strings are anagrams. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, for example, the letters in the word “listen” can be re-arranged to spell “silent”.

In this exercise, you have to define a class called AString. In this string you must declare:

For all of the above, be very careful how you choose them to be private or public.

Your program must also define a function called compareCounts that takes in 2 integer arrays and returns a Boolean value that says if the 2 arrays are equivalent in their values or not.

Requirements

  1. Be sure to utilize only techniques we’ve covered in lecture. Do not use “special” arrays, do not use vectors, do not use built-in sorting functions, etc. You will get zero points if you do.
  2. You are given 3 skeleton programs: headers.h, functions.cpp, and newanagram.cpp. You must complete missing lines in all 3 files and submit all 3 as well.
  3. The skeleton program newanagram.cpp has lines in it that must not change from what you are given.
  4. In all 3 skeleton files, there are comments in there as guidelines to help you finish this exercise. You should read them carefully.
  5. The member function getAString should ask the user for an input (see examples below). The input can be a sentence containing space characters. The input is assigned to the member variable StringValue.
  6. The member function cleanUp should “clean up” the variable StringValue by removing all non-alphabet characters in it and then transforming all the remaining to lower-case characters.
  7. The member function countLetters takes 1 argument: an integer array of size 26. The function should count the frequency of occurrences of all the letters in StringValue and place that count in the integer array in the appropriate index (0 is for a, 1 is for b, etc.)
  8. The program function compareCounts takes 2 argument: 2 integer arrays each of size 26. The function should compare to see that the 2 arrays are the same. It returns a Boolean value of false if it is and true otherwise, as is implied by the statement in the skeleton file: bool badCount = compareCounts(ca1, ca2);
  9. There are 2 constructors as is evidenced by the way that the class objects are created in the main function in newanagram.cpp.

A session should look like one of the following examples (including whitespace and formatting). You’ll note the similarity to the exercise in Lab 5.

$ ./newanagram
Enter string value: Eleven plus two!!
Enter string value: Twelve plus three
The strings are not anagrams.
$ ./newanagram
Enter string value: Rats and Mice:)
Enter string value: in cat's dream?!
The strings are anagrams.

You should test your program with multiple examples before you submit it. Make sure your outputs match the above. The strings printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

Step 3: Create a makefile and compile the code

In order to learn another way to manage our source codes and their compilations, we will first create a Makefile and put in the usual g++ commands in it. Afterwards, whenever we want to compile our programs, the command is a lot shorter—so this is a convenience.

Using your text editor, create a new file called Makefile and enter the following into it:

all: newanagram

newanagram: newanagram.cpp headers.h functions.cpp
    g++ -o newanagram newanagram.cpp -Wall -std=c++11

Then from the command line, you can issue one command that will compile this code, like so:

$ make

If the compilation is successful, you will not see any output from the compiler. You can then run your program like this:

$ ./newanagram

If you encounter an error, use the compiler hints and examine the line in question. If the compiler message is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile the relevant files after you make any changes to the C++ code.

Step 4: Submit your programs for grading

Once you are satisfied your programs are correct, then it’s time to submit them. While working with others is OK, you still must submit your own lab. Even if you’re working with another person, do not copy each other’s code.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 8 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Follow the steps to upload all 3 files to Gradescope (do not upload any other files). If your files do not have those exact names as presented in this lab, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.