Question

IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from...

IN JAVA PLS DUE TODAY

Assignment 4 - Email, Shwitter and Inheritance

Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge.

OPTION A (Basic): Message, EMail and Tweet

Understand the Classes and Problem

Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other traits (author, date and/or time of creation), but let's keep things simple and say that this is the only aspect of a basic message.

Some messages, however, have further components that define them. For example, an email consists of a normal message, plus at least two other items: a from email address and a to email address. An email is amessage, but a special kind of message. So we can smell inheritance in the neighborhood. A messageseems like it will make a good base class, and an email, having an is a relationship with message,is a good candidate for a derived class or extension of the base class message.

Meanwhile, a tweet consists of a normal message, plus at least one other item: a from user id. An tweet is a message, too. It has a different kind of specialization than an email has, so it is a different extension of the base class. (We are not considering a direct tweet, which would also require a to user id.)

Not only will both emails and tweets contain extra data than the base class, message, but in at least one instance we will see the same member (the content) have a different kind of restriction in the derived class than in the base class (can you guess what I'm suggesting before you read on?). Thus, even though we store the tweet content in the base class member, without the need for a new content area for the tweet, we will still have a different validation of the length of that message content.

Warning: Because it makes sense to do so, we are going to name the message content message and the message class Message. So what I called content in the above paragraph will be known as (lower-case)message in what comes next. The class, itself, will be (upper-case) Message. You'll see. Just keep reading.

File Structure: You will use one file for the entire program, so the non-Foothill classes should be non-public.

The Base Class: Message

Your base class is named Message.

Public or Protected (your choice) Static Class Constants

Define a full set of limits and defaults, like MAX_MSG_LENGTH, for both min, max length and default data of the member. Set the maximum message length to 100,000.

Private Member Data

   String message;

Public Methods

  • Default and 1-parameter constructors.
  • Mutator and Accessor for the member.
  • a toString() method that provides a nicely formatted return String for potential screen I/O.

Private Methods

  • private static validation helper to filter client parameters. These will support your public methods.

Recommended test of Class Message

Instantiate two or more Message objects, some using the default constructor and some using the parameter-taking constructor. Mutate the member, and after that use the toString() to assist a screen output so we can see what all of your objects contain. Next, test the accessor. Finally, test the mutator, providing both legal and illegal arguments and testing the return values (thus demonstrating that the mutator does the right thing). Here is a sample run from my test (but you will have to imagine/deduce the source I used and create your own source for your testing, which will produce a different output).

Example Test Run of Message Class

/* ---------------------------------------------------------
Base Class Testing ******************************

 Message ---------------------- 
Some messages just aren't worth sending.

 Message ---------------------- 
hello world

testing Message accessor:

hello world

testing Message mutator:
too long (as expected)

 Message ---------------------- 
Some messages just aren't worth sending.

acceptable length (should  be)

 Message ---------------------- 
LONG STRING abcde abcde abcde abcde  abcde abcde abcde  abcde  abcde  abcde  abc
de  abcde abcde  abcde abcde abcde
--------------------------------------------------------- */

The Derived Class: Email

Your first derived class is named Email. Email uses the member already present in the base class; the Email's message is the message of the base class. Do not try to store an Email's message body as a new member of the derived class or you will get get a 0 on the assignment -- not desirable by you or me. Duplicating base class data in derived class means you do not understand what inheritance is, thus the significance of this kind of mistake.

Public Static Class Constants

To the existing base class statics, add at least two with names and meanings similar or identical to MAX_EMAIL_ADDRESS_LENGTH and DEFAULT_EMAIL_ADDRESS. Look up the maximum length of an email address, and make sure yours is no larger than that (but you can make it smaller for easier testing).

Additional Private Member Data

   String fromAddress;
   String toAddress;

Public Methods

  • Default and 3-parameter constructors. Use constructor chaining.
  • Mutators and Accessors for the new members.
  • Override those methods of the base class for which it makes sense to do so. Think about this and come to your own conclusions.

Private Methods

  • private static validation helper to filter a bad email address. You should do the minimum, but you don't have to be more complete than that. This is just to show that you can perform basic filtering. The minimum is a method isValidEmailAddress(), which tests for both length and the existence of at least one '@' and one '.' character. Further is up to you, but don't overdo it or be too restrictive.

Recommended test of Class Email

Similar to the Message class.

Example Test Run of Email Class (done in same run as overall program run)

ALL OF these are supposed to use at g-mail dot come not @ mail.com but Chegg does not allow the use of those words

/* ---------------------------------------------------------
Derived Email Testing ***************************

From: lili999@ mail.com
To: chloe123@ mail.com.com
 Message ---------------------- 
Arf, arf, arf, arf ........ arf

From: chloe123@ mail.com
To: lili999@ mail.com
 Message ---------------------- 
bark bark

testing EMail accessors:
lili999@ mail.com

lili999@ mail.com

 testing Email mutators:
too long (as expected)
From: lili999@ mail.com
To: chloe123@ mail.com.com
 Message ---------------------- 
Arf, arf, arf, arf ........ arf

missing @ char (as expected)
From: lili999@ mail.com
To: chloe123@ mail.com.com
 Message ---------------------- 
Arf, arf, arf, arf ........ arf

missing DOT char (as expected)
From: lili999@ mail.com
To: chloe123@ mail.com.com
 Message ---------------------- 
Arf, arf, arf, arf ........ arf
--------------------------------------------------------- */

The Derived Class: Shweet

(It is like a Twitter Tweet, but since I can't be certain that the details of a Tweet are exactly what I state here, we will disengage this from Twitter, and make the constraints that I impose accurate for a Shweet by decree.)

Shweet uses the member already present in the base class; The Shweet's message is the message of the base class. Do not try to store a Shweet's message body as a new member or you will get a 0 on the assignment, as mentioned above.

Public Static Class Constants

To the existing base class statics, add three: MAX_SHWITTER_ID_LENGTH (15), MAX_SHWEET_LENGTH (140) and DEFAULT_USER_ID.

While the MAX_SHWEET_LENGTH is going to be smaller than the base class's MAX_MSG_LENGTH, do not assume this fact in your implementation. A valid message length for a Shweet has to be smaller than both of these values. If it's not a valid base class message, it isn't a valid Shweet message, and this should be true if we later change the MAX_SHWEET_LENGTH length to be 50 million.

Additional Private Member Data

   String fromID;

Public Methods

  • Default and 2-parameter constructors. When it is possible and meaningful, use constructor chaining. Think about which constructor to chain to.
  • Mutator and Accessor for the new member.
  • Override those methods of the base class for which it makes sense to do so. Think about this and come to your own conclusions. You have to somehow enforce the length limits of both the base class and the derived class for the same message member.

Private Methods

  • private static validation helpers (plural) to filter out bad tweets and shwitter IDs. Create anisValidShweet()for the message. Also, create an isValidShwitterID() for the Shwitter ID. But also, make a third, even lower-level, helper that would make isValidShwitterID() clear and short. This helper-helper should be namedstringHasOnlyAlphaOrNumOrUnderscore() and that name tells you the kind of thing it should do: it should make sure the shwitter ID contains only some combination of letters, numbers or an underscore ('_').     Also, I don't care whether you decide that the Shwitter ID be case sensitive. If you don't know what String or Character methods can be used for this, look them up online. They are very easy to find.

Recommended test of Class Shweet

Similar to the Message class.

Example Test Run of Shweet Class (done in same run as overall program run)

/* ---------------------------------------------------------
Derived Shweet Testing ***************************

Shweet: @kimkardashian
Oh Deer https://www.keek.com/!PYjCdab

Shweet: @katyperry
It's a verb and an adjective.

testing Shweet accessors:
kimkardashian

testing Shweet mutators:
bad shwitter ID (as expected)
Shweet: @kimkardashian
Oh Deer https://www.keek.com/!PYjCdab

acceptable shwitter ID (as expected)
Shweet: @a_good_user99
Oh Deer https://www.keek.com/!PYjCdab
--------------------------------------------------------- */
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package inheritance;

// Defines super class Message

class Message

{

// Instance variable to store message

private String message;

// Defines constants

private static final int MAX_MSG_LENGTH = 100000;

private static final int MIN_MSG_LENGTH = 1;

// Default constructor

Message()

{

message = "";

}// End of Default constructor

// Parameterized constructor

Message(String msg)

{

// Calls the method to validate message

validationHelper(msg);

}// End of Parameterized constructor

// Method to validate message

private void validationHelper(String msg)

{

// Checks if length of the message is less than minimum

if(msg.length() < MIN_MSG_LENGTH)

{

// Displays error message

System.out.println("ERROR: Message must have atleast 1 character.");

// Sets the space as message

message = " ";

}// End of if condition

// Otherwise checks if message length is greater than maximum length

else if(msg.length() > MAX_MSG_LENGTH)

{

// Displays error message

System.out.println("ERROR: Message must not exceed " +

MAX_MSG_LENGTH + " characters.");

// Extracts from starting index position to maximum length from message

message = msg.substring(0, MAX_MSG_LENGTH);

}// End of else if condition

// Otherwise assigns parameter message

else

message = msg;

}// End of method

// Method to set message

void setMessage(String msg)

{

// Calls the method to validate message

validationHelper(msg);

}// End of method

// Method to return message

String getMessage()

{

return message;

}// End of method

// Override toString() method to return message

public String toString()

{

return "\n Message ---------------------- \n " + message;

}// End of method

}// End of class Message

// Creates a sub class Email derived from super class Message

class Email extends Message

{

// Instance variable to store address

private String fromAddress;

private String toAddress;

// Defines constants

private static final int MAX_EMAIL_ADDRESS_LENGTH = 20;

private static final String DEFAULT_EMAIL_ADDRESS = "pmsg-mail";

// Default constructor

Email()

{

// Calls the base class default constructor

super();

fromAddress = toAddress = "";

}// End of Default constructor

// Parameterized constructor

Email(String msg, String fromA, String toA)

{

// Calls the base class parameterized constructor

super(msg);

// Calls the method to validate from address

// If method returns true then assign the parameter to instance variable

if(isValidEmailAddress(fromA))

fromAddress = fromA;

// Otherwise invalid address

else

{

// Displays error message

System.out.println("ERROR: Invalid from E-Mail address.");

// Assigns the default address

fromAddress = DEFAULT_EMAIL_ADDRESS;

}// End of else

// Calls the method to validate to address

// If method returns true then assign the parameter to instance variable

if(isValidEmailAddress(toA))

toAddress = toA;

// Otherwise invalid address

else

{

// Displays error message

System.out.println("ERROR: Invalid to E-Mail address.");

// Assigns the default address

toAddress = DEFAULT_EMAIL_ADDRESS;

}// End of else

}// End of method

// Method to return if email address is valid

// Otherwise returns false

private boolean isValidEmailAddress(String add)

{

// Initially valid status is true

boolean status = true;

// Extracts '@' index position from the address

// if it is -1 then not found

if(add.indexOf('@') == -1)

{

// Displays error message

System.out.println("ERROR: E-Mail address missing '@' symbol.");

// Sets the status to false

status = false;

}// End of if condition

// Extracts '.' index position from the address

// if it is -1 then not found

if(add.indexOf('.') == -1)

{

// Displays error message

System.out.println("ERROR: E-Mail address missing '.' symbol.");

// Sets the status to false

status = false;

}// End of if condition

// Returns the status

return status;

}// End of method

// Method to set from id

void setFromAddress(String fromA)

{

// Calls the method to check valid from id

// if valid then assigns the parameter fromA to instance variable fromAddress

if(isValidEmailAddress(fromA))

fromAddress = fromA;

// Otherwise invalid from id

else

// Assigns default id to fromAddress

fromAddress = DEFAULT_EMAIL_ADDRESS;

}// End of method

// Method to set to id

void setToAddress(String toA)

{

// Calls the method to check valid to id

// if valid then assigns the parameter toA to instance variable toAddress

if(isValidEmailAddress(toA))

toAddress = toA;

// Otherwise invalid to id

else

// Assigns default id to toAddress

toAddress = DEFAULT_EMAIL_ADDRESS;

}// End of method

// Method to return from id

String getFromAddress()

{

return fromAddress;

}// End of method

// Method to return to id

String getToAddress()

{

return toAddress;

}// End of method

// Override toString() method to return from, to address and message

public String toString()

{

return "\n From: " + getFromAddress() +

"\n To: " + getToAddress() +

super.toString();

}// End of method

}// End of class EMail

// Creates a sub class Shweet derived from super class Message

class Shweet extends Message

{

// Instance variable to store message

private String fromID;

// Defines constants

private static final int MAX_SHWITTER_ID_LENGTH = 15;

private static final int MAX_SHWEET_LENGTH = 140;

private static final String DEFAULT_USER_ID = "A_111";

// Default constructor

Shweet()

{

// Calls the base class default constructor

super();

fromID = "";

}// End of Default constructor

// Parameterized constructor

Shweet(String msg, String id)

{

// Calls the base class parameterized constructor

super(msg);

if(isValidShwitterID(id))

fromID = id;

else

{

System.out.println("Invalid ID.");

fromID = DEFAULT_USER_ID;

}// End of else

}// End of method

// Method to return true if address first character is an alphabet

// and second character is a under score character

// otherwise returns false

private boolean stringHasOnlyAlphaOrNumOrUnderscore(String add)

{

// Initially valid status is true

boolean status = true;

// Extracts first index position character of address

// Checks if it is not a letter

if(!Character.isLetter(add.charAt(0)))

{

// Displays error message

System.out.println("ERROR: First letter of ID must be alphabet.");

// Sets the status to false

status = false;

}// End of if condition

// Extracts second index position character of address

// Checks if it is not a '_' underscore character

if(!(add.charAt(1) != '_'))

{

// Displays error message

System.out.println("ERROR: Second letter of ID must be '_' character.");

// Sets the status to false

status = false;

}// End of if condition

// Returns the status

return status;

}// End of method

// Method to return true if from id is valid

// Otherwise returns false

private boolean isValidShwitterID(String add)

{

// Initially valid status is true

boolean status = true;

// Checks if address length is greater than maximum id length

if(add.length() > MAX_SHWITTER_ID_LENGTH)

{

// Displays error message

System.out.println("ERROR: Exceeds length of ID." + MAX_SHWITTER_ID_LENGTH);

// Sets the status to false

status = false;

}// End of if condition

// Otherwise

else

{

// Calls the method to checks alphabet or number or underscore sequence

status = stringHasOnlyAlphaOrNumOrUnderscore(add);

}// End of else

// Returns the status

return status;

}// End of method

// Method to set from id

void setFromID(String id)

{

// Calls the method to check valid id

// If valid id then assign parameter id to instance variable fromID

if(isValidShwitterID(id))

fromID = id;

// Otherwise invalid id

else

// Assigns default id

fromID = DEFAULT_USER_ID;

}// End of method

// Method to return from id

String getFromID()

{

return fromID;

}// End of method

// Override toString() method to return from address and message

public String toString()

{

return "\n Shweet: " + getFromID() +

super.toString();

}// End of method

}// End of class Shweet

// Driver class MessageSending definition

public class MessageSending

{

// main method definition

public static void main(String pyari[])

{

// Creates objects to test

Email em = new Email("bark bark", "[email protected]", "[email protected]");

System.out.println(em);

Email em1 = new Email("LONG STRING abcde abcde abcde abcde abcde abcde abcde " +

"abcde abcde abcde abc de abcde abcde abcde abcde abcde",

"lili999mail.com", "chloe123@mailcom");

System.out.println(em);

Shweet sw = new Shweet("https://www.keek.com/!PYjCdab", "@kimkardashian");

System.out.println(sw);

}// End of main method

}// End of class

Sample Output:


From: [email protected]
To: [email protected]
Message ----------------------
bark bark
ERROR: E-Mail address missing '@' symbol.
ERROR: Invalid from E-Mail address.
ERROR: E-Mail address missing '.' symbol.
ERROR: Invalid to E-Mail address.

From: [email protected]
To: [email protected]
Message ----------------------
bark bark
ERROR: First letter of ID must be alphabet.
Invalid ID.

Shweet: A_111
Message ----------------------
https://www.keek.com/!PYjCdab

Add a comment
Know the answer?
Add Answer to:
IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...

    This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection. The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file. The Tweet Class The Tweet class consist of nine members that include two static ones (the members decorated with the...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • Overview This assignment will give you experience on the use of classes. Understand the Application Every...

    Overview This assignment will give you experience on the use of classes. Understand the Application Every internet user–perhaps better thought of as an Internet connection–has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other—sometimes optional, sometimes needed—data (such as a MAC address, port, local IP address, gateway, etc), but we keep things simple and use...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is...

    Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is to practice inheritance. To get credit for the lab, you need to demonstrate to the student helper that you have completed all the requirements. Question 1: Consider the following detailed inheritance hierarchy diagram in Fig. 1. 1) The Person.java constructor has two String parameters, a first name and a last name. The constructor initializes the email address to the first letter of the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT