Programming Assignment 3 – Email, Twitter and Inheritance/JAVA ASSIGNMENT

This assignment is designed to test your understanding of inheritance in Java. The key concept is writing inheritance hierarchies.
Understand the Classes and Problem Every message contains some content (“The British are coming! The British are coming!” and a creator, or author (“Paul Revere”).

We could enhance these by adding other traits (date and/or time of creation), but let’s keep things simple and say that these are the only two aspects 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 a message, but a special kind of message. So, we can smell inheritance in the neighborhood. A message seems 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. 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

and DEFAULT_AUTHOR, for both min, max lengths and default data of every member. Set the maximum message length to a large number (at least a million) and the maximum

author length to a reasonable value like 40 or 65, not 5 or 200. Private Member Data
String message; String author; Public Methods ��� Default and 2-parameter constructors. ��� Mutator and Accessor for each member. ��� a toString() method that provides a

nicely formatted return String for potential screen I/O.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 2 of 5
Private Methods ��� private static validation helpers to filter client parameters. The Derived Class: Email Your first derived class is named Email. Email uses the

two members already present in the base class; the Email’s message is the message of the base class, and the author of the base class is the Emails��� author’s actual

name (not the from email address which I will introduce next). Do not try to store an Email’s message body or author as a new member of the derived class.

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 yours is no larger than that (but you can make is smaller for easier testing). Additional Private Member Data
String fromAddress; String toAddress; Public Methods ��� Default and 4-parameter constructors. Use constructor chaining. ��� Mutator and Accessor 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 isValidEAddr() (or similarly named) 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. The Derived Class: Tweet ��� Tweet uses the two members already present in the base class;

The Tweet’s message is the message of the base class, and the author of the base class is the Tweet’s author’s actual name. Public Static Class Constants ��� To the

existing base class statics, add three with a names and meaning similar or identical to MAX_TWITTER_ID_LENGTH (15), MAX_TWEET_LENGTH (140) and DEFAULT_USER_ID. Look

up and use the actual maximum lengths of the first two constants. ��� While the MAX_TWEET_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 Tweet has to be smaller than both of these values. If it’s not a valid base class message, it

isn’t a valid Tweet message, and this should be true if we later change the MAX_TWEET_LENGTH length to be 50 million.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 3 of 5
Additional Private Member Data
String fromID; Public Methods ��� Default and 3-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 Twitter IDs. ��� Create an isValidTweet() for the message. ��� Create an isValidTwitterID() for the

Twitter ID. ��� Make a third, even lower-level, helper that would make isValidTwitterID() clear and short. This helper- helper should be named something like

stringHasOnlyAlphaOrNumOrUnderscore() and that name tells you the kind of thing it should do: it should make sure the Twitter ID contains only some combination of

letters, numbers or an underscore (‘_’).
The Driver Class: MessageDriver Create several sample objects of type Message, Email, and Tweet in your main method. ��� Instantiate two or more Message objects, some

using the default constructor and some using the parameter-taking constructor. Mutate one or more of the members, and after that use the toString() to assist a screen

output so we can see what all of your objects contain. Next, test one or more accessors. Finally, test two or more mutators, providing both legal and illegal

arguments and testing the return values (thus demonstrating that the mutators do 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). ��� Recommended test of Class Email Similar to the

Message class. ��� Recommended test of Class Tweet Similar to the Message class.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 4 of 5
Sample Run:
Base Class Testing ****************************** Author: Smith Message ———————- Some messages just aren’t worth sending.
Author: Johnson Message ———————- hello world testing Message accessors: Smith
hello world
testing Message mutators: too long (as expected) Author: Smith Message ———————- Some messages just aren’t worth sending.
acceptable length (shoud be) Author: Smith Message ———————- LONG STRING abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde

abcde abcde abcde abcde
Derived Email Testing ***************************
From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
From: chloe123@gmail.com To: lili999@gmail.com Author: Liza Message ———————- bark bark
testing EMail accessors: lili999@gmail.com
lili999@gmail.com
testing Email mutators: too long (as expected) From: lili999@gmail.com
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 5 of 5
To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
missing @ char (as expected) From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
missing DOT char (as expected) From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
Derived Tweet Testing ***************************
testing Tweet mutators: bad Twitter ID (as expected) Tweet: Matt Johnson @Mattcsu Hello From Foothill college
acceptable Twitter ID (as expected) Tweet: Matt Johnson @a_good_user99 Hello From Foothill college Submission Instructions ��� Execute the program and copy/paste the

output that is produced by your program into the bottom of the source code file, making it into a comment. I will run the programs myself to see the output. ��� Make

sure the run “matches” your source. If the run you submit could not have come from the source you submit, it will be graded as if you did not hand in a run. ��� Use the

Assignment Submission link to submit the source code file. ��� Submit the following files: o Message.java o Email.java o Tweet.java o MessageDriver.java ��� Submit a zip

file containing your java file(s). Include your name in the name of the zip file.

Programming Assignment 3 – Email, Twitter and Inheritance/JAVA ASSIGNMENT

This assignment is designed to test your understanding of inheritance in Java. The key concept is writing inheritance hierarchies.
Understand the Classes and Problem Every message contains some content (“The British are coming! The British are coming!” and a creator, or author (“Paul Revere”).

We could enhance these by adding other traits (date and/or time of creation), but let’s keep things simple and say that these are the only two aspects 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 a message, but a special kind of message. So, we can smell inheritance in the neighborhood. A message seems 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. 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

and DEFAULT_AUTHOR, for both min, max lengths and default data of every member. Set the maximum message length to a large number (at least a million) and the maximum

author length to a reasonable value like 40 or 65, not 5 or 200. Private Member Data
String message; String author; Public Methods ��� Default and 2-parameter constructors. ��� Mutator and Accessor for each member. ��� a toString() method that provides a

nicely formatted return String for potential screen I/O.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 2 of 5
Private Methods ��� private static validation helpers to filter client parameters. The Derived Class: Email Your first derived class is named Email. Email uses the

two members already present in the base class; the Email’s message is the message of the base class, and the author of the base class is the Emails��� author’s actual

name (not the from email address which I will introduce next). Do not try to store an Email’s message body or author as a new member of the derived class.

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 yours is no larger than that (but you can make is smaller for easier testing). Additional Private Member Data
String fromAddress; String toAddress; Public Methods ��� Default and 4-parameter constructors. Use constructor chaining. ��� Mutator and Accessor 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 isValidEAddr() (or similarly named) 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. The Derived Class: Tweet ��� Tweet uses the two members already present in the base class;

The Tweet’s message is the message of the base class, and the author of the base class is the Tweet’s author’s actual name. Public Static Class Constants ��� To the

existing base class statics, add three with a names and meaning similar or identical to MAX_TWITTER_ID_LENGTH (15), MAX_TWEET_LENGTH (140) and DEFAULT_USER_ID. Look

up and use the actual maximum lengths of the first two constants. ��� While the MAX_TWEET_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 Tweet has to be smaller than both of these values. If it’s not a valid base class message, it

isn’t a valid Tweet message, and this should be true if we later change the MAX_TWEET_LENGTH length to be 50 million.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 3 of 5
Additional Private Member Data
String fromID; Public Methods ��� Default and 3-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 Twitter IDs. ��� Create an isValidTweet() for the message. ��� Create an isValidTwitterID() for the

Twitter ID. ��� Make a third, even lower-level, helper that would make isValidTwitterID() clear and short. This helper- helper should be named something like

stringHasOnlyAlphaOrNumOrUnderscore() and that name tells you the kind of thing it should do: it should make sure the Twitter ID contains only some combination of

letters, numbers or an underscore (‘_’).
The Driver Class: MessageDriver Create several sample objects of type Message, Email, and Tweet in your main method. ��� Instantiate two or more Message objects, some

using the default constructor and some using the parameter-taking constructor. Mutate one or more of the members, and after that use the toString() to assist a screen

output so we can see what all of your objects contain. Next, test one or more accessors. Finally, test two or more mutators, providing both legal and illegal

arguments and testing the return values (thus demonstrating that the mutators do 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). ��� Recommended test of Class Email Similar to the

Message class. ��� Recommended test of Class Tweet Similar to the Message class.
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 4 of 5
Sample Run:
Base Class Testing ****************************** Author: Smith Message ———————- Some messages just aren’t worth sending.
Author: Johnson Message ———————- hello world testing Message accessors: Smith
hello world
testing Message mutators: too long (as expected) Author: Smith Message ———————- Some messages just aren’t worth sending.
acceptable length (shoud be) Author: Smith Message ———————- LONG STRING abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde abcde

abcde abcde abcde abcde
Derived Email Testing ***************************
From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
From: chloe123@gmail.com To: lili999@gmail.com Author: Liza Message ———————- bark bark
testing EMail accessors: lili999@gmail.com
lili999@gmail.com
testing Email mutators: too long (as expected) From: lili999@gmail.com
CS 1B Assignment #3 Winter 2017 January 24,

2017 _________________________________________________________________________________________________________
Page 5 of 5
To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
missing @ char (as expected) From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
missing DOT char (as expected) From: lili999@gmail.com To: chloe123@gmail.com.com Author: lili koi Message ———————- Arf, arf, arf, arf …….. arf
Derived Tweet Testing ***************************
testing Tweet mutators: bad Twitter ID (as expected) Tweet: Matt Johnson @Mattcsu Hello From Foothill college
acceptable Twitter ID (as expected) Tweet: Matt Johnson @a_good_user99 Hello From Foothill college Submission Instructions ��� Execute the program and copy/paste the

output that is produced by your program into the bottom of the source code file, making it into a comment. I will run the programs myself to see the output. ��� Make

sure the run “matches” your source. If the run you submit could not have come from the source you submit, it will be graded as if you did not hand in a run. ��� Use the

Assignment Submission link to submit the source code file. ��� Submit the following files: o Message.java o Email.java o Tweet.java o MessageDriver.java ��� Submit a zip

file containing your java file(s). Include your name in the name of the zip file.