SelectionAndIteration

Part 2:
Iteration Statements

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should be able to:
  • Implement selection control in a program by using if statements.
  • Write boolean expressions with relational and boolean operators.
  • Evaluate given boolean expressions correctly.
  • Nest an if statement inside another if statement's then or else part correctly.
  • Describe how objects are compared.
  • Implement repetition control in a program by using while statements.
  • Implement repetition control in a program by using do-while statements.
  • Implement a generic "loop-and-a-half" repetition control statement.
  • Implement repetition control in a program by using for statements.
  • Nest a loop repetition statement inside another repetition statement.
  • Choose the apppropriate repetition statement for a given task.

Items in red are learning objectives for this part of the project.

Instructions:

Group work

SelectionAndIteration report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your SelectionAndIterationReport.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in ? to ? minutes after seeing their instructor demonstrate the ideas.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.

    Preliminaries

  1. Open your SelectionAndIteration project and your SelectionAndIterationReport.txt file. Add a heading to your report to indicate the beginning of Part 2.
  2. Skim this document. Then return to this point.
  3. Question: About how many minutes do you think it will take you to complete Part 2 of SelectionAndIteration?
  4. Question: What time is it now?

    Documenting the Project

  5. Write JavaDoc comments based on the planned behavior of the project. Include all of the required tags. Ensure that you can generate the HTML documentation correctly.
  6. Iteration Statements

  7. Unless otherwise specified, use JOptionPane for all required input and output (I/O) in this project (you are free to use whatever method you prefer for other I/O)..
  8. Modify the introductory message so that it uses the showConfirmDialog() method to ask the user whether or not they would like to use the program. Use the YES_NO_OPTION optionType. The return value should be either YES_OPTION or NO_OPTION. Read the JDK Help for more details.
  9. Question: What is the return type of the showConfirmDialog() method?
  10. Add an if statement so that the user's input determines whether or not the rest of the program is executed. Make sure that the program exits gracefully regardless of the user's input.
  11. Question: Where does the closing curly brace of the if statement go with respect to the System.exit( 0 ); statement?
  12. Change the if statement into a while statement, and add another showConfirmDialog() call at the bottom of the loop that asks the user whether or not to run the program again. The syntax for a while statement is:
          while( condition ) {
             statement block
          }
  13. Question: Explain why it makes sense to store the value returned by the new showConfirmDialog() call in the same variable as the value returned from the first showConfirmDialog() call.
  14. Comment out the code that inputs the coefficient a, and "hardcode" the value to 1.
  15. After the code that inputs the coefficient b, calculate the largest value of c that will result in real roots [maxC = b^2 / 4].
  16. Question: Explain why ensuring that c is no greater than b^2 / 4 guarantees that the roots will be real.
  17. Add a while loop with a break statement that ensures that the value of c is small enough that the roots will be real. Modify the prompt for the coefficient c so that informs the user of the criterion, and add an appropriate dialog window that informs the user when the input does not meet the criterion.
  18. Question: How many times would the loop while (false) { statement block } execute?
  19. Test your program on these cases:
  20. Question: Explain why you are convinced that your program is correct.
  21. Add a for loop that displays the roots for 10 cases, each of which uses the input value of the coefficent b, and which use 10 equally spaced values of c ranging from the input value to the largest value that results in real roots. Write your code in such a way that changing the number of cases only requires changing the value of a constant. The syntax for a for statement is:
          for( initialization expression; condition; increment expression ) {
             statement block
          }
  22. Question: How much does the coefficient c change from one iteration to the next?
  23. Submitting the Project

  24. Make any final changes that you want to your project.
  25. Generate the HTML documentation for your project.
  26. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  27. Tag your CVS module SelectionAndIteration2.
  28. Summary

  29. Question: Describe the role of while statements.
  30. Question: Describe the role of do-while statements.
  31. Question: Describe the role of for statements.
  32. Question: How much time did you spend on Part 2 of SelectionAndIteration? Compare your answer to how much time you had estimated that you would spend. You receive full credit no matter how far off your estimate is!