A9-Getting Classy

Table of Contents


Objectives

  • Develop classes and construct objects.
  • Hide data using the keyword private.
  • Write member functions that access private data.
  • Code multiple constructors to initialize private data.
  • Call member functions of objects.

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
  • You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
  • You may not look at another student's code until you complete and submit this assignment, except for code you develop code together with your pair-programming partner.
  • You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.

Preparation

  1. Make sure you have completed the exercises from lesson 9.
  2. Complete the Review Exercises in CodeLab 9. These exercises will help prepare you for the problem-solving programs and should be completed first.

Project Specifications

Your solutions to these projects must use only techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we have covered, as well as the new rules, including:

  1. Class naming conventions (See: Class Names) new!
  2. Indentation in classes and placement of curly braces new!
  3. Every function declaration (prototype) in the class, including constructor functions, have a function comment block new!
  4. Every file has a file comment block
  5. No magic numbers (See: No Magic Numbers)

goat image
Image source: kilmkin (pixabay)

Project 1: Class Goat

Goats are one of the oldest domesticated species, and have been used for their milk, meat, hair, and skins over much of the world. Female goats are called "nannies" and male goats are called "billies". Young goats are called kids [1]. Goats can live up to about 18 years, though the average life span is 8-12 years [2].

The sound a goat makes is called a "bleat". The bleat sound can vary depending on what the goat is trying to communicate [3]. The usual sound is something like "Meh-eh-eh" or "Baa" [4].

Project Specifications
  1. Write a declaration and a definition for a class named Goat along with a short main() function to test the class code.
  2. You must name the source code file goat.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. The Goat class must contain exactly (no more and no less) two private member variables:
    1. string name
    2. int age
  4. Write function declarations (prototypes) inside the class and separately define (implement) all the functions outside the class declaration (curly braces of the class).
  5. Write a default constructor that sets the name to "Kid" and the age to 0 (zero).
  6. Write a two-parameter constructor that sets the name and age member variables to the parameter values. The parameters must be coded in the order of name then age.
  7. Write one "get" function for each of the member variables that returns the current value. The name of the function must follow the naming standards for "get" functions, which is the name of the variable with the prefix word "get".
  8. Write an accessor function rest() that has no parameters and prints three or more z's to the console, like:
    Zzzz
    
  9. Write an accessor function bleat() that has no parameters and prints either the word "Meh-eh-eh" or the word "Baaa", like:
    Meh-eh-eh!
    
  10. All accessor functions must include the keyword const as discussed in lesson 9.2.4 under Accessor Functions and in the textbook on pages 402-403 under the section on, "const Correctness".
  11. In your main() function, construct two Goat objects (variables). Then write a story that calls all the member functions of the class at least one time. For example:
    Meet my new goat Kid.
    He plays with his mother, a 9 year old Goat named Nanny.
    My new goat Kid knows three things:
    1. How to eat. Show them Kid: Chomp, burp!
    2. How to bleat. Show them Kid: Meh-eh-eh!
    3. How to rest. Show them Kid: Zzzz
    
  12. Have no user input for this project. Testing code for the program is hard-coded in main(). Do NOT use any keyboard input streams, like cin, in this project. If you have a cin statement in your code, you will get a low score.
  13. After displaying the story, exit the program.
  14. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Goat: Wikipedia article.
  2. Teeth, Life Expectancy & How to estimate a goat's age: Fias Co Farm
  3. What sound does a goat make?: Reference.com
  4. Billy Goat Sounds: the SoundBible.com

Image
Image Source: Atlas of the Universe [1]

Project 2: Class Star

In this project we encapsulate the data and functions for a class that produces Star objects. The Star objects represent nearby stars in our galaxy. We will declare a programming interface and define the functions for that interface. In addition we will write a short main() function to test our interface.

Since stellar distances are so long, most distances are given in light-years. A light year is about 5.878e+12 miles [2]. To accommodate this measurement distance, we will provide functions to provide the distance in miles and in light-years.

Planets have been detected around several stars. As of March 16, 2017, a total of 3461 stars have confirmed exoplanets [3]. The Sun has 8 planets. Two of the stars we cataloged in Assignment 4 have confirmed exoplanets: Proxima Centauri b and Epsilon Eridani b. Barnard's star was once thought to have planets but their existence has since been refuted [4].

Project Specifications
  1. Write a declaration and a definition for a class named Star, that represents a star, along with a short main() function to test the class.
  2. Begin with the starter code: starclass.cpp. You must name the source code file starclass.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. The Star class must contain exactly (no more and no less) three private member variables:
    1. a string named name
    2. an int named planets
    3. a double named distance
  4. Write function declarations (prototypes) inside the class and separately define (implement) all the following functions outside the class declaration (curly braces of the class):
    1. A default constructor that sets all member variables to default values
    2. A three-parameter constructor with parameters coded in the order of: name, planets and distance. Each parameter sets its matching member variable to the value it was passed (by value).
    3. One "get" function for each member variable that returns the current value. The name of the function must follow the naming standards for "get" functions, which is the name of the variable with the prefix "get".
    4. One "set" function for each member variable that sets a new value. The name of the function must follow the naming standards for "set" functions, which is the name of the variable with the prefix "set".
    5. Accessor function getDistanceInLightYears() that has no parameters and returns the average distance to the Earth in light years. For conversion, use the constant 5.878e+12 miles for 1 light year.
    6. Accessor function print() that has no parameters and returns no values, but displays the data of the object all on one line like this:
      Epsilon Eridani            1   6.17e+13     1.05e+01
      The data displayed in order are:
      1. star name
      2. Number of planets
      3. distance in miles
      4. distance in light years

    Note: even though you must write the get functions, you may not need to actually call them for this assignment. We will be using the get functions in future assignments.

  5. All accessor functions must include the keyword const as discussed in lesson 9.2.4 under Accessor Functions and in the textbook on pages 402-403 (1/e: 382) under the section on, "const Correctness".
  6. In the starclass.cpp file, use the specified main() function to do the following:
    1. Construct at least the three Star objects in the starter code.
    2. Set values (name, distance and planets) for two objects using the three-argument constructor and for one object using the default constructor and "set" functions.
    3. Displays output for all 3 objects as shown in the Example Output specification.

    Uncomment each constant and function call as you complete the assignment. Use the provided code for testing.

  7. Example Run: When you run the program you must see the following output without any user input.
    Stellar Neighbors:
                                  Distance from Earth In
    Star               # Planets      Miles  Light Years
    Sun                        8   9.31e+07     1.58e-05
    Proxima Centauri           1   2.48e+13     4.22e+00
    Epsilon Eridani            1   6.17e+13     1.05e+01
    

    In the above example run, the user entered "whatever" (without the quotes) as the thing to enter.

    In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  8. Create the correct spacing for the output using the setw()formatting manipulator described on pages 49-51 (1/e: 53-55) of the textbook. In addition, you will need to use the left and right formatting manipulators. To use these manipulators, include the iomanip library. Align text or numbers in a setw() field using left or right to align the data. For example:
    cout << setw(18) << left << name; // aligns data left in 18 character field
    

    Use scientific to set exponential notation.

  9. After displaying the output, exit the program.
  10. Do not ask the user for any input for this project. Data for the program is hard-coded in main(). Do NOT use any keyboard input streams, like cin, in this assignment. If you have a cin statement in your program, you will get a low score.
  11. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. The Nearest Stars: An Atlas of the Universe
  2. How Long is a Light Year?: Universe Today article.
  3. NASA Exoplanet Archive: an online astronomical exoplanet and stellar catalog.
  4. Barnard's Star: Wikipedia article.

Extra Credit

The following are worth extra credit points if the main program works well:

  1. Complete the assignment using pair programming. (2 points)
  2. Add an extra function named eat() to the Goat class and call the function at least once. When called, the function prints (NOT returns) eating noises for the animal. (1 point)
  3. Add a function named toString() to class Star that has no parameters and returns a string value containing all the data about the star. Call the function one time to show that it works. (2 points)

    The function does NOT print any values and must NOT use cout. Instead, the function must return a string with one line of text like that shown below.

    Epsilon Eridani            1   6.17e+13     1.05e+01
    

    Once returned, print the string within main() using code like:

    cout << epsilon.toString() << endl;
    

    Note that this extra credit will require you to convert numbers to strings. Research stringstream for the conversion and provide an attribution for the source of your stringstream information, either a URL or book and page number. No attribution means no credit.

Make certain that your README.txt file describes any extra credit attempted.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

  1. Read the assigned reading in the textbook
  2. Complete the Tutorial Exercises in CodeLab 9 before the specified due date.

    Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Goat and Star Functionality (x2)

  • 7: Demonstrates mastery of the assignment.
    • Applies concepts from the lessons appropriately
    • Meets all specifications with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correctly formatted output
  • 6: Has all the functionality expected of the assignment with mostly correct formatting.
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications
    • Generates correctly formatted output given correct input
    • May have one minor formatting error
  • 5: Has most the functionality and format expected of the assignment
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications
    • Implementation seems more complicated than necessary.
    • May have one or more formatting errors in output
    • May have one minor functional error
  • 4: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications
    • Implementation seems excessively complicated.
    • May have many formatting errors in output
    • May have 2-3 minor functional errors
  • 3: Has some of the functionality expected of the assignment
    • Attempts to meet at least 1/2 of the specifications
    • Demonstrates many techniques from the lessons
    • Implementation seems excessively complicated.
    • May have more than 3 minor functional errors
    • Source code compiles with no errors or warnings
  • 2: Serious functional problems but shows significant effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lessons
    • Source code compiles but may have multiple warnings
  • 1: Serious functional problems but shows some effort and understanding.
    • Does not compile but shows an attempt to meet most specifications
    • Implementation seems somewhat complete but has errors
    • Demonstrates some techniques from the lessons
  • 0: Minimal to no work apparent or made in an incorrect way.
    • Not turned in
    • Does not compile with little work apparent
    • Attempts to meet less than 1/2 of the of the specifications
    • Uses techniques not covered in the course so far

Program Style

  • 4: Code is well-documented including:
  • 3: Code has a minor documentation error.
  • 2: Code has some documentation errors or a missing file.
  • 1: Code has many documentation errors or a missing file and some errors.
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab and Other Tutorial Exercises

Number completed correctly / number exercises * 8 and rounded up to the nearest integer.

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Submit your assignment to Canvas, in the assignment folder that matches the name of this assignment, following the instructions for submitting homework. Include the following items for grading:

  1. README.txt file
  2. All the exercise files from Lesson 9
  3. goat.cpp
  4. starclass.cpp

Your assignment files must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline, but must resubmit all your assignment files.

Last Updated: May 08 2017 @14:25:00