122 Final Project

Enter any 11-digit prime number to continue... -- Anon.

Class Types: Separating Specification from Implementation.

When we first learned how to write user-defined functions, we adopted three guidelines. A function is:

  1. Declared before main()
  2. Called in main()
  3. Defined after main()

For user-defined class types, we now have three similar guidelines for writing member-functions. Consider a class named X. A member function of X is:

  1. Declared in X.h
  2. Called in X-client.cpp
  3. Defined in X.cpp

Previously we learned how to write multi-function programs. Now we are learning to write multi-file programs (one .h and two .cpp files).

The class interface (how you declare it and use objects of the class) is specified (i.e., declared) in the .h file, and implemented (i.e., defined) in the .cpp file.

This is called information hiding or data encapsulation: details of the implementation are restricted to the .cpp file, and can be changed independently of the specification. This allows developers to update and optimize an implmentation without requiring any change to the client(s).

I. A Time Machine.

Solve ex. 9.5, p. 309, as follows:
  1. Put the Time class in /122/include/Time.h.

  2. The constructor for the time class should consult the system clock and determine the current time (PST). See pp. 56-57 for ideas. Note that Time(0) returns GMT. PST is -8 hrs from GMT, and your constructor should adjust for our time zone.

  3. Define at least one member function out-of-line, in /122/fp/Time.cpp.

  4. Name the client program time-client.cpp (/122/fp/time-client.cpp).

  5. Add a member function named display() that displays the current time in digital clock format (hh:mm:ss)

  6. Use an eof-controlled while loop in the client so that the user can test the Time::display() function on several values.

  7. When complete, upload Time.h to /122/include/, Time.cpp to /122/fp/, and time-client.cpp to /122/fp/ on shell.uoregon.edu.

II. StringMachine: Anagram Checker.

Solve ex. 9.10, p. 310 (& p. 250) by writing a StringMachine class and a client program.

Strategy: Liang offers solutions to even-numbered exercises. See isAnagram-liang.cpp for his non-OOP solution to the Anagram problem.

Using Liang's sort() function, here's my simplfied solution: isAnagram.cpp.

Object Oriented Solution:

Create a StringMachine class with a separate specification (StringMachine.h) and implementation (StringMachine.cpp).

Simplifying assumption: potential anagrams are lower-case strings.

  1. Declare the StringMachine class in /122/include/StringMachine.h.

  2. Define StringMachine::sort(string& s) as an out-of-line member function, in /122/fp/StringMachine.cpp. Test it.

  3. Define isAnagram() as an out-of-line member function, in /122/fp/StringMachine.cpp. Test it.

    To be consistent with 7.18 p. 250, use this function signature:

       bool StringMachine::isAnagram(string s1, string s2);

    (A function's signature is a combination of the function's name; the number, order, and types of its parameters. Sometimes function signatures for member functions are called "method signatures.")

  4. Name the client program strMach-client.cpp (/122/fp/strMach-client.cpp).

  5. Use an eof-controlled while loop in the client so that the user can test the StringMachine::isAnagram() function on several values.

  6. When complete, upload StringMachine.h to /122/include/, StringMachine.cpp to /122/fp/, and strMach-client.cpp to /122/fp/ on shell.uoregon.edu.

III. StringMachine: Palindrome Checker.

Strategy: reverse a string and then compare it to the original. If the reverse is equal to the original, it's a palindrome.

Simplifying assumptions: omit palindromes containing spaces and punctuation. I.e., just check for palindromic words not sentences.

Use an eof-controlled while loop in the client so that the user can test the StringMachine::isPalindrome() function on several values.

When complete, upload StringMachine.h to /122/include/, StringMachine.cpp to /122/fp/, and strMach-client.cpp to /122/fp/ on shell.uoregon.edu.

Note: Students completing both parts II & III, will have just one set of files with these names. strMach-client.cpp will test both member functions isAnagram() and isPalindrome().

IV. Extra Credit Options.

Do not spend any time on extra credit problems until you have completed all project requirements. Extra credit does not substitute for sub-standard work or requirements not completed.

  1. [3 pts] 12-hr digital time format: AM/PM. Modify your time class to use a 12-hr display format. Thus 15:15:00 should display as 03:15:00 PM.

  2. [7 pts] Modify your palindrome checker to recognize palindromes containing spaces, punctuation and upper/lower case.

    This means you'll have to read sentences that contain spaces using getline() instead of >>.

    Also, you will need a new client that just processes palindromes, and not anagrams. Name this new client XC-client.cpp.

Final Project Grading Rubric


C-Level: complete part I only.
B-Level: complete parts I and II.
A-Level: complete parts I, II, and III.

How to Submit the Final Project for Grading


On or before the due-date, submit your final project in Blackboard. Since you will be submitting your project URL, no local files need to be attached in Blackboard.


Image credits


Nautilus shell: scanned by and copyright Eric A. Meyer. CSS by Eric A. Meyer.

Jump to

CIS 122 EndGame