Programming Assignment 6
-
(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. (usetime.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.
- Name the file and program
ch07_pr08.py
. - Example execution
(
python3 ch07_pr08.py
on MAC andpy -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
- Private data fields
-
(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 andpy -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 andpy -3 ch08_pr01.py
on Windows) user provides input (red):
Enter an SSN number in the format ###-##-####: 123-456-789
Invalid SSN
- Name the file and program
-
(25 points) Problem 8.17 of the book.
A Point class. Design a class named
Point
to represent a point withx-
andy-
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 thePoint
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 than5
- Implement the
__str__
method to return a string in the form (x, y).
- Name the file and program
ch08_pr17.py
. - Example execution
(
python3 ch08_pr17.py
on MAC andpy -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 andpy -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
- Two private data fields
-
(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.
- Name the file and program
bio_inspired.py
. - Example execution
(
cat bio_inspired.py | ssh pi@gopigoXX python3 -u
on MAC andtype 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?
-
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.
- Name the file and program
ch08_pr03.py
. - Example execution
(
python3 ch08_pr03.py
on MAC andpy -3 ch08_pr03.py
on Windows) user provides input (red):
Enter a password: abcdef11
Valid password - Example execution
(
python3 ch08_pr03.py
on MAC andpy -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
- Name each file and program as listed in the instructions.
- The top of each program should have a comment block where the first line
is your name and following lines are a description of your program.
# Name: Jory Denny
# This program debates Batman vs Superman!
# References:
# - Ref 1. - Any program solution that defines its own function or class must have a
main
function and properly specified entry point into the program, e.g.:
def main():
# Do something
if __name__ == '__main__':
main() - Use proper coding style (described more in Grading below)
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in.
- Follow turn-in instructions precisely.
- Failure to complete any of these steps will result in a significant loss of points.
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.
- Soft copy (Online submission)
- Accept the invitation to the classroom assignment, which creates your repository on GitHub.
- Clone this repository, which includes some starting files.
- Add assignment files (stage, commit, push).
- Modify the Coverpage.txt file to provide name, sources, signature of following the honor code, etc. Be sure to stage, commit, and push these changes as well. If you do not fill out the coverpage, I will not grade the assignment.
- After the due date, I will clone your repository and add a grade and comments as necessary. After which you can pull these changes for your records.
Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.
Points
- Each assignment is graded out of 100 points (not including bonus).
- Criteria and point distribution
- If the code is not named precisely or does not execute correctly, -50% on a problem.
- Following instructions and algorithm used to solve the problem, 25% on a problem. Following instructions is extremely important in computer science, train yourself to think like a computer. There are many ways to solve a problem, some may be better or worse than others.
- Stylistic elements of written code,
25% of a problem. Style includes (but is not
limited to):
- Every class, method, and function must be documented with docstrings describing its purpose, formal arguments, and return value.
- Control flow should be documented with intent and purpose of code
- Naming should be descriptive and consistent
- Indenting properly
- Consistent spacing
- Avoiding code duplication
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in. If I suspect excessive use of online materials, I will invite you to walk me through your solution. If you are not able to explain the work sufficiently, I will not give credit for that problem.
- If there are any discrepancies in grades please see the instructor during his office hours or by appointment (do not discuss with the lab assistants or graders).