pH: Is human blood more acidic or alkaline than water?
Objectives
- practice implementation with simple conditionals and loops
- understanding abstractions to manage program complexity
Prerequisites
- basic math operators, conditionals, and loops
Reading
- Background: pH = -log(concentration of hydrogen ions), 7 is neutral, lower than 7 is acidic, higher than 7 is alkaline
Materials
Activities
- Discuss pH measures the concentration of hydrogen ions, which determines how acidic/alkaline a substance is. pH = 7 means Neutral, lower pH values than 7 mean more acidic, higher pH values than 7 mean more alkaline.
- Consider we want to build a quiz to ask the user to compare pH of two common substances. The program randomly picks two substances, asks the user to select the one which has a lower pH value, checks if the user is correct, repeats 5 times, and prints the % of correct answers.
- Ask them to implement the checking of whether the user answers a question correctly.
- Ask them to modify the program to ask 5 questions instead of 1 using a loop.
- Verify the output is correct.
- Discuss they modified two java "methods" (isCorrectAnswer and quiz) of the program and there are a total of 5 methods. Ask them to identify the other three. (main, QandA, and readSubstances)
- Discuss the organization and purpose of the five methods:
main (the whole program)
- readSubstances (read pH and names of substances from a file)
- quiz (ask multiple questions and calculate score)
- QandA (conduct one Q&A)
- isCorrectAnswer (check if the answer is correct)
- Discuss the different levels of abstractions. (main uses
readSubstances and quiz without worrying about the details of how they work. Similarly, quiz uses QandA without worrying about the details of how QandA works...)
- Many systems, for example cars, are designed with different levels of abstractions. As a driver, you mostly use the steering wheel, gas and brake pedals, but do you need to worry about the details of how the gas pedal makes the wheels turn faster? Automotive engineers work in different subsystems such as fuel system, engine system, brake system, transmission system, ...
- The program could have been written with only one method (main), why would we want to break the program into five methods?
- easier to read: the smaller programs have names, which idenitfy what they do at a higher level, in English, not in detail java instructions.
- easier to write: writing a number of smaller programs is easier than one large program--"divide and conquer."
- easier to find and correct (debug) mistakes: checking a number of smaller programs is easier than one large program--"divide and conquer."
- smaller programs can be reused in other larger programs (without rewriting them again and again). Note that java provides a lot of these "smaller" programs that can be reused by anyone. What is an example? (System.out.println)
-
-
Assessment
- Instead of pH values, what other chemical properties can the quiz compare? (molecular mass, atomic mass, ion charge)
- The sample pHvalues.txt has 16 substances, how many possible unique questions are there? (16 * 15 = 240; first substance has 16 possibilities, each of them has 15 possibilities for the second substance)
- How would you break QandA into three parts? (ask a question, collect answer, check answer)