Sports Car Revisited

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:

Math.sin( 0.5 ) >= Math.tan( 0.2 ) || 43.259 / 12.073 > 3.5 || ( 100 > 45 )

There are no side-effects here, so it is safe to look at the last part:

( 100 > 45 )

and realize that the entire expression will be true. Java (working from left to right) might have found a different true, but in the absence of side-effects that doesn't matter. The entire expression is true.

Sports Car Revisited

Math functions, such as Math.sin() and Math.cos(), do not have side effects. They are safe to use in boolean expressions.

Say that you have not yet given up on buying that $25,000 red Miata sports car. The sales manager of the car lot will let you buy the car if:

  • You have at least $25,000 in cash, OR
  • You have at least $25,000 in credit AND no outstanding debts.

The logic of this situation is slightly complicated. Here are several situations:

  • You have $30,000 in cash, no credit and massive debts.     You get the car.
  • You have $1,000 in cash, $50,000 in credit and massive debts.     You DON'T get the car.
  • You have $1,000 in cash, $10,000 in credit and no debts.     You DON'T get the car.
  • You have $1,000 in cash, $30,000 in credit and no debts.     You get the car.

Notice that it is not enough to merely have a high credit line. To buy the car on credit you must also be free of debt.

QUESTION 12:

You have $25,000 in cash, $50,000 in credit and massive debts. Do you get the car?