Car Purchase Decision

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.

Car Purchase Decision

You need money OR credit. Just one would do. Of course, if you had lots of money and plenty of credit you could certainly buy the car.

Sometimes a program has to test if just one of the conditions has been met. Here is how that is done with the car purchase problem:


How much cash do you have?   

How much credit do you have?  

  if ( cash>=25000 ||  credit>=25000 )
    System.out.println("Enough to buy this car!");
  else
    System.out.println("Have you considered a Yugo?");
  


The symbol || (vertical-bar vertical-bar) means OR. On your keyboard, vertical-bar is the top character on the key above the "enter" key. The OR operator evaluates to true when either qualification is met or when both are met. The if statement asks a question with two parts:

if ( cash >= 25000 || credit >= 25000  )
     -------------      -------------
       cash part         credit part

If either part is true, or both parts are true, then the entire boolean expression is true.

QUESTION 17:

Say that you enter 56000 for cash and 0 for credit. What answer (true or false) does each part give you?

               
cash  >= 25000  

credit >= 25000  

What does the entire boolean expression give you?

               
cash  >= 25000 || credit >= 25000