Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The first portion contains only alphanumeric characters, periods, and dashes. Moreover, a period or a dash must always be followed by one or more
The first portion contains only alphanumeric characters, periods, and dashes. Moreover, a period or a dash must always be followed by one or more alphanumeric characters. Finally, the first and last character must be alphanumeric. The second portion contains only letters of the alphabet. Examples of valid domains are: "mail.cc", "mail-archive.com", "mail.org", "mail.mcgill.ca" (here the first portion of the domain is "mail.mcgill", while "ca" is the second portion) Examples of invalid domains are: "mail.c", "mailllarchive.com", "mail", "mail.com", ".com", "mail.c9". To complete your task, you need to implement all the methods listed below. All the code for this question must be placed in the file named EmailValidation.java. Note that you are free to write additional methods if they help the design or readability of your code. 1a) Method to check if a character is alphanumeric Write a method isAlphanumeric() that takes as input a character. The method returns true if such character is a letter of the English alphabet (uppercase or lower case) or one of the arabic numerals. The method returns false otherwise. For example: isAlphanumeric('g') returns true isAlphanumeric('B') returns true isAlphanumeric('3') returns true isAlphanumeric(-) returns false 1b) Methods to check is a character is a valid prefix/domain character Write the following two methods: A method isValidPrefixChar() that takes as input a character and returns true if the character can be used in the prefix of a valid email address, false otherwise. Note that a valid prefix can contain only alphanumeric characters, dashes, periods, or underscores. For example, isValidPrefixChar('_') returns true, while isValidPrefixChar('&") returns false. A method isValidDomainChar() that takes as input a character and returns true if the character can be used in the domain (first portion) of a valid email address, false otherwise. Note that a valid first portion of a domain can contain only alphanumeric characters, dashes, or periods. For example, isValidDomainChar("-") returns true, while isValidDomainChar('') returns false. To get full marks, your method must use the isAlphanumeric() method defined above. 1c) Method to check if a String contains exactly one '@ Write a method exactlyOneAt() that takes as input a String representing a possible email address, and returns true if the string contains exactly one '@', false otherwise. For example: exactlyOneAt("example@email.com") returns true. exactlyOneAt("b@n@n@s") returns false exactlyOneAt("@pple") returns true 1d) Method to get the prefix of a possible email address Page 6 Write a method getPrefix() that takes as input a String representing a possible email address. The method returns a String containing the prefix of the possible email address. In this method, you can assume that the String received as input contains exactly one '@'. For example: getPrefix("example@email.com") returns "example". getPrefix("cats @nd dogs") returns "cats". getPrefix("@pple") returns "". 1e) Method to get the domain of a possible email address Write a method getDomain() that takes as input a String representing a possible email address. The method returns a String containing the domain of the possible email address. In this method, you can assume that the String received as input contains exactly one '@'. For example: getDomain("example@email.com") getDomain("cats @nd dogs") returns "nd dogs". getDomain("@pple") returns "pple". returns "email.com". 1f) Methods to check if the prefix and the domain are valid Write the following two methods: isValidPrefix() takes a String as input representing the prefix of a possible email address. The method returns true if the input adhere to all the constraints listed in the above paragraph titled "Acceptable prefix formats*, false otherwise. isValidDomain() takes a String as input representing the domain of a possible email address. The method returns true if the input adhere to all the constraints listed in the above paragraph titled "Acceptable domain formats", false otherwise. Examples: isValidPrefix("abc_def") returns true. isValidPrefix("mail.cc") returns true. isValidPrefix("abc..d") returns false. isValidPrefix("abcd") returns false. isValidDomain("mail.cc") returns true. isValidDomain("abc-def.ghi") returns true. isValidDomain("abc..d") returns false. isValidDomain(".com") returns false. isValidDomain(".com.com") returns false. To get full marks, your method must use at least isValidPrefixChar() and isValidDomainChar(). 1g) Methods to check if a string is a valid email address Write the method isValidEmail() which takes as input a String and returns true if the string is a valid email address, false otherwise. To get full marks, your method must use all the methods you have written up to now (either directly or indirectly). For example: isValidEmail("abc..def@mail.com") isValidEmail("abcdef@mail.com") isValidEmail("abc.def@mail") returns false. isValidEmail("abc.def@mail..com") isValidEmail("abc_d@mail.com") isValidEmail("abc.def@mail.com") isValidEmail("abc@mail.com") returns true. isValidEmail("abc.def@mail-archive.com") isValidEmail("karan.sharma@mail.mcgill.ca") returns false. returns false. returns false. returns true. returns true. returns true. returns true. For this question, you will write a Java program that helps validate email addresses. Email addresses are often requested as input to websites as a way to validate the identity of the user. To assure that the email provided is actually good, a combination of various validation techniques is required. For the purpose of this question, we will focus on checking whether or not a given string represents a syntactically correct email address. A valid email address consists of a prefix, an '@' symbol, and an email domain. Both the prefix and the domain must be written in acceptable formats. For instance, in the address john.smith@mail.com, "john.smith" is the prefix, and "mail.com" is the domain. Note that, we say that a character is alphanumeric if it is a letter of the alphabet, 'A' to 'Z' or 'a' to '2', or one of the arabic numerals, 'O' to '9'. For instance, 'G' is an alphanumeric character while '&' is not. Acceptable prefix formats. For a prefix to be acceptable it must adhere to the following constraints: It contains at least one character. It contains only alphanumeric characters, underscores (_), periods (':'), and dashes ("-"). An underscore, a period, or a dash must always be followed by one or more alphanumeric characters. The first and last character must be alphanumeric. Examples of valid prefixes are: "abc-d", "abc.def", "abc", "abc_def". Examples of invalid prefixes are: "abc-", "abc..d", ".abc", "abclldef". Acceptable domain formats. For a domain to be acceptable it must adhere to the following constraints: It is made up of two portions separated by a period. The first portion contains at least one character. The second portion contains at least two characters. Page 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started