Programming Fundamentals in Java

Module [202] - Java Selection Statements

This module will explore Java's selection statements and allow you to implement conditional control flow in your Java programs.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #3 – Selections

Module Learning Outcomes

In this module, you will develop further your programming skills as follows;

Tracing
Ability to read and trace Java programs leveraging conditional expressions and IF-ELSE statements.
Implementing
Ability to write Java programs leveraging conditional expressions and IF-ELSE statements.
Testing
Ability to develop tests, on paper for now, that are going to allow us to ensure Java programs leveraging IF-ELSE statements and conditional expressions computes the results expected by requirements.
Debugging
Ability to troubleshoot Java programs leveraging conditional expressions and IF-ELSE statements.
Designing
Ability to design Java programs leveraging conditional expressions and IF-ELSE statements.

  • A01 - Is this number divisible by 5 or 6?
  • Video Link: YouTube
  • This exercise was #3.3 in the 10th edition of our text. Since it is no longer available as of the 11th edition, please see description below. Please note that we are using a "trick" to generate random numbers in this video. Feel free to use instead the Math.random method described in the latest edition of the textbook. You need to write a Java program which will read an integer value from the user. You will then display three different messages depending on whether it is
    • Divisible by 5 AND 6
    • Divisible by 5 OR 6
    • Divisible by 5 or 6 but not both
  • A02 - Adddition Tutor with 2 numbers
  • Video Link: YouTube
  • This exercise was #3.2 in the 10th edition of our text; it was #3.4 in older editions of our text. Since it is no longer available as of the 11th edition, please see description below. Write a Java program which will display two random positive integers between 0 and 100. You will then prompt the user to enter the result of their sum, check that their guess is right, then display a message indicating whether they guessed right or not.
  • A03 - Addition Tutor with 3 numbers
  • Video Link: YouTube
  • This exercise is #3.2 in the 11th edition of our text, it was #3.5 in the 10th edition. Improve the above program by allowing it to show the user three single-digit random numbers and prompt him or her to enter their sum.
    ERRATA - Lines 13 and 18 of the solution use the variable nb2 twice. They should instead use nb3 also. Thank you Patrick Brett for pointing this out.
  • A04 - Sort 3 Integers
  • Video Link: YouTube
  • This exercise is #3.8 in the 11th & 10th editions of our text. Read three integers from the user, and display them back in ascending order.
  • A05 - Compute the perimeter of a triangle
  • Video Link: YouTube
  • This exercise is #3.19 in the latest edition of our text; it was #3.9 in older editions of our text. Read three integers from the user, determine whether they represent the lengths of the sides of a triangle. If so, use them to compute the perimeter of this triangle. Otherwise, display an error message. Reminder - the values represent the lengths of the sides of a triangle if the sum of any two sides is greater than the lenght of the third side.
  • A06 - Number of days in a month / year
  • Video Link: YouTube
  • This exercise was #3.11 in the 10th edition of our text. Since it is no longer available as of the 11th edition, please see description below. Write a program that prompts the user to enter the month and years and displays the number of days in the month. For example; month 2 and year 2012 had 29 days. Month 3 and year 2015 had 31 days.

Each of the following links will lead you to a separate tutor applet. The tutors take usually 30 minutes to work through.

  • Problet #1 - Expression Evaluation - Relational
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to practice evaluating Java conditional expressions.
  • Problet #2 - Expression Evaluation - Logical
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to practice evaluating Java conditional expressions.
  • Problet #3 - Selection Statement - IF/IF-ELSE
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to practice evaluating use of Java IF-ELSE statements.

The following exercises are short exercises which are hosted on Nick Parlante's CodingBat website, previously known as JavaBat. His website allows you to work on Java exercises directly in your web browser and get your solutions to be automatically evaluated by running them against a series of tests. A report shows you which tests your solution passed and which tests it failed. This helps you improve your solution until you get it right.

As a reminder from our "Welcome Aboard" section, here is the link you used during week #1 to test that your system was able to run CodingBat practice exercises.

  • Test CodingBat.com
  • URL Link: http://codingbat.com/prob/p116624
  • Use any recent browser to go to the above URL. Modify the code provided to you so that it reads as follows;
    public int diff21(int n) {
      return 0;
    }
    When you are ready, click the "go" button. A table with a list of mostly failed tests will show to the left of your program.

The following video will show you how to work with CodingBat exercises;

  • How to work on CodingBat / JavaBat
  • Video Link: YouTube
  • Our textbook introduces Java methods later in the semester. However, JavaBat exercises work by assuming that you are writing simple methods from the get go. This video is meant to supplement the textbook by providing you with the information you need in order to be comfortable while working on the JavaBat website. We will just introduce enough knowledge about Java methods for you to be able to understand how you would implement these JavaBat exercises in the environment we use for all our other exercises & examples. The video will also show you an example of a CodingBat session.

While Problets uses a single link, specific to this course & semester, the JavaBat links below will point you directly to the exercises assigned for this module. Make sure that you login with your CodingBat account every time you practice, in order to get credit for your efforts. Refer to your instructor's announcements for details on how and when to submit proof of your practice to get credit.

Let us start with a few warm-up exercises which should be familiar to you.

  • JB1 - Palo Alto Squirrels
  • URL Link: Squirrel Play
  • They are back! Let us revisit the Palo Alto Squirrels problems in Java. The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean isSummer, return true if the squirrels play and false otherwise.
  • JB2 - Palo Alto Squirrels Party
  • URL Link: Squirrel Party
  • When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.
  • JB3 - Speeding Ticket
  • URL Link: Speeding Ticket
  • You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.

The following exercises are still based on simple conditional expressions but are new to this module.

  • JB4 - Summing Dice Rolls without Doubles
  • URL Link: withoutDoubles
  • Return the sum of two 6-sided dice rolls, each in the range 1..6. However, if noDoubles is true, if the two dice show the same value, increment one die to the next value, wrapping around to 1 if its value was 6.
  • JB5 - Are Three Integers in order?
  • URL Link: inOrderEqual
  • Given three ints, a b c, return true if they are in strict increasing order, such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.
  • JB6 - Are Two Integers Sharing a Digit?
  • URL Link: shareDigit
  • Given two ints, each in the range 10..99, return true if there is a digit that appears in both numbers, such as the 2 in 12 and 23. (Note: division, e.g. n/10, gives the left digit while the % "mod" n%10 gives the right digit.)

The following exercises explore more elaborate conditional expressions to be used in IF statements.

  • JB7 - Make Bricks
  • URL Link: makeBricks
  • We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks.
  • JB8 - Are two Integers respectively close & far from a Third one?
  • URL Link: closeFar
  • Given three ints, a b c, return true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.
  • JB9 - Lucky Sum
  • URL Link: luckySum
  • Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
  • B01 - Solve Quadratic Equations
  • This exercise is #3.1 in the 11th & 10th editions of our text.
  • B02 - Random Month
  • This exercise is #3.4 in the 11th & 10th editions of our text.
  • B03 - Finances: Monetary Units
  • This exercise is #3.7 in the 11th & 10th editions of our text & is based on Listing #2.10.
  • B04 - Game: Addition Tutor
  • This exercise is #3.10 in the 11th & 10th editions of our text.
  • B05 - Palindrome Numbers
  • This exercise is #3.12 in the 11th & 10th editions of our text.
  • B06 - Finances: Taxes
  • This exercise is #3.13 in the 11th & 10th editions of our text.