Assignments - update


Please write the Python programs specified in this booklet. Please ensure that each program properly compiles, runs, and meets the specifications given. Submit these in hardcopy form with a cover page that includes your name and student ID number. Staple the solution to each assignment to this cover page and submit to the box in the reception area of the Department of Computer Science on the first floor of the Western Gateway Building. Grading. Each assignment is worth 8 marks each.

Copy each of code into a file and run.

Assignment 1: Convert Latitude/Longitude to Decimal Specification. Write your own Python program that converts Latitude and Longitude information in degrees/minutes/seconds into decimal. For example, 38 degrees, 53 minutes and 55 seconds, translates to 38.89861 degrees in decimal. Your programme should ask the user to enter the degrees, minutes and seconds separately, store these in variables called degrees, minutes and seconds, respectively. Then apply the following formula to store the decimal representation in a variable called decimal: decimal = degrees + minutes/60.0 + seconds/3600.0 Print the result to the screen, formated so that there are only four decimals places shown. An example converter is available online1

Assignment 2: Body Mass Index Specification. A person’s Body Mass Index (BMI) is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for their height. A person’s BMI is calculated by the formula: BMI = weight × 703/height@@0@@ , where weight is measured in pounds and height is measured in inches. Using the BMI a person can be classified as being either underweight, normal, overweight or obese, according to the following ranges for the BMI: Category BMI Range Underweight < 18.5 Normal weight 18.5-24.9 Overweight 25-29.9 Obese ≥ 30 Write a programme that asks for a person’s height in feet and inches, and th

Assignment 3: Rock, Paper, Scissors Game Specification. Write a program that lets the user play the game Rock, Paper, Sissors against the computer. The program should work as follows: 1. When the program begins, a random number is the range 1-3 is generated. If the number is 1, then the computer has chosen rock. If the number if 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet). 2. The user enters his or her choice of “rock”, “paper” or “scissors” at the keyboard. 3. The computer’s choice is displayed. 4. A winner is selected according to the following rules: - If one player chooses rock and the other chooses scissors, then rock wins. - If one player chooses scissors and the other chooses paper, then scissors wins. - If one player chooses paper and the other chooses rock, then paper wins. - If both players make the same choice, the game must be played again to determine the winner.

Assignment 4: Handling Files & Exceptions Specification. Assume that a file containing a series of integers, one integer per line, called numbers.txt exists on the computer’s disk in the same directory as the Python program you will write. Write a program that reads this file. Your program should write another file called output.txt that contains an integer for each line of the input file but containing the double of the value in the input. The last line of output.txt should contain the sum of the numbers in the numbers.txt file. Example. numbers.txt: 3 4 10 5 15 output.txt: 6 8 20 10 30 Original sum: 37

Assignment 5: Classes and Objects Specification. Write a class named Car that has the following attributes:

  • __year (for the car’s year)
  • __make (for the car’s make)
  • __model (for the car’s model)
  • __speed (for the car’s current speed)

The Car class should have an init method that accepts the car’s year, make, and model as arguments. These values should be assigned to the object’s year, make and model data attributes, respectively. It should assign 0 to the __speed data attribute. The class should also have the following methods: accelerate This method should add 5 to the speed data attribute each time it is called. brake This method should subtract 5 to the speed data attribute each time it is called. get speed This method should return the current speed. Next, design a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.