7️⃣

14.7 Practice

 

Practice

Account

🚧
Exception Handling (Low Difficulty)
Create the following classes that simply extend the Exception class:
  • InvalidUsernameException
  • InvalidPasswordException
  • InvalidAgeException
  • PasswordMismatchException
 
In the Account class, code a method called createAccount that takes a username, age, password, and confirmPassword as parameters.
 
In createAccount, throw an:
  • InvalidUsernameException - if the given username is <4 or >10 characters.
  • InvalidPasswordException - if the given password is <4 or >10 characters.
  • InvalidAgeException - if the given age is <18.
  • PasswordMismatchException - if the given password does not match the given confirm password.
 
In your main method (within the Account class):
  • To simulate the creation of an account online, prompt the user to enter their username, age, password, and their password again (to confirm).
  • Call the createAccount method continuously with the user's input and use try/catch to handle the possible exceptions and print the appropriate error message. (You can assume that the user input will only throw at most 1 exception.)
  • Keep asking the user for input until the account is created successfully.
 
Example output:
Welcome to Account Creation! Enter username (4 to 10 characters): hi Enter age: 18 Enter password (4 to 10 characters): thisislong Confirm password (4 to 10 characters): thisislong Invalid username. Enter username (4 to 10 characters): four Enter age: 18 Enter password (4 to 10 characters): thisislong Confirm password (4 to 10 characters): thisislong Account created successfully!
 

Bank

🚧
Exception Handling (High Difficulty)
Create a class called BankAccount (a separate class from your main program) that contains a private balance attribute for the amount of money in the account.
 
The user should be able to set the original balance upon initialization (through the constructor).
 
The class should have deposit and withdraw methods that add and subtract money from the account. There should also be a getter method to access the balance, since the attribute is private.
 
Create at least 2 instances of BankAccount. Deposit, withdraw, and display the final balance of each.
 
Create a NotEnoughMoneyException class that is a checked exception. The withdraw method should throw this exception if the user tries to withdraw an amount that is greater than their current balance. Handle this exception by displaying the difference between the balance and the amount to be withdrawn, and do this exception handling in the main program where you have created instances of BankAccount.
 

Lexicographical Order

📁
File I/O (Medium Difficulty)
Use File I/O to compare two files lexicographically. Lexicographical order is very similar to alphabetical order, except that it includes more than just lowercase alphabets. Given two text files f1 and f2, write a program that will compare their contents and output to the console the title of the file which comes first lexicographically.
 
💡
Hint: Java’s String.compareTo method may be of use to you.

Average

📁
File I/O (High Difficulty)
The file “numbers.txt” has 100 random numbers (one on each line). Use file i/o to find the average of these numbers.
 
Write “Average: ” and display the average on the next (101st) line of the file.Hint: Just like BufferedReader and FileReader, there are BufferedWriter and FileWriter classes that allow you to add full words to files, instead of just bytes.
 
You should do a quick Google search to explore these classes first, but just to get you started: new FileWriter(“fileName.txt”, true)will create an instance of FileWriter, and the “true” puts it in append mode, so you start writing to the end of the file instead of writing over the existing content from the beginning.
 

Next Section

⁉️
Ch. 14 Quiz
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.