Java Bootcamp – Workbooks and Challenges

Dealership Workbook – Part 2

A "bulletproof" application is free of bugs and doesn't fail/crash no matter what. The key is to:

  • catch checked exceptions (outside the application's control).

  • fix unchecked exceptions (resulting from badly written code).

You already caught the checked exceptions. Now, you will fix unchecked exceptions.

Task 1 – InputMismatchException

Thrown by Scanner if the user enters a value that it wasn't expecting.

You need to prevent the application from crashing. An unchecked exception implies that there's something wrong/missing in your code.

If you run the starter code, it prompts the user to enter a spot:

But if the user enters a type mismatch, the application crashes.

In the event of a type mismatch: you need to:

  1. pick up the mismatch using nextLine().

  2. println: "INVALID INPUT."

  3. restart the while loop.

Expected Output:

Task 2 – Fix the bug

A bug is code that doesn't behave the way it's supposed to.

If you enter a valid number (0 or 1) followed by a mismatch, your app will present a bug:

Your task is to fix the bug. Use breakpoints to visualize the runtime and identify the bug.

Hint: the bug is the classic 'nextLine' trap.

Task 3 – ArrayIndexOutOfBoundsException

Thrown if you index outside the bounds of the array.

If the user enters an index that's out of range, the application will crash.

You can't let the application crash. If the index is out of bounds:

  • println: "Please choose a valid parking spot."

  • restart the while loop.

Don't use the array from main() to get the length. Define a method in Dealership named getLength() and call it from main(). Then, test your condition by entering spots of -1, 2, and 5.

Expected Output:

Task 4 – NullPointerException

Thrown if you try to access something (dot syntax) from a null.

If the user tries to buy a car from an empty spot, the application throws a NullPointerException.

If the user tries to buy a car from an empty spot:

  • println("Spot " + <spot> + " is empty.")

  • restart the while loop.

Hint: You'll need to fix the getter in Use the conditional assignment syntax (ternary operators ? and : ) in place of if-else.

Expected Output:

Task 5 – Check if the user wants to continue.

At the end of the while loop, ask the user:

  • Type "yes" to continue shopping.

  • If they type anything else, break the loop.

Task 6 – break if dealership isEmpty.

If you haven't already, add an isEmpty() method to the Dealership class.

/** * Name: isEmpty * * @return (boolean) * Inside the function: * - return true if there are no more cars. * - return false otherwise. * */

Call the isEmpty method from main(). If the result is true:

  • println: "We're all sold out!".

  • break the loop.

Expected output:

Task 7 – scan.close()

scan.close() is now reachable. You can uncomment it.

The application is now bulletproof!

No matter what the user throws at you, the application is free of bugs and will never crash. It can process any input and respond gracefully.

Feedback Summary
39 students





Written Reviews
There are no written reviews yet.