Computer Science Department |
CSCI 135 |
Part 1: Distance Between Two Points
The straight line distance between two points (x1, y1) and (x2, y2) on a Cartesian coordinate plane is given by the equation:
Your program will take in the values of the x1, y1 and x2, y2 points as integers from the command line. It must then calculate the distance between the two points. For example, if the input was 0 0 3 4, your program would calculate and output the distance between (0, 0) and (3, 4) which is 5. Of course, the answer should be something else if the arguments on the command line are different.
In Python, to take the square root of a number, say x, import the math library and use the method below:
import math math.sqrt(x)
To raise a number x to a power, say 2, you can use:
The output from your program should be formatted exactly as below, with the command line input shown between parentheses and the result of your equation at the end:x**2
Name your program Distance.py.The distance between (0, 0) and (3, 4) is 5.0
Part 2: Birthday Wizard
Given a person's year of birth and an age, both from the command line, calculate what year they will be (or were) that age. Your output should look like:
Name this program Birthday.py.You will (or did) turn 21 in the year 2020.
Part 3: Boolean Logic
Three sensors are attached to a printing device, with three alarms attached to the sensors. The first sensor, "A," detects if the device needs ink. The second sensor, "B," detects if the device needs repair. The third sensor, "C," detects if the device is jammed.
If the device jams or needs repair, alarm 1 sounds. If the device jams or is short on ink, alarm 2 sounds. If two or more problems occur at once, alarm 3 sounds.
The command line arguments should contain values for each of the sensors. These are boolean values, either True or False, which should be entered as 1 or 0 respecitvely on the command line. In order to convert a String to a boolean value, use bool(int(var_name)).
The table below shows which alarms sound based on the sensors. 0 represents False and 1 represents True.
SensorA | SensorB | SensorC | Alarm1 | Alarm2 | Alarm3 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 0 | 0 |
0 | 1 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 0 | 1 | 0 |
1 | 0 | 1 | 1 | 1 | 1 |
1 | 1 | 0 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 |
Your program should print out the value of each of the alarms, depending on the input sensor values. This must be done using boolean logic, not just a list of logic statements. You should be able to calculate the value of the alarms in three statements. The output should be formatted like:
Alarm 1: True Alarm 2: False Alarm 3: False
Name this program Logic.py.
Grading Each of the three programs is worth 10 points each, for a total of 30 points. For each program, you will be graded according to the following criteria:
Grade Item | Distance | Birthday | Logic | Points Earned |
---|---|---|---|---|
Program Compiles and Runs | 4 | 4 | 4 | |
Header Comment | 2 | 2 | 2 | |
Program Runs Correctly (including correctly formatted output) | 4 | 4 | 4 |
Page last updated: September 11, 2019