You have $240 on hand and no credit. Can you buy the $25,000 Miata?

A good answer might be:

No.

Car Purchase as a Program

All you needed was 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 have been met. Here is a program that does that with the car purchase problem:

// Sports Car Purchase
//    New $25,000 red Miata sports car.
//    You need cash or credit.
//
import java.io.*;
class HotWheels
{
  public static void main (String[] args) throws IOException
  { 
    BufferedReader stdin = 
        new BufferedReader ( new InputStreamReader( System.in ) );
 
    String inData;
    int    cash, credit ; 

    // get the cash
    System.out.println("How much cash?");
    inData   = stdin.readLine();
    cash    = Integer.parseInt( inData ); 

    // get the credit line
    System.out.println("How much credit do you have?");
    inData   = stdin.readLine();
    credit   = Integer.parseInt( inData ); 

    // check that at least one qualification is met
    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." It evaluates to true when either qualification is met (or both are met.) The if statement is asking a question with two parts:

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

Each one of these parts is a relational expression (just as with previous programs in this chapter.) A relational expression looks at two numbers and gives you true or false.

QUESTION 15:

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

               
cash   >= 25000  ________________

credit >= 25000  ________________