A riddle wrapped around an enigma containing interfaces in Java.
Your assignment requires you to create two classes that provide different implementations of the same interface, which is given to you. Several additional class definitions will also be needed to solve the assignment.
Sentences contain words. Words contain characters The order of these words and characters is important for many human languages.
If you were trying to model human written language, you might decide to create a Sentence class to represent sentences, a Word class to represent words, and a Character class to represent characters.
- Since sentences contain words, any
Sentenceobject would have to encapsulate a list ofWordobjects. - Since words contain
Characterobjects, anyWordobject would have to encapsulate a list ofCharacterobjects. - Since both
SentenceandWordobjects contain sequentially ordered lists of things, you might make them both implement the sameSequentiallyOrderedinterface to guarantee consistency of behavior. - Since both
WordandCharacterobjects can be stored in ordered lists, you may have them both inherit from a commonOrderedThingclass that may contain any attributes shared by all ordered things.
The following interface code is given to you. The Sentence and Word classes that you will create must implement this interface.
package edu.nyu.cs;
import java.util.ArrayList;
public interface SequentiallyOrdered {
public abstract OrderedThing getFirst();
public abstract OrderedThing getLast();
public abstract ArrayList<OrderedThing> getSequence();
}You will need to create a class that represents a single character of text.
CharacterextendsOrderedThingbecause eachCharacterobject will be stored in an orderedArrayListofCharacterobjects in a Word object.
Note: a class named Character already exists in the Java API java.lang package, so your class with the same name hides that one. If you want to refer to that API class (which you shouldn't need to), you'll need to reference it by its full package and class name, such as java.lang.Character in your code.
You should create a class Word that represents words in a language.
Wordimplements theSequentiallyOrderedinterface, because a word is a sequence of characters.WordextendsOrderedThingbecause eachWordobject will be stored in an orderedArrayListofWordobjects in aSentenceobject.
The Word class should have two instance fields:
- an instance field of type
ArrayList<Character>which will store a word's character sequence asCharacterobjects. - an instance field of type
int, representing theWord's position in aSentencein which it is being used (with the firstWordin aSentencebeing position 0).
Word should have the following methods:
getFirst()should return the firstCharacterobject of the WordgetLast()should return the lastCharacterobject of the WordgetSequence()should return anArrayListcontaining all theCharacterobjects in the Word.getPosition()should return theintrepresenting theWord's position in the sentence
Based on the description of this class above, it should be clear to you how Word implements the SequentiallyOrdered interface, which requires the getFirst() and getLast() methods to return an OrderedThing. If not, here is a hint: a child class can be considered an instance of its parent class... this is polymorphism. So a Character object, since it extends OrderedThing, can also be considered an instance of the OrderedThing class.
The Word constructor should take two parameters:
- a
Stringparameter and add the individual characters of theStringto theArrayList<Character>instance field. - an
intparameter representing the position of theWordin aSentenceand set the relevant instance field accordingly
You should create a third class, Sentence, that represents sentences in a language. Sentence implements the SequentiallyOrdered interface, because a sentence is a sequence of words.
Sentence should have a single instance field of type ArrayList<Word> which will store the words of a sentence. This relationship between the Sentence and Word classes is called composition, because a Sentence is composed of Wordobjects.
Note: there is NOT an inheritance relationship between Sentence and Word.
Sentence should have the following methods:
getFirst()should return the firstWordof theSentence,getLast()should return the lastWordof theSentencegetSequence()should return anArrayListcontaining all theWordobjects in theSentence.
Based on the description of this class above, it should be clear to you how Sentence implements the SequentiallyOrdered interface, which requires the getFirst() and getLast() methods to return an OrderedThing. If not, here is a hint: a child class can be considered an instance of its parent class... this is polymorphism. So a Word object, if it extends OrderedThing, can also be considered an instance of the OrderedThing class.
The Sentence constructor should take a single String parameter representing the Sentence, and add each Word of the sentence to the ArrayList<Word>. You can split the String into words by using the String split() method in the following way:
// split by any non-alphanumeric character
String[] words = s.split("[^\\w']+")Notice that the split() method will give you an array of String objects, and you will need to go through that array, creating Word objects and adding them to the ArrayList<Word>.
Create a test class with a main method that shows how a Sentence object can be instantiated with a sentence of your choosing, how each of the methods of the Sentence class can be called in a meaningful way, and how each of the methods of the Word class can be called on at least one of the Word objects encapsulated within the Sentence object's ArrayList<Word> instance field that you created.
This project has several important directories:
src- contains the Java source code for the project (i.e..javafiles)bin- contains the compiled code (i.e..classfiles)lib- contains any dependencies (other libraries of code that the project depends upon to work)
If your project has no dependencies and has not been compiled, you may not see the lib or bin directories.
Once you have completed the changes to th assignment, you are ready to submit it. Do this from within Visual Studio Code.
- Click on the
Source Controlicon in the left activity bar in Visual Studio Code. - In the Source Control side bar, you will see a field named
Message- type in a unique message about what you have done, e.g. "Finished assignment!" or whatever you want to write as a short note to yourself. - Hover over the words
Source Control. You will see a...icon appear - click it to see a menu. In that menu, clickCommit->Commit. This logs the changes you've made to the Git project - remember Git is used to keep track of changes. - Go to the same menu and click
Pushto submit your assignment - this uploads your updated files to the copy of your respository on GitHub.
That's it... you're done.
Prove to yourself that you have correctly submitted by viewing your repository on the GitHub website - you should see your completed README.md file there.
You can re-submit as many times as you want before the deadline. Just make changes to the files on your own computer and repeat the process outlined above to upload them to GitHub.
