Incorrect Parentheses

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

No, you can't buy the car (at least that is what the incorrect boolean expression says.)

Incorrect Parentheses

You have $50,000 in cash, $100,000 of credit, and $3,000 of debt. Here is how the incorrect expression is evaluated:

( cash >= 25000  ||  credit >= 25000 ) && debt < 1000 

(      true      ||  credit >= 25000)  && debt < 1000

(               true                )  && debt < 1000

                true                   &&     false

                                      false

The correctly grouped expression,

cash >= 25000  ||  ( credit >= 25000 && debt < 1000 )

immediately evaluates to true.

Here is another problem: a program screens job applicants. An applicant is accepted for an interview only if the applicant meets two conditions:

  • 4 or more years of college, AND
  • 2 years experience programming in Java OR a grade point average greater than 3.5.

QUESTION 15:

Put parentheses in this boolean expression so that it correctly tests applicants:

if (   college >= 4   &&   experience   >= 2   ||    gpa > 3.5   )

  System.out.println("Interview applicant");

else

  System.out.println("Send resume to circular file.");