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

Your first task is to create a method called CreateAccount. The method will ask

ID: 3682617 • Letter: Y

Question


Your first task is to create a method called CreateAccount. The method will ask a user to input their username and their password. Assume password is valid if it's number between 500 and 600. If the password is incorrect, the system should ask the used to input another password (assume this can be done forever). The method will return the credentials of the user (username and password) as a concatenated string separated by comma. For example, if the username is "mary" and the password is "503", the CreateAccount method will return "mary,503". Write the pseudocode for the method CreateAccount. Write the java code for the method CreateAccount. Assume the user enters 100, 200, and 503. Trace how your system would work for these three inputs.

Explanation / Answer

import java.util.Scanner;

/**
* @author srinu
*
*/
public class CreateAccount {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // calling the method and printing the result
       System.out.println(CreateAccount());
   }

   /**
   *
   * method to read user name and valid password and return the result
   *
   * @return
   */
   public static String CreateAccount() {
       // declaring the variables
       Scanner scanner = null;
       String userName = null;
       int password = 0;
       try {
           // initializing the scanner
           scanner = new Scanner(System.in);

           // prompt the user to enter user name
           System.out.print("Enter the User Name:");
           // reading username from console
           userName = scanner.next();
           // infinite loop for reading valid password
           do {
               // prompt the user to enter password
               System.out.print("Enter the Password:");
               // reading password from console
               password = scanner.nextInt();
               // check the password is between 500 and 600
               // if it is true break otherwise continue the loop to read the
               // password
               if (password > 500 && password < 600)
                   break;
               else
                   System.out.println("Try Again!");
           } while (true);

       } catch (Exception e) {
           // TOeDO: handle exception
       } finally {
           scanner.close();
       }
       // return the result
       return userName + "," + password;

   }
}

OUTPUT:

Enter the User Name:mary
Enter the Password:100
Try Again!
Enter the Password:200
Try Again!
Enter the Password:502
mary,502

NOTE:

code is well comented wich keeps the pseudocode at comments in the method,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote