CISC 1115
Introduction to Programming Using Java
Lab #6
Logical Operators

How to Develop and Submit your Labs

Lab 6.1 — Vaccination — Take 2 (Vaccination) (Approval)

Modify Program 6.1 to use the && operator instead of the || operator

Sample Test Run #1

Enter patient's age: 2
***** Do NOT ADMINISTER VACCINE *******

Sample Test Run #2

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter patient's age: 3
Go ahead and stick it to 'em

Lab 6.2 — Vaccination — Take 3 (Vaccination)

Modify Program 6.1 (or Lab 6.1) so that the gender of the patient (M/F) is entered, and taken into account, as well. The vaccine should NOT be administered to those patients who match one of the categories in the following table ( a '-' means that no distinction is made for that column):

Age Gender
Under 3 -
20-45 F
Over 65 -

Sample Test Run #1

Enter patient's age: 2
Enter patient's gender: M
***** Do NOT ADMINISTER VACCINE *******

Sample Test Run #2

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter patient's age: 64
Enter patient's gender: F
Go ahead and stick it to 'em

Lab 6.3 — Divisibility Again (Divisibility)

Write a program containing a method boolean divisibleBy17(int n) that returns true if n is divisible by 17 and falseotherwise. Your main should prompt the user for an integer, pass it to the method and print the returned boolean value. (This is a variation of Program 3.1).

Sample Test Run #1

Enter number for which to check divisibility by 17: 17
17 is divisible by 17

Sample Test Run #2

Enter number for which to check divisibility by 17: 15
15 is not divisible by 17

Lab 6.4 — Divisibility Again — Take 2 (Divisibility)

Rewrite Lab 6.4 so that the method is now boolean isDivisible(int n, int m). isDivisible returns true if n is divisible by m. Again your main should read in the two numbers, pass them to the method and print the result.

Sample Test Run #1

Enter number to be checked: 10
Enter number to be divided by: 5
10 is divisible by 5

Sample Test Run #2

Enter number to be checked: 3
Enter number to be divided by: 2
3 is not divisible by 2

  • A more general version of the method of the previous lab … this one tests for general divisibility of one number by another, not just by 17.

Lab 6.5 — Evenness (Evenness) (Approval)

Write a program that contains two methods: Your main should prompt for the number, pass it (to isEven, and print the result.

Sample Test Run #1

Enter number to be checked: 12
12 is even

Sample Test Run #2

  • In this exercise you are using the logic of one method (isDivisible) to facilitate the writing of another method (isEven).
    • This is known as leveraging; i.e., the existence of the first method makes the writing of the second method easier.
      • But it's more than simply easier; assuming you have tested the first method, if most of the work of the second method is shunted to that first method, the second method will be more repliable as well.
    • In this particular case, the logic of neither method is all that involved (i.e., even without relying on isDivisible, isEven would be fairly easy to code), but this is meant as an introductory example.

Lab 6.6 — Evenness - Take 2 (Evenness) (Approval)

Rewrite Lab 6.5, but this time, assume the method isDivisible already exists and is in the class Utilities

Sample Test Run #1

Enter number to be checked: 12
12 is even

Sample Test Run #2

  • Some practice calling methods from outside the class
    • We've done this before; this is no different than our calling the sqrt methof of the Math class
      • isDivisible is invoked the same way as Math.sqrt, expcept it class name is Utilities

Lab 6.7 — Fun with Numbers (InfoOf3) (Approval)

Write a program that reads three numbers from the keyboard, and then prints out some information about the relationships between the numbers-- whether they are all equal, in ascending order, in descending order, and so on. To determine these relationships, write the following boolean-valued functions: Try to "talk boolean"

Sample Test Run

first number? 1
second number? 2
third number? 3
allAreEqual: false
twoAreEqual: false
noneAreEqual: true
areAscending: true
areDescending: false
strictlyAscending: true
strictlyDescending: false