Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must be written in Java: Write a small program that allows a user to change his

ID: 667301 • Letter: M

Question

Must be written in Java:

Write a small program that allows a user to change his or her password using the Console class. As in the example in the text, assume that there is a method validateLogin already created that validates the login, but simply returns true. Also assume there is a method resetPassword that would reset the password in the system, but for now, simply returns true. Your program should require a new password to have at least six characters with at least one character that is not a letter (upper case or lower case). Also, be sure to prompt the user for the password twice and make sure the responses match. If they don't match, the user should start the process of entering a new password again. This reduces the possibility that the user mistypes the new password. Remember, you may need to run this program from a command window.

Explanation / Answer

Please find the required solution:

import java.io.Console;

public class PasswordValidation {
   public static boolean validateLogin ()
   {
       return true;
   }

   public static boolean resetPassword ()
   {
       return true;
   }

   public static String getPassword ( Console console )
   {
       char ch[] = console.readPassword();
       String password = new String( ch );
       return password;
   }

   public static boolean allAlphaNumeric ( String password )
   {
       for ( int i = 0; i < password.length(); i++ )
       {
           if ( !Character.isAlphabetic( password.charAt( i ) ) )
               return false;
       }
       return true;
   }

   public static void main ( String[] args )
   {
       Console console = System.console();
       System.out.println( console );

       String password, reEnterPassword;
       boolean cont = false;
       do
       {
           password = getPassword( console );
           System.out.println( password );
           if ( password.length() < 6 )
           {
               System.out.println( "Password length is minimum 6" );
               cont = true;
           }
           else if ( allAlphaNumeric( password ) )
           {
               System.out.println( "Password should contain atleast one non-digit" );
               cont = true;
           }
           else
               cont=false;
       } while ( cont );
       do
       {
           System.out.print( "Reenter Password:" );
           reEnterPassword = getPassword( console );
           if ( !password.equals( reEnterPassword ) )
           {
               System.out.println( "Password does not match" );
           }
       } while ( !password.equals( reEnterPassword ) );
       System.out.println("Success");
   }
}