Programming Principles: Workshop 2 and 3 Solutions - Code and Testing

Verified

Added on  2022/09/18

|9
|533
|25
Homework Assignment
AI Summary
This document presents solutions for Programming Principles Workshops 2 and 3. The solutions cover two main tasks: an Income Tax Program and an Enhanced Lap Time Recorder. For the Income Tax Program, the document includes pseudocode, a flowchart, Python code, and testing examples. The Python code implements the tax calculation based on income brackets. For the Enhanced Lap Time Recorder, the document provides pseudocode, a flowchart, and Python code. The code allows users to input lap times, calculates the fastest, slowest, and average lap times, and displays the results. The document also includes concept revision examples for data types, selection, data structures, and loops, demonstrating how to manipulate lists and strings in Python. The solutions are designed to help students understand and apply programming concepts effectively, and are accompanied by testing data to validate the code's functionality.
Document Page
Running head: PROGRAMMING PRINCIPLES
PROGRAMMING PRINCIPLES
Name of the Student
Name of the University
Author Note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1PROGRAMMING PRINCIPLES
Workshop 2, Task 4 (“Income Tax Program”):
Pseudocode:
Input income in $
If income <= 18200
Display tax = $ 0
Elseif 18000<income <= 37000
Calculate tax = (income - 18000)*0.19
Display tax
Elseif 37000< income <=90000
Calculate tax = (income - 37000)*0.325 + 3572
Display tax
Elseif 90000< income <= 180000
Calculate tax = (income - 90000)*0.37 + 20797
Display tax
Else
Calculate tax = (income - 180000)*0.45 + 54097
Display tax
End If
Document Page
2PROGRAMMING PRINCIPLES
Flowchart:
Python code:
income = float(input('Enter your income in $: '))
if income <= 18200:
Document Page
3PROGRAMMING PRINCIPLES
tax = 0;
elif income > 18000 and income <= 37000:
tax = (income - 18000)*0.19 # 19 cents = $ 0.19
elif income > 37000 and income <= 90000:
tax = (income - 37000)*0.325 + 3572 # 32.5 cents = $ 0.325
elif income > 90000 and income <= 180000:
tax = (income - 90000)*0.37 + 20797 # 37 cents = $ 0.37
else:
tax = (income - 180000)*0.45 + 54097 # 45 cents = $ 0.45
print('The amount of total tax to be paid for income of $%.2f is $%.2f' % (income,tax) )
Testing:
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4PROGRAMMING PRINCIPLES
Workshop 3, Task 5 (“Enhanced Lap Time Recorder”):
Psudocode:
Initialize laptime with empty list or entry not equal to ‘x’
Initialize lap number = 1
Initialize laplists with empty array
While laptime ≠ ‘x’
Input laptime from user with display of lap number
If laptime == ‘x’
Break from while loop
Else
Convert laptime to floating point format
End If
Append laptime to laplists array
Increment lap number
End While
Calculate minimum laptime and print as fastest laptime
Calculate maximum laptime and print as slowest laptime
Document Page
5PROGRAMMING PRINCIPLES
Calculate average laptime = (total of all laptimes / length of laplists)
Print average laptime correct to two decimal places
Flowchart:
Document Page
6PROGRAMMING PRINCIPLES
Python code:
laptime = ‘a’; i = 1; laplists = []
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7PROGRAMMING PRINCIPLES
while laptime != 'x':
laptime = input('Enter laptime %.0f in seconds (Enter "x" to end the loop): ' % i)
if laptime == 'x':
break
else:
laptime = float(laptime)
laplists.append(laptime)
i = i+1
print('\nFastest Laptime is :',min(laplists))
print('\nSlowest Laptime is :',max(laplists))
avglaptime = sum(laplists)/len(laplists)
print('\nAverage Laptime is : %.2f' % avglaptime)
Testing:
Document Page
8PROGRAMMING PRINCIPLES
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]