CS 115 Lab 3
Calculating and Comparing (Individual)
Due Sunday, February 4, 2018, midnight
Educational goals of this lab - verify that every student can
- design and implementation
- input and output functions
- arithmetic operators
- importing a library
- using an if statement
Submit your files with this link. Use the Lab 3 menu choice.
and the "Code" menu choice.
(50 points) Individual Problem: Computing a distance and determining a hit or miss
Points on a plane are specified by x and y coordinates.
The distance between points can be calculated by the formula
You will ask the user for 4 numbers, which represent 2 points on the plane.
Calculate the distance between the two points using the sqrt function.
Then determine if the two points are within 25 units of each other, exactly 25 units from each other or more than 25 units from each other.
You can imagine one point as the center of a target that is 25 units
in radius and the other point as a dart being thrown at the target.
The dart either lands inside the target, on the edge of the target or outside the target.
(20 points) Problem #1: Complete a test plan.
- Calculate what A., B., C and D will be. Yes, a calculator is perfectly fine to use here.
4 integers (x1, y1, x2, y2)
|Normal, float distance || 5, 9, 3, 2 || A. |
|Boundary, zero distance || 6, 8, 6, 8 || B. |
|Normal, y's equal || 17, 3, 21, 3 || C. |
|Normal, x's equal || 10, 5, 10, 13 || D. |
|Normal, distance exactly 25||1, 1, 1, 26 || E. |
- Put these answers as comments at the bottom of the program you write for
(30 points) Problem #2: Write the program
- Write the program in Python to do what is described above.
The program needs a prolog, which
should contain your name, section, email address (uky.edu).
- It should also have documentation (comments) in the code.
You MUST use this design as documentation for your program.
Make the design steps comments in the Python file.
Put each Python code statement immediately after its design step comment.
- Here is the design.
Purpose: Calculate the distance between 2 points and decide if a target was hit or not.
Preconditions: input 4 integers, x,y of first point, x, y of second point
Postconditions: output prompts, distance between points (showing 3 places) and whether it was a hit, miss or on the edge
- Get needed data from the user, 4 integers in order x1, y1, x2, y2.
- Calculate the distance between the two points.
- Output the distance with label and a blank line.
- Compare distance to the value 25
- if it's equal to 25, output "on the edge"
- if it's less than 25, it's a hit
- if it's greater than 25, it's a miss
- The equation to use is shown above.
Use the sqrt function from the math library for this equation.
Make sure you document your program. Use good, multi-character, meaningful identifiers.
Use the round function to show 3 decimals in the distance.
- Write your logic in the most efficient way; you can do it without using 3 separate if's.
- Remember that ALL programs in this class MUST have a main function "def main():" and a call to main "main()" at the bottom of the file.
- Your program's output should look like the sample run.
Note that there is a blank line output between the
input statement and the output statement.
Note that the numbers the user types in are on the SAME LINE as the input
prompt, that is, "First x: 7". Make yours do that too.
Sample Runs - each one is a separate run of the program.
First x: 7
First y: 19
Second x: 13
Second y: 8
The distance is 12.53
Another Sample run
First x: 25
First y: 50
Second x: 17
Second y: 175
The distance is 125.256
- Run your test cases to make sure that the program is correct.
- Save the program to a file called lab3.py.
Submit your Python file using the link above
and the menu choices Lab 3 and Code.
Log off properly - you don't want your account misused by someone else!
Remember NOT to leave files
on the local hard drives in a lab or anywhere else on
campus! Make sure you save your projects on a portable
storage device you take with you!