CS‐521 讲解

【CS?1 讲解】CS‐521 Homework Assignment 6
1
Assignment Directions
The specific instructions for these programming exercises are adapted from problems shown in the
textbook.Make sure to follow the specific instructions in THIS document.
Each program is worth 50 points for a total of 100 points (100%).
Style Requirements
For all assignments, follow the guidelines in the PEP8 Standards and Best Practices that have been
shared to date, along with course specific requirements. Minor deductions will occur for:
? Improper naming of programs or zip container
o Among other things, names for regular programs and zip file must be all lower case
and class file names are CamelCase.
? Missing program docstring
? Inadequate # line comments (just a few in each program, don’t go crazy)
? Going significantly over 80 characters for code/comment lines
? Not naming objects appropriately
o variables and functions must use snake_case (lower case plus underscores)
o CONSTANT variables in all upper case
o Class Objects, which are taught in module 6, as CamelCase
? Asking for input() without descriptive prompts telling the user what is expected.
o Especially not mentioning the delimiter when input will be split()
? Printing output that is not clearly explained (where necessary)
? Not providing a docstrings for functions and class objects
? Crashing uncontrolled on bad user input
? Having too large a scope for try blocks
? Not using format() to round output and add thousand separating commas (US standards)
o 123456.126should be 123,456.13 ? '{:,.2f}'.format(123456.126)
? Having a “main()” function, with no input arguments or return values
? Not using an if name == '__main__' block where appropriate

CS‐521 Homework Assignment 6
2
Chapter 11 Exercise
6.11.1:Write a python program using the following requirements:
Create a class called Sentence which
? has a constructor that takes a sentence string as input.
o The default value for the constructor should be an empty string
o The sentence must be a private attribute in the class
? contains the following class methods:
o get_all_words — Returns all the words in the sentence as a list
o get_word — Returns only the word at a particular index in the sentence
? Arguments: index
o set_word — Changes the word at a given index in sentence to a new word
? Arguments: index, new_word
? Does not return anything
o a str method — Returns the sentence as a string using the built‐in method
Include a unit test in an if name block to test the class. Use assert to check for errors.
Hint: The problem might be easier to code if sentence attribute is a list of the words, where
punctuation has already been parsed out.
Unit Test Template:

  1. Set a sentence variable with a sentence of your choice
  2. Instantiate the Sentence object using the variable
  3. Test that get_all_words() correctly returns that sentence as a list
  4. Test that get_word(#) correctly returns the # word in the sentence
  5. Test that set_word() actually changes a word in the sentence object
  6. Print the sentence before and after the word change using the built in str method

    CS‐521 Homework Assignment 6
    3
    Chapter 12 Exercise
    6.12.2 Write a python program using the following requirements:
    The human body has many organs. These organs have some properties in common and
    others that are specialized.
    ? Create an Organ class as a base class for specific organs
    o Attributes for the organ class are:
    ? organ_name
    ? organ_weight_grams
    ? is_vital_organ
    ? organ_system
    ? is_transplantable
    ? organ_gender
    o Required reserved methods for organ class:
    ? Constructor
    ? Repr method that returns the attributes of organ instance
    ? Create a Heart class as a child of the organ class
    o The heart is a vital organ in the Muscular system
    o Attributes for heart, defined in the constructor, are:
    ? heart_length_cm
    ? heart_weight_grams (use the parent class)
    ? heart_thickness_cm
    ? heart_breadth_cm
    o Required reserved methods for heart class:
    ? Constructor
    ? Repr method that returns the attributes of heart instance
    o Required general methods for heart class:
    ? heart_status – returns ‘Pumping blood’
    ? heart_weight_oz – returns the heart weight in ounces
    ? 1 gram = 0.035 ounces


    CS‐521 Homework Assignment 6
    4
    6.12.2 continued…
    ? Create a Brain class as a child of the organ class
    o The brain is a vital organ in the Nervous system
    o Attributes for brain, defined in the constructor, are:
    ? brain_volume (in centimeters)
    ? brain_weight_gram (use the parent class)
    o Required reserved methods for brain class:
    ? Constructor
    ? Repr method that returns the attributes of brain instance
    o Required general methods for brain class:
    ? brain_status – returns ‘Thinking’
    ? brain_weight_oz – returns the brain weight in ounces
    ? 1 gram = 0.035 oz
    Include a unit test in an if name block to test the class. Use assert to check for errors.
    Unit Test Template:
    ? Instantiate the heart object with attributes 12, 280, 6.0, 9.0
    ? Instantiate the brain object with attributes 1260, 1370.0
    ? Test that all the parent and child attributes match what was instantiated
    ? Test that the general methods return the appropriate values
    Where to submit?
    Click Assignments in the Navigation Area and then click on the title of the assignment to enter the
    submission area and upload the zip file with your response.

    推荐阅读