Java, please include comments with your code. Also, make sure to do the bonus. W
ID: 3706508 • Letter: J
Question
Java, please include comments with your code. Also, make sure to do the bonus. Will offer a thumbs up for the correct solution or even a solution that kinda works. However, include comments with your code so I know what's going on.
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
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
Refresh your project to see the output file.
Email.java
==========
public class Email {
protected String userName;
protected String domain;
protected String extension;
public Email()
{
}
public Email(String uname, String dom, String ext)
{
userName = uname;
domain = dom;
extension = ext;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String toString()
{
return userName + "@" + domain + "." + extension;
}
public int getCode()
{
return 0;
}
}
UniversityEmail.java
=========
public class UniversityEmail extends Email {
private String department;
private int code;
public UniversityEmail()
{
}
public UniversityEmail(String uname, String dept, String dom, String ext)
{
super(uname, dom, ext);
department = dept;
setCode(dept);
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
setCode(department);
}
@Override
public int getCode() {
return code;
}
private void setCode(String department) {
if(department.equals("art"))
code = 1;
else if(department.equals("chee"))
code = 2;
else if(department.equals("chem"))
code = 3;
else if(department.equals("coe"))
code = 4;
else if(department.equals("cs"))
code = 5;
else if(department.equals("egr"))
code = 6;
else if(department.equals("polsci"))
code = 7;
}
public String toString()
{
return getUserName() + "@" + department + "." + getDomain() + "." + getExtension();
}
}
ProcessEmail.java
=================
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class ProcessEmail {
public static void main(String[] args) {
String inFilename, outFilename;
Scanner keyboard = new Scanner(System.in);
int code;
System.out.print("Enter input filename: ");
inFilename = keyboard.nextLine();
System.out.print("Enter output filename: ");
outFilename = keyboard.nextLine();
System.out.print("Enter the code (0-8) for emails to be written to output: ");
code = keyboard.nextInt();
ArrayList<Email> emails;
try {
emails = loadEmails(inFilename);
writeOutput(outFilename, emails, code);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
private static ArrayList<Email> loadEmails(String filename) throws FileNotFoundException
{
Scanner file = new Scanner(new File(filename));
ArrayList<Email> emails = new ArrayList<Email>();
while(file.hasNext())
{
String word = file.next();
if(word.indexOf('@') != -1) //if the word is an email?
emails.add(createEmail(word));
}
file.close();
return emails;
}
private static Email createEmail(String s)
{
int idx = s.indexOf('@');
String uname = s.substring(0, idx);
String rest = s.substring(idx+1);
String[] parts = rest.split("\."); //split the remaining part using . as delimiter
int len = parts.length;
//if there are any punctuation symbols like , or . at end, remove them
parts[len-1].replaceAll("[^A-Za-z]", ""); //replace anything other than alphabet with empty string
if(parts.length == 3) //there is a sub-domain, create UniversityEmail
return new UniversityEmail(uname, parts[0], parts[1], parts[2]);
else
return new Email(uname, parts[0], parts[1]);
}
private static void writeOutput(String outFilename, ArrayList<Email> emails, int code) throws FileNotFoundException
{
PrintWriter pw = new PrintWriter(new File(outFilename));
for(int i = 0; i < emails.size(); i++)
{
Email e = emails.get(i);
if(code == 0 && e.getCode() == 0) //emails without subdomain
pw.println(e.toString());
else if(code == 8 && e.getCode() != 0) //all emails with subdomain
pw.println(e.toString());
else if(code == e.getCode()) //only emails matching specified code
pw.print(e.toString());
}
pw.close();
System.out.println("Output written to file " + outFilename);
}
}
output
=====
Enter input filename: emails.txt
Enter output filename: out.txt
Enter the code (0-8) for emails to be written to output: 5
Output written to file out.txt
File: out.txt
someemail@cs.uh.edu,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.