G.E.S J.H.S Programming

The Software Development Method

Programming is a problem-solving activity that involves designing, coding, testing, and maintaining software applications.

If you are a good problem solver, you have the potential to become a good programmer.

To effectively solve problems and create high-quality software, developers often follow a structured approach known as the software development method. This method provides a systematic way to plan, execute, and manage software projects.

Software Development Method

  1. Specify the problem requirements
  2. Analyze the problem
  3. Design the algorithm to solve the problem
  4. Implement the algorithm
  5. Test and verify the completed program
  6. Maintain and update the program

Specify the Problem Requirements

The first step in the software development method is to clearly define what the problem is and what the desired outcome should be.

This involves gathering requirements from stakeholders, understanding the needs of users, and identifying any constraints or limitations that may affect the solution.

By specifying the problem requirements, developers can ensure that they are working towards a clear and well-defined goal, which helps to guide the subsequent steps in the development process.

The problem must be stated clearly and unambiguously.

You should eliminate unimportant aspects and focus on the root problem.

This requirement gathering from the one who posted the problem ensures that the solution addresses the core issue effectively.

Analyze the Problem

Once the problem requirements are specified, the next step is to analyze the problem in depth.

This involves breaking down the problem into smaller, more manageable components, identifying any patterns or relationships, and understanding the underlying principles that govern the problem domain.

By analyzing the problem, developers can gain insights into the best approaches for solving it and can identify potential challenges or obstacles that may arise during the development process.

During the analysis phase, developers may also consider different algorithms or data structures that could be used to solve the problem, and they may evaluate the trade-offs between different approaches to determine the most efficient and effective solution.

Analyzing the problem involves identifying the following:

Underline phrases in the problem statements that identify the inputs and outputs.

Let's consider an example:

Problem Statement: Write a program that takes a list of numbers as input and outputs the average of those numbers.

In this problem statement, the input is a list of numbers, and the output is the average of those numbers.

Problem Statement: Write a program that takes a number and outputs whether the number is even or odd.

In this problem statement, the input is a number, and the output is whether the number is even or odd.

Problem Statement: Write a program that takes a cost per unit and a quantity as input and outputs the total cost.

In this problem statement, the inputs are the cost per unit and the quantity, and the output is the total cost.

Once you know the problem inputs and outputs, develop a list of formulas that specify relationships between them.

For example, for the average problem: average = sum_of_numbers / count_of_numbers

For the even/odd problem: if number % 2 == 0, then it's even; otherwise, it's odd.

For the total cost problem: total_cost = cost_per_unit * quantity

Design the Algorithm

After analyzing the problem and identifying the inputs and outputs, the next step is to design an algorithm that outlines the steps needed to solve the problem.

An algorithm is a sequence of well-defined instructions that describes how to solve a particular problem.

When designing an algorithm, it's important to consider the following:

Designing the algorithm involves creating a step-by-step plan that outlines how the problem will be solved, considering the inputs, outputs, and the sequence of operations required.

Most algorithms can be broken down into a series of simple steps that are executed in a specific order.

The steps to most algorithms are as follows:

  1. Step 1: Initialize variables
  2. Step 2: Read input data
  3. Step 3: Process the data according to the algorithm
  4. Step 4: Output the results

For example, for the average of N numbers problem, the algorithm could be:

  1. Initialize variables (N,sum_of_numbers,counter,average)
  2. Read the value of N (the number of numbers)
  3. Use loop to read each number and add it to sum_of_numbers
  4. Calculate average as sum_of_numbers / N after the loop
  5. Output the average

For the even/odd problem, the algorithm could be:

  1. Initialize variables (number)
  2. Read the input number
  3. If the number is divisible by 2 (number % 2 == 0), output "Even"
  4. Otherwise, output "Odd"

For the total cost problem, the algorithm could be:

  1. Initialize variables (cost_per_unit, quantity, total_cost)
  2. Read the cost per unit and quantity
  3. Calculate total cost as cost_per_unit * quantity
  4. Output the total cost

Notes:

  1. % is the modulus operator, which returns the remainder of a division operation.
  2. * is the multiplication operator, which returns the product of two numbers.

For a bigger problem, you might divide it into smaller subproblems and design algorithms for each subproblem.

During the documentation phase of the algorithm design, flowcharts and pseudocode can be used to visually represent the algorithm and make it easier to understand and communicate.

Flowchart

A flowchart is a visual representation of an algorithm that uses various symbols to depict the steps and the flow of control.

Flowcharts can help to clarify the logic of an algorithm and make it easier to identify any potential issues or inefficiencies.

Common symbols used in flowcharts include:

Flowcharts can be created using various tools, such as drawing software, online flowchart makers, or even by hand on paper.

Flowcharts are particularly useful for visualizing complex algorithms and for communicating the logic of the algorithm to others, such as team members or stakeholders.

The flowcharts to the above algorithms are shown below:

Flowchart for Average of N Numbers

Flowchart for Average Algorithm

Explanation to flowchart

  1. Start: The algorithm begins at the "Start" symbol (oval) and ends with the end symbol (oval).
  2. Initializations: Initialize the variables needed for the algorithm (rectangle symbol is used for processes).
  3. N Input: Read the number of numbers to be averaged (parallelogram symbol is used for input/output).
  4. Loop: Use a loop to read each number (diamond symbol is used for decision points to check whether to end the loop or not).
  5. Yes: If the condition is true, get the number (parallelogram symbol is used for input/output) and add the number to the sum (rectangle symbol is used for processes).
  6. No: After the loop, calculate the average by dividing the sum by N (rectangle symbol is used for processes).
  7. Output: Output the average (parallelogram symbol is used for input/output).
  8. End: The algorithm ends at the "End" symbol (oval).

Flowchart for Even/Odd Algorithm

Flowchart for Even/Odd Algorithm

Explanation to flowchart

  1. Start: The algorithm begins at the "Start" symbol (oval) and ends with the end symbol (oval).
  2. Initializations: Initialize the variables needed for the algorithm (rectangle symbol is used for processes).
  3. Input: Read the number to be checked (parallelogram symbol is used for input/output).
  4. Decision: Check if the number is even or odd (diamond symbol is used for decision points).
  5. Yes: If the condition is true, output that the number is even (parallelogram symbol is used for input/output).
  6. No: If the condition is false, output that the number is odd (parallelogram symbol is used for input/output).
  7. End: The algorithm ends at the "End" symbol (oval).

Flowchart for Total Cost Algorithm

Flowchart for Total Cost Algorithm

Explanation to flowchart

  1. Start: The algorithm begins at the "Start" symbol (oval) and ends with the end symbol (oval).
  2. Initializations: Initialize the variables needed for the algorithm (rectangle symbol is used for processes).
  3. Input: Read the cost per unit and quantity (parallelogram symbol is used for input/output).
  4. Process: Calculate the total cost by multiplying cost per unit with quantity (rectangle symbol is used for processes).
  5. Output: Output the total cost (parallelogram symbol is used for input/output).
  6. End: The algorithm ends at the "End" symbol (oval).

Pseudocode

Pseudocode is a high-level, informal language that is used to describe the steps of an algorithm in a way that is easy to understand and can be translated into actual code.

Pseudocode is not bound by the syntax rules of any specific programming language, which allows developers to focus on the logic and structure of the algorithm without worrying about language-specific details.

When writing pseudocode, it's important to use clear and concise language, and to structure the pseudocode in a way that reflects the flow of the algorithm, using indentation and keywords to indicate control structures such as loops and conditionals.

Pseudocode can be a valuable tool for planning and designing algorithms, as it allows developers to think through the logic of the algorithm and identify any potential issues before writing actual code.

HOW TO WRITE PSEUDOCODE

When writing pseudocode, you can follow these guidelines:

1. Data Operations

Statement Meaning Example
INPUT / READ Get data from the user or a file INPUT UserAge
OUTPUT / PRINT Display a result or message on the screen PRINT "Access Granted"
SET / = Store a value in a variable (Assignment) SET Total TO 0

2. Decision Making (Conditional)

Statement Meaning Example
IF ... THEN ... ELSE ... ENDIF Checks if a condition is true. If it is, it does one thing; if not, it does something else.

Two Options

IF Age >= 18 THEN
     PRINT "You can vote"
ELSE
     PRINT "Too young"
ENDIF

More Than Two Options

INPUT score
IF score >= 90 THEN
     OUTPUT "Grade: A"
ELSE IF score >= 80 THEN
     OUTPUT "Grade: B"
ELSE IF score >= 70 THEN
     OUTPUT "Grade: C"
ELSE
     OUTPUT "Grade: F"
END IF

3. Repetition (Loops)

Statement Meaning Example
WHILE ... ENDWHILE Repeats steps as long as a condition remains true. (Pre-test loop)
WHILE WaterLevel < 100 ... FILL TANK ... ENDWHILE
FOR ... NEXT Repeats steps a specific number of times.
FOR Count = 1 TO 10 ... PRINT Count ... NEXT
REPEAT ... UNTIL Repeats steps until a condition becomes true. (Post-test loop).

Format

REPEAT
     // Block of code to execute
UNTIL <condition>

Example

REPEAT
     OUTPUT "Please enter your password:"
     INPUT userPassword
UNTIL userPassword = "Secret123"
OUTPUT "Access Granted"

Prompts users at least once and continues to be prompted until they enter the correct password.

REPEAT
   OUTPUT "Enter a number (0 to quit):"
   INPUT number
   IF number != 0 THEN
   square = number * number
   OUTPUT "The square is: ", square
ENDIF
UNTIL number = 0

Calculating the square of numbers until the user enters 0 to stop the process.

4. Arithmetic

Statement Meaning Example
+ Addition SET Sum TO a + b
- Subtraction SET Difference TO a - b
* Multiplication
SET Price TO Quantity * UnitCost
/ Division SET Pressure TO Force/Area

The pseudocodes for the above algorithm are as follows:

Pseudocode for Average of N Numbers

START
    Initialize sum_of_numbers to 0
    Initialize counter to 1
    Initialize average to 0
    Read N (the number of numbers)
    WHILE counter <= N DO
        Read number
        sum_of_numbers = sum_of_numbers + number
        counter = counter + 1
    END WHILE
    average = sum_of_numbers / N
    Output average
END

Note: the while is an iteration (repetition) structure and we will explain further under Loops.

Pseudocode for Even/Odd Algorithm

START
    Initialize number to 0
    Read number
    IF number % 2 == 0 THEN
        Output "The number is even"
    ELSE
        Output "The number is odd"
    END IF
END

Note: the if-else is a selection structure and we will explain further under Conditional statements.

Pseudocode for Total Cost Algorithm

START
    Initialize cost_per_unit to 0
    Initialize quantity to 0
    Initialize total_cost to 0
    Read cost_per_unit
    Read quantity
    total_cost = cost_per_unit * quantity
    Output total_cost
END

Implement the Algorithm

Once the algorithm is designed, the next step is to implement it by writing code in a programming language.

Implementation involves translating the algorithm into a specific programming language, such as C, Python, Java, etc., and ensuring that the code correctly follows the logic of the algorithm.

When implementing the algorithm, it's important to consider the following:

Implementing the algorithm involves writing code that accurately reflects the steps outlined in the algorithm, while also adhering to best practices for coding style and structure.

You must convert each algorithm into one or more code statements in your chosen programming language.

We will implement some of the above algorithms later on in this course after learning the basics of programming.

Test and Verify the Completed Program

After implementing the algorithm, it's crucial to test and verify that the completed program works correctly and meets the specified requirements.

Testing involves running the program with various inputs to ensure that it produces the expected outputs and behaves as intended under different conditions.

Verification involves checking that the program meets the requirements and specifications defined in the first step of the software development method.

When testing and verifying the program, it's important to consider the following:

Testing and verifying the program is essential to ensure that it functions correctly and meets the needs of the users and stakeholders.

Don't rely on just one test case. Create multiple test cases using different sets of data to thoroughly evaluate the program's behavior.

Maintain and Update the Program

After the program is deployed, it's important to maintain and update it as needed to address any issues, improve performance, or add new features.

Maintenance involves fixing bugs, optimizing code, and ensuring that the program continues to function correctly as the environment or requirements change.

Updating the program may involve adding new features, enhancing existing functionality, or adapting the program to new technologies or platforms.

When maintaining and updating the program, it's important to consider the following:

Maintaining and updating the program is an ongoing process that ensures the software remains relevant, functional, and valuable to its users over time.