0% found this document useful (0 votes)
9 views

Programming assignment unit 4

The document outlines the incremental development process for creating two functions: one for calculating the hypotenuse of a right-angled triangle and another for calculating compound interest. Each function is developed in stages, starting from a basic structure to implementing and testing the logic, ensuring correctness at each step. This method promotes robust and maintainable code by validating each stage of development.

Uploaded by

sasj230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Programming assignment unit 4

The document outlines the incremental development process for creating two functions: one for calculating the hypotenuse of a right-angled triangle and another for calculating compound interest. Each function is developed in stages, starting from a basic structure to implementing and testing the logic, ensuring correctness at each step. This method promotes robust and maintainable code by validating each stage of development.

Uploaded by

sasj230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Part 1: Hypotenuse Function Using Incremental Development

In this task, we are required to create a function hypotenuse(a, b) that calculates

the length of the hypotenuse of a right-angled triangle, given the lengths of the two legs.

We use incremental development, a process that involves building and testing the

function in small steps to reduce the complexity of debugging.

Stage 1: Define the Function Skeleton

We begin by writing a basic function with placeholders:

def hypotenuse(a, b):

pass

At this stage, the function accepts two inputs but performs no calculations. This sets up

the input structure for further development.

Stage 2: Square Each Side

Next, we compute the square of each leg:

def hypotenuse(a, b):

a_squared = a ** 2

b_squared = b ** 2

print(a_squared, b_squared)

Test input:

hypotenuse(3, 4)

Output:

9 16
This confirms that the squaring step works correctly.

Stage 3: Add the Squared Values

We then add the two squared values:

def hypotenuse(a, b):

a_squared = a ** 2

b_squared = b ** 2

sum_squares = a_squared + b_squared

print(sum_squares)

Test input:

hypotenuse(3, 4)

Output:

25
Now we verify that the function correctly calculates the sum of the squares.

Stage 4: Calculate the Hypotenuse

We finalize the function by computing the square root of the sum of squares:

import math

def hypotenuse(a, b):

return math.sqrt(a ** 2 + b ** 2)

This is the final, optimized function.

Test Outputs

print(hypotenuse(3, 4))

print(hypotenuse(5, 12))

print(hypotenuse(8, 15))
These results are consistent with known Pythagorean triples, confirming the function

works correctly.

Conclusion:

Using incremental development simplifies troubleshooting and ensures each stage of

logic is validated. This approach leads to robust and maintainable code, especially in

applications where accuracy is essential.


Part 2: Custom Function Using Incremental Development – Compound Interest

Calculator

To build my programming portfolio and showcase my incremental development skills, I

created a custom function that calculates compound interest. The function demonstrates

a real-world financial computation that’s useful in personal finance apps or educational

tools. The formula used is:

t
A=P×(1+r )

Where:

 A is the future value

 P is the principal amount

 r is the annual interest rate (decimal)

 t is the time in years

Stage 1: Define the Function Structure

We begin by defining the function with parameters but no logic yet.

def compound_interest(p, r, t):

pass

This establishes the structure of the function. No output is expected at this point — it’s

just the starting point for development.


Stage 2: Implement Basic Formula

Next, we compute the compound interest using the standard formula. At this stage, we

simply print the result to verify the output.

def compound_interest(p, r, t):

amount = p * ((1 + r) ** t)

print(amount)

Test Input:

compound_interest(1000, 0.05, 2)

Output:

1102.5

This confirms the formula is being applied correctly.

Stage 3: Finalize and Return Result

Instead of printing, the function should return a properly rounded result for practical use:

def compound_interest(p, r, t):


return round(p * ((1 + r) ** t), 2)

Function Testing with Different Inputs

print(compound_interest(1000, 0.05, 2))

print(compound_interest(1500, 0.04, 3))

print(compound_interest(2000, 0.03, 5))

These test cases cover various principal amounts, interest rates, and time spans, and

confirm the function behaves as expected.

Technical Explanation

Each stage of development isolated and tested a logical component:

 Stage 1 defined the input interface.

 Stage 2 implemented and verified the mathematical logic.

 Stage 3 optimized the function to return a clean, formatted result.

This mirrors professional software development practices, where features are added

incrementally and tested immediately to catch bugs early and ensure correctness.
Reference:

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea

Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like