Part 1:
Selection 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.


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.


  1. Make sure that you're following the instructions above (in particular, you should be working with somebody new).
  2. Create a new project using the Basic Console Application template and the project name SelectionAndIteration.
  3. Create a file called SelectionAndIterationReport.txt, placing it in the docs subfolder of your SelectionAndIteration project.
  4. Open your SelectionAndIterationReport.txt file. You will use it to record your answers to the questions in all of the parts of this exercise. Start the report with an appropriate title, your names, the date, and a heading to indicate the beginning of Part 1.
  5. Skim this document. Then return to this point.
  6. Question: About how many minutes do you think it will take you to complete Part 1 of SelectionAndIteration?
  7. Question: What time is it now?
  8. Creating the CVS Module

  9. Create a new module for your SelectionAndIteration project, calling the module SelectionAndIteration, spelled just like that.
  10. Add your report and the initial contents of your SelectionAndIteration project to your new CVS module.
  11. Documenting the Project

  12. 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.
  13. Selection Statements

  14. 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)..
  15. Add an introductory message.
  16. Ensure that the program exits gracefully when it finishes.
  17. Input the coefficients of a quadratic equation ax^2 + bx + c = 0 from the user.
  18. Calculate the discriminant D = b^2 - 4ac.
  19. Use the disriminant to calculate both roots of the quadratic equation [root1 = (-b + sqrt(D)) / 2a, and root2 = (-b - sqrt(D)) / 2a].
  20. Test your program on these cases:
  21. Question: Explain what happens in the last case and why.
  22. Comment out the code that calculates the roots.
  23. Write an if statement that displays the message The roots are complex conjugates when the discriminant is negative. The syntax for an if statement is:
          if( condition 1 ) {
             statement block 1
  24. Question: Explain why it is important to include the curly braces, even though they are optional when there is only one statement in the body of the if statement.
  25. Add an else part to your if statement that displays the message The root(s) are real when the discriminant is non-negative. The syntax for an if-then-else statement is:
          if( condition 1 ) {
             statement block 1
          } else {
             statement block 2
  26. Question: Explain why it is important to add an else part in this case as opposed to a second if statement.
  27. Modify the else part so that it displays There is a single repeated root when the discriminant is zero, and There are two distinct real roots when the discriminant is positive.
  28. Question: You should now have a "cascaded if statement." Explain why it should be indented like this:
          if( condition 1 ) {
             statement block 1
          } else if( condition 2 )
             statement block 2
          } else {
             statement block 3
  29. Display the root(s) in each of the three cases. Note: in the complex case, the real part is -b / 2a, and the imaginary part is +/- sqrt(-D)/2a.
  30. Submitting the Project

  31. Make any final changes that you want to your project.
  32. Generate the HTML documentation for your project.
  33. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  34. Tag your CVS module SelectionAndIteration1.
  35. Summary

  36. Question: Describe the role of if statements.
  37. Question: How much time did you spend on Part 1 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!