Program Logic

Unit 6 Index

Multiple Boolean Example

Example 4: Multiple Boolean Expressions Algorithm

Problem Statement

A program is required that determines if someone gets a driver's license. The user is prompted for the age of the applicant, if the applicant passed the written exam, and if they passed the behind-the-wheel exam. The applicant is given a driver's license if they are at least 16 and they passed both exams.

1. Nouns and Verbs

Identify the nouns and verbs in the problem statement.

PROBLEM STATEMENT:
    A program is required that determines if someone gets a
    driver's license. The user is prompted for the age of
    the applicant, if the applicant passed the written exam,
    and if they passed the behind-the-wheel exam. The
    applicant is given a driver's license if they are at
    least 16 and they passed both exams. Output a condolence
    if they don't get their license.

Nouns: driver's license, age, written exam, behind-the-wheel exam, condolence

Verbs: determines, prompted, passed, given

2. Defining Diagram

Convert the nouns and verbs into variable names and processes and create a defining diagram.

INPUTS:
  age
  writtenExamResults
  behindTheWheelExamResults

PROCESSING:
  prompt for age
  prompt for written exam results
  prompt for behind-the-wheel results
  award driver's license if all inputs are okay

OUTPUTS:
  driversLicense
  condolence

3. Solution Algorithm

Create a solution algorithm based on the defining diagram.

public Constructor(){
  call driversLicense();
}

private method driversLicense(){
  declare and initialize int age that will hold end-user input
  declare and initialize String writtenExamResults that will hold end-user input
  declare and initialize String behindTheWheelExamResults that will hold end-user input

  instantiate Scanner object using input as its variable

  print to the console "Please enter the applicants age"
  validate input using hasNextInt()
  
  IF (age > 15){
  
     print to the console "Did applicant pass the written exam? Y/N"
     save the input.next() value into the writtenExamResults variable

     print to the console "Did applicant pass the behind-the-wheel exam? Y/N"
     save the input.next() value into the behindTheWheelExamResults variable

     IF (writtenExamResults is "Y" AND behindTheWheelExamResults is "Y"){
       display "Congratulations, here is your driver's license!"
     }
     ELSE{
       display  "We are sorry, you did not pass. Please retake the test again in 30 days"
     }

  }//ending curly brace for the outer if (age > 15)
  ELSE{
     display "Sorry, you are not old enough to drive. Good-bye".
  }  
}

4. Test Plan

Create a test plan for the algorithm.

VARIABLES: age, writtenExamResults, behindTheWheelExamResults,


TEST CASE 1:

  INPUT VALUES: 16, Y, Y

  EXPECTED RESULT: "Congratulations, here is your driver's license!"

  ACTUAL RESULT: "Congratulations, here is your driver's license!"


TEST CASE 2:

  INPUT VALUES: 15

  EXPECTED RESULT:  Sorry, you are not old enough to drive. Good-bye.

  ACTUAL RESULT:  Sorry, you are not old enough to drive. Good-bye.

TEST CASE 3:

  INPUT VALUES: 17, Y, N

  EXPECTED RESULT:  We are sorry, you did not pass. Please retake the test again in 30 days

  ACTUAL RESULT: We are sorry, you did not pass. Please retake the test again in 30 days

5. Java Code

Write the Java source code based on the solution algorithm. Test your code using the test plan.

Below is a Java code snippet. A working Java solution must also include a class, constructor, as well as the main method and necessary import statements. Please study the code. Better yet, create a working Java project in NetBeans.

REALLY IMPORTANT! The hasNextInt() method is to get you used to end-user validation. As is, the results are not what you may expect if the end-user does not enter valid data. Also, this code does not give the end-user the opportunity to try again. These details were left out in the interest of clarity for the topic at hand...Decision Structures.

Study the Java code below. Notice I nested all the processing code inside of the first if statement. This is because there is no reason continue if the end-user did not enter a valid number in the beginning. Then notice I decide whether to continue based on the value of age.


// Driver's License
   private void driversLicense() {
    // Declare variables
	int age = 0;
	String behindTheWheelExamResults = "";
	String writtenExamResults = "";
	Scanner input = new Scanner(System.in);

	// Get data from user
	System.out.println("Please enter the applicant's age.");
        
        if(input.hasNextInt()){
            
            age = input.nextInt();
			
            if(age > 15){ 
                System.out.println("Did applicant pass the written exam? Y/N");
                writtenExamResults = input.next();

                System.out.println("Did applicant pass the behind-the-wheel exam? Y/N");
                behindTheWheelExamResults = input.next();
				
                //Use a multiple Boolean expression to 
                //determine if the applicant gets their license
                if (writtenExamResults.equalsIgnoreCase("Y")
                                && behindTheWheelExamResults.equalsIgnoreCase("Y")) {

                        System.out.println("Congratulations, here is your driver's license!");
                }
                else {
                        System.out.println("We are sorry, you did not pass. Please retake "
                                        + "the test again in 30 days.");
                }
                                   
            }//end: if age > 15
            else{
                System.out.println("Sorry, you are not old enough to drive. Good-bye.");
                System.exit(0);
            }//end: else for age > 15
            
        }//end if hasNextInt
        else{
            System.out.println("Sorry, your age is not valid. Good-bye.");
            System.exit(0);
        }
      
    }//end of driversLicense()

Here is the output if age >= 16, behindTheWheelExamResults equals "Y" and writtenExamResults equals "Y":


Think about it

You cannot be sure if the end-user will type uppercase or lowercase letters. We handled that uncertainty by using the Java syntax .equalsIngoreCase("Y"). Remember, .equalsIngoreCase() is a built in Java library method.

What would happen if the end-user typed in the letter Z instead of a Y or N?