Exception Handling (Low Difficulty)
Create the following classes that simply extend the
Account class, code a method called
createAccount that takes a
confirmPassword as parameters.
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.
main method (within the
- 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
createAccountmethod continuously with the user's input and use
catchto 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.
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!
Exception Handling (High Difficulty)
Create a class called
BankAccount (a separate class from your main program) that contains a
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
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
Create at least 2 instances of
BankAccount. Deposit, withdraw, and display the final balance of each.
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
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.
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.
Add a page link block here (if there is no next section, delete this column)
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 firstname.lastname@example.org for inquiries.