Programming Assignment 6


  1. (25 points) Problem 7.8 of the book. Stopwatch. Design a class named StopWatch. The class should contain:
    • Private data fields __startTime and __endTime with get methods.
    • A constructor that initialized __startTime with the current time. (use time.time()).
    • A method names start() that resets __startTime with the current time.
    • A method named stop() that sets __endTime to the current time.
    • A method named getElapsedTime() that returns the elapsed time for the stop watch in milliseconds.
    Write a main method to test your program. The test should measure the execution time of adding numbers from 1 to 1,000,000.
    • Name the file and program ch07_pr08.py.
    • Example execution (python3 ch07_pr08.py on MAC and py -3 ch07_pr08.py on Windows) user provides input (red) (output may vary):
      Summing numbers from 1 to 1,000,000 takes 73.802 milliseconds
  2. (25 points) Problem 8.1 of the book. Check SSN. Write a program that prompts the user to enter a social security number in the format ddd-dd-dddd, where d is a digit. The program displays whether the social security number is in the correct format or not. Use a function oriented approach and do not have any global variables.
    • Name the file and program ch08_pr01.py.
    • Example execution (python3 ch08_pr01.py on MAC and py -3 ch08_pr01.py on Windows) user provides input (red):
      Enter an SSN number in the format ###-##-####: 123-45-6789
      Valid SSN
    • Example execution (python3 ch08_pr01.py on MAC and py -3 ch08_pr01.py on Windows) user provides input (red):
      Enter an SSN number in the format ###-##-####: 123-456-789
      Invalid SSN
  3. (25 points) Problem 8.17 of the book. A Point class. Design a class named Point to represent a point with x- and y-coordinates. The class should contain:
    • Two private data fields __x and __y with get methods.
    • A constructor that constructs a point with specified coordinates with default point (0, 0).
    • A method named distance that returns the distance from this point to another point of the Point type.
    • A method named isNearBy that returns true if another point (a parameter) is close to this point. A point will be considered close if their distance is less than 5
    • Implement the __str__ method to return a string in the form (x, y).
    Write a main method to test the class. It should prompt the user to enter two points, display the distance between the points, and indicate whether they are near each other.
    • Name the file and program ch08_pr17.py.
    • Example execution (python3 ch08_pr17.py on MAC and py -3 ch08_pr17.py on Windows) user provides input (red):
      Enter two points x1, y1, x2, y2: 2.1, 2.3, 19.1, 19.2
      The distance between the two points is 23.97
      The two points are not near each other
    • Example execution (python3 ch08_pr17.py on MAC and py -3 ch08_pr17.py on Windows) user provides input (red):
      Enter two points x1, y1, x2, y2: 2.1, 2.3, 2.3, 4.2
      The distance between the two points is 1.91
      The two points are near each other
  4. (25 points) Bio-inspired Behaviors. Implement a class representing a robotic cockroach and its behavior with regard to light. Using the GoPiGo within the class (and any other data you need), create methods to mimic the following behavior:
    • When the "cockroach" sees a light, let it spin randomly to the left or right.
    • If the "cockroach" does not see a light, then there is a 25% chance that it moves forward.
    • Every action takes place for 0.1 seconds. Let the "cockroach" live for 60 seconds.
    Let your main method test your class and application. Do not have any global variables in this program and rely on implementing a proper class and methods.
    • Name the file and program bio_inspired.py.
    • Example execution (cat bio_inspired.py | ssh pi@gopigoXX python3 -u on MAC and type bio_inspired.py | ssh pi@gopigoXX python3 -u on Windows, remember to use the correct hostname) user provides input (red) (output may vary):
      No textual output.
    • Create a file bio_inspired.txt. In this file, please answer the following questions:
      • Why are bio-inspired robots useful?
  5. Bonus. (10 points) Problem 8.3 of the book. Check password. Some websites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows:
    • A password must have at least eight characters.
    • A password must consist of only letters and digits.
    • A password must contain at least two digits.
    Write a main method to test the function. The program should prompt the user to enter a password and displays whether it is valid or not.
    • Name the file and program ch08_pr03.py.
    • Example execution (python3 ch08_pr03.py on MAC and py -3 ch08_pr03.py on Windows) user provides input (red):
      Enter a password: abcdef11
      Valid password
    • Example execution (python3 ch08_pr03.py on MAC and py -3 ch08_pr03.py on Windows) user provides input (red):
      Enter a password: !!abcdef11
      Invalid password


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in through GitHub classroom. Please find the link to create a repository at the top of this page, or through this link. Robot problems must be demonstrated by 5pm and assignments must be submitted to GitHub by 11:59pm of the due date. I do not accept any late assignments.

Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.

Points