Please provide comments with your code. This must be done in Java. Correct answe
ID: 3708127 • Letter: P
Question
Please provide comments with your code. This must be done in Java. Correct answer will be given a thumbs up asap.
2 - The Program You must detect all of the the email addresses in the input file called input.txt. If an email address does not have a sub-domain, create an Email object to store the contents of that email. If an email address does have a sub-domain, please create UniversityEmail object for that email. You must store all emails in the same (one single) array 2.1 Email Class Define a base class called Email. The class Email will have 3 String member variables: username, domain,and extension. For example, if one of the email addresses in the file is wearelivinginasimulation21@ridiculousconspiracytheories.net, then the values of the member variables should be: Username: wearelivinginasimulation21 - Domain: ridiculousconspiracytheories Extension: net The Email class should contain a function that returns the code value of 0 and will be overwritten in the extended class to return the various associated codes for each UniversityEmail. This will enable you to print your output by code selection. Refer to section 2.2 for information about the code value. 2.2 UniversityEmail Extended Class Derive a class called UniversityEmail that extends Email. University Email will have 2 additional member variables: department (string) and code (int). Refer to the table below to assign a value to the member variable code.Explanation / Answer
//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111
import java.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
//------------------PART 1. EMAIL CLASS ---------------------//
class Email
{
private String userName, domain, extension;
Email() {
userName = "";
domain = "";
extension = "";
}
private void fragmentStoreContentEmail(String email)
{
userName = email.substring(0,email.indexOf("@"));
String domainAndExtn = email.substring(email.indexOf("@")+1);
domain = domainAndExtn.substring(0,domainAndExtn.lastIndexOf("."));
extension = domainAndExtn.substring(domainAndExtn.lastIndexOf(".")+1);
}
//Method to split and store email contents.
public void setEmail(String email) {
fragmentStoreContentEmail(email);
}
//Getter methods
public String getDomain(){ return domain; }
public String getUserName(){ return userName; }
public String getExtension(){ return extension; }
}
//--------------- PART 2. UNIVERSITYEMAIL CLASS -------------//
class UniversityEmail extends Email
{
private String department;
private int code;
private enum Subdomains {
art, chee, chem, coe, cs, egr, polsci;
}
UniversityEmail() {
department = "";
code = 0;
}
public String getDepartment()
{ return department; }
public int getCode()
{ return code; }
public void setDeptAndCodeForEmail(Email emailContent) {
String domain = emailContent.getDomain();
String subDomain = domain.subString(0,domain.indexOf("."));
}
public void determineDeptAndCode(String subDomain) {
try{
Subdomain subdmn = Subdomain.valueOf(subDomain);
switch(subDomain) {
case art: {
department = "art";
code = 1;
break;
}
case chee: {
department = "chee";
code = 2;
break;
}
case chem: {
department = "chem";
code = 3;
break;
}
case coe: {
department = "coe";
code = 4;
break;
}
case cs: {
department = "cs";
code = 5;
break;
}
case egr: {
department = "egr";
code = 4;
break;
}
case polsci: {
department = "polsci";
code = 4;
break;
}
default:
break;
}
}catch(Exception e) {
}
}
}
//------------------ PART 3. FILE INPUT/OUTPUT ----------------------//
/*------------------ Static function to read emails
from file and store/ return
array of emails ---------------------------*/
public static String[] readEmailsFromFile(String fileName) {
HashSet<String> hs = new HashSet<>();
FileReader file = null;
try {
file = new FileReader(new File(fileName));
} catch (FileNotFoundException e1) {
System.err.println("File: "+ fileName +"not found!");
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(file);
String line;
try {
//Scan each line to check if email exist.
while ((line = br.readLine()) != null) {
scanLineForEmail(line, hs);
}
} catch (IOException e) {
System.err.println("Error when reading");
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.err.println("Unexpected error");
e.printStackTrace();
}
}
}
System.out.println("Emails Identified ");
for (String string : hs) {
System.out.println(string);
}
//storing identified emails into array
String[] emailArray=hs.toArray(new String[hs.size()]);
//returning email array
return emailArray;
}
//--------------- function to scan each line for email pattern ---------------//
public static void scanLineForEmail(String line,HashSet<String> container){
//------------ PART 4. EMAIL ADDRESS FORMAT -----------------//
Pattern p = Pattern.compile("([\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[A-Za-z]{2,4})");
Matcher m = p.matcher(line);
while(m.find()) {
container.add(m.group(1));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.