Group Assignment 3- File I/O Due: Sunday, April 15 at 11:59 PM One submission pe
ID: 3708066 • Letter: G
Question
Group Assignment 3- File I/O
Due: Sunday, April 15 at 11:59 PM
One submission per group
1 - Objective
The purpose of this assignment is for students to practice File IO in Java.
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.1Email 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.2UniversityEmail Extended Class
Derive a class called ?UniversityEmail ?that extends ?Email. UniversityEmail ?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?.
Code Subdomains:
1
art
2
chee
3
chem
4
coe
5
cs
6
egr
7
polsci
For instance, if one of the email addresses in the input file is iamrunningoutofcleverusernames@cs.email.com?, then the values of the member variables in the derived class should be:
Department: cs
Code:5
You can create any member functions needed in order to achieve the desired output (i.e. creating getters to check code).
3- File Input/Output
You will need to read the entire file content and find out if there are any email addresses in the input file. There may be any number (including zero) of email addresses in an input file.
3.1Email Address Format
A username consists of characters and/or digits (you can assume ?no special characters or spaces in the username?)
A domain consists of characters only (you can assume no special characters, no spaces, and no digits in the domain)
An extension consists of characters only (you can assume no special characters, no spaces, and no digits in the extension)
A username is always separated from the email by the special character ‘@’. The special character ‘@’ is not going to be used for other purposes in the file.
If there is no sub-domain, a domain and an extension will be given (separated by dot (.)), immediately after the special character ‘@’
If a sub-domain exists, a sub-domain, a domain and an extension will be given, separated by dots (.), immediately after ‘@’
Every email in the input file will be separated from the rest of the text by a space (‘ ‘) or comma (,)
3.2Input Example
An example of input file content:
My email address is someemail@cs.uh.edu, and my backup is someemail2@art.uh.edu, what is yours? Hi my name is Cook Tim. My email address is cooktiml2000@uh.edu, got it? Here are just a list of emails youveonlygotonespagetti@eminem.rap, admin@negativeloops.org, wouldyouliketobuystuff@pyramid.scheme, dontletyourmemes@be.dreams
3.3Output
After you copy all emails from the file to the array, prompt the user to enter type of emails to be included in the output file, ?out.txt?.
If the user enters 0, write the emails that do not have sub-domain in the array list.
If the user enters a number between 1-7, write all emails from the specific department in the output file.
If the user enters 8, write all email address that have a sub-domain Assume that the user will enter only one integer from (0 to 8).
You must override the ?toString() ?function to print the email address.
3.4Output Example
Taking the input from 3.2, an example of output file content:
If the user enters a 0, the contents of the output file will be: cooktiml2000@uh.edu
youveonlygotonespagetti@eminem.rap admin@negativeloops.org
wouldyouliketobuystuff@pyramid.scheme dontletyourmemes@be.dreams
If the user enters a 5, the contents of the output file will be: someemail@cs.uh.edu
If the user enters a 1, the contents of the output file will be: someemail2@art.uh.edu
If the user enters a 8, the contents of the output file will be: someemail@cs.uh.edu someemail2@art.uh.edu
If the user enters a 2, the output file will be empty.
NOTE: It is always a good habit to implement your file I/O operations with the exception handling mechanism. That is, put your file I/O operations within the try block followed by a number of catch blocks that handle the file I/O related exceptions.
4- Grading
All files related to this homework should be zipped and submitted on Blackboard. Your code will be compiled and tested on the server; ensure that you have thoroughly tested it on the server. Note: You must also include a README.txt file that includes the exact commands used to successfully compile and run your code on the server.
Programs that do not compile will be worth at most, ?10 points?. Programs with infinite loops will be worth at most, ?10 points?.
Grading Criteria
Points
README.txt
5
Email Class
35
UniversityEmail Extended Class
30
Input
15
Output
15
Total
100
5- Bonus: 5 Pts
Improve your code by handling incomplete email addresses (e.g. thisemail@) or email addresses with non-character domains (e.g., xy@cs23.uh3). In both of these cases, your code should not store the email address into the array.
If you are attempting the bonus, you must say so in your README.txt file.
1
art
2
chee
3
chem
4
coe
5
cs
6
egr
7
polsci
Explanation / Answer
I have answered this question earlier and giving the code for same.
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.