Car Buying Program

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:

Yes.

Car Buying Program

The sales manager of the car lot will let you buy the car if:

  • You have $25,000 in cash, OR
  • You have $25,000 in credit and no outstanding debts.

Say that "outstanding debts" means debts more than $1,000. Here is a program that makes the car buying decision:

// Sports Car Purchase
//    New $25,000 red Miata sports car.
//    You need cash or credit with no debts .
//
import java.util.Scanner;
class HotWheels2
{
  public static void main (String[] args)
  { 
    Scanner scan = new Scanner( System.in );
 
    String inData;
    int    cash, credit, debt ; 

    // get the cash
    System.out.println("How much cash?");
    cash    = scan.nextInt();

    // get the credit line
    System.out.println("How much credit do you have?");
    credit   = scan.nextInt(); 

    // determine the debts
    System.out.println("How much much do you owe?");
    debt     = scan.nextInt();

    // check that at least one qualification is met
    if ( cash >= 25000  ||  ( credit >= 25000 && debt < 1000 ) )
      System.out.println("Enough to buy this car!" );
    else
      System.out.println("Have you considered a Yugo?" );

  }
}

The boolean expression of the if statement correctly implements the car buying rules. The expression includes both && and ||.

QUESTION 13:

You have zero dollars, $26,000 in credit and $500 in debts. Do you get the car?