Java Student Project         


PickEmployee
Ability Level: Intermediate  
Estimated Time: 50 minutes
Objectives:
  • Learn how to use the AND, OR and NOT logical operators.
  • Be able to use the switch statement for decisions.
  • Be able to use the conditional operator.
  • Learn about operator precedence in Java.
Materials & Resources:
  • Java Software
  • Textbook
Overview: Write a program that asks a user to input an initial.  Display the full name of an employee who matches the initial: A is Armando, B is Bruno, and Z is Zachary.  All other entries should cause a "No such employee" message to display.  The class name is PickEmployee.
Instructions:  See the notes in overview above.
Hints: Some important points to keep in mind in doing this project:
  • A common use of the OR operator is to decided to take action whether a character variable is uppercase or lowercase, as in if(selection == 'A' || selection == 'a') ... The subsequent action occurs whether the selection variable holds an uppercase or lowercase A.
  • As long as you are dealing with whole dollar amounts, the expression if(saleAmount > 1000) can be expressed just as well as if(saleAmount >= 1001).  Addionally, if(1000 < saleAmount) and if(1001 <= saleAmount) have the same meaning.  Use whichever has clearest meaning to you.
  • You can place and indent the if following an else, but a program with many nested if...else combinations soon grows very long and "deep," and with indentations, later statements in the nest would move farther and farther to the right on the page.  For easier-to-read code, Java programmers commonly place each else and its subsequent if on the same line.
  • Within a nested if...else, it is most efficient to ask the most likely question first.  In other words, if you know that most saleAmount values are over 1000, compare saleAmount to that value first.  If, however, you know that most saleAmounts are small, you should ask if(saleAmount <= 500) first.
  • Remember that the Event constructor that you created earlier in this chapter requires both a character and a double argument.
  • You are not required to list the case values in ascending order.  It is most efficient to list the most common case first, instead of the case with the lowest value.
  • Remember from Chapter 1 that characters are actually stored as integers.  That is why they are allowed as the case variables in a switch statement.
  • Even though the && is evaluated first in the expression age < 25 && gender == 'M' || tickets > 3, there is no harm in adding extra parentheses as in (age < 25 && gender == 'M') || tickets > 3.  The outcome is the same, but the intent is clearer to someone reading your code.
Extra: Check for lower and upper case letters.