Loading...
Loading...
A college portal stores each user's password but must never show it. A new password needs at least 8 characters and at least one digit, and it must differ from the old one.
Write a class UserProfile with private username and password. Give it getUsername(), boolean login(String attempt) and String changePassword(String oldPw, String newPw) that returns the result message. There is no getPassword().
login pw or change oldPw newPw. Passwords have no spaces.login: WELCOME username or LOGIN FAILED. For change, check in this order: WRONG PASSWORD, TOO SHORT, NEEDS A DIGIT, SAME AS OLD, otherwise PASSWORD CHANGED.Example
Input
asha secret123 4 login secret12 change secret123 abc1 change secret123 newpass99 login newpass99
Output
LOGIN FAILED TOO SHORT PASSWORD CHANGED WELCOME asha
abc1 is too short; after the change only the new password works.
Try the problem first. Open one hint at a time.
Without a getter for password, the only way to use it is inside the class, in login and changePassword.
Compare Strings with equals, never ==.
Loop over the new password with Character.isDigit(ch) to find a digit.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor