Modelling a file in a text editor

Your task in this assignment will be to implement some of the methods in a basic text editing class and to extend its functionality using inheritance. The methods you are required to implement are labelled with TO IMPLEMENT, and are summarised for you below.

What you have to do

Download the Java Programming Test Assignment Bundle available through the iLearn COMP229 page. In it you will find the basic implementation of a class called TextEditing, and an extended class called TextEditor.

TextEditing contains the basic methods for handling text in an "open" file. The file is modelled as a sequence of characters, split between two stacks. The variable fileFront contains the characters at the front of the file, i.e. those which precede the position of the cursor, and fileEnd contains the characters at the end of the file, i.e. those which come after the position of the cursor as viewed in a text editor. This means that the cursor position is always situated at (strictly speaking, right after) the last character in fileFront, and there is also a variable cursorPosition which keeps a record of the numerical position of the cursor relative to the whole file.

Note that this somewhat curious arrangement makes it very easy indeed to insert characters just after the last character in fileFront, and to move the cursor forward and back, one character at a time. [Why?]

TextEditor extends the class TextEditing to include the possibility of highlighting and copying, excising and pasting text. The copied or excised text is saved in a new variable called savedText. There are also methods for opening a file and saving a file. Note that all the methods from TextEditing are available in TextEditor by means of the inheritance relationship between the two classes. This means that the characters in an "open file" are divided between the two stacks inherited from TextEditing; the cursor position and how it moves around the file is managed in exactly the same way as before. We say that the first character of the open file is the character at the bottom of the stack fileFront, if it is nonempty, and the character at the top of the stack fileEnd, otherwise. Characters in the open file are numbered from the first character. [Where is the last character in the open file?]

1. Look at the method stubs in TextEditing and implement those which are as yet unimplemented. In summary, these are:

Character delete(); // Removes the character at cursorPosition
//(if there is one)
// and returns that character.
// Does nothing if cursorPosition is zero.

void left(); // Moves the cursor one place to the left if
// cursorPosition is greater than zero, (i.e. if
// fileFront is not empty.)

void right() ; // Moves the cursor one place to the right if
// cursorPosition is less than than the total
//number of characters
// (i.e. if fileEnd is not empty.)

Character get(int i); // Moves the cursor to position i (if it //exists) and returns the character
//found there. If it does not exist then
//changes nothing.

void front(); // Puts the cursor at the very front of the file,
//i.e. so that fileFront is empty, and all the
//characters are in fileEnd.

[Hint: For these methods you must change fileFront and fileEnd so that they correctly model the cursor's position.]

Please note: You may make full use of the methods available in the Java stack library. You may familiarise yourself with that library by consulting the Java API. Note also that your methods must maintain the relation between the cursor, cursorPosition, fileFront and fileEnd.

2. Write JUnit tests to check your work. For each test state in its comments exactly what the test is for.

3. In the class TextEditor implement the following method stubs:

public TextEditor(); // Constructor: this must call the
//TextEditing's constructor and
// initialises savedText (to empty).
// Hint: you must call TextEditing's
//constructor to initialise the
//inherited variables.

void selectAndCopy(int i, int j); // selects the characters
//between the i'th and
//j'th characters in the
//open file and stores
//them sequentially in
//savedText. After this
// the cursor position
//should be positioned
// at the i'th character.
// If the i'th and j'th
//characters do not exist,
// then it does nothing.

void selectAndCut(int i, int j); // selects the characters // between the i'th and
//j'th positions in the
// open file, stores them //in savedText, and then
//deletes them from
// the open file. The cursor
//position should be at the
// i-1'th position after
//the delete. Note we assume
// that i, j > 0.
// If the i'th and j'th
//characters do not exist,
// then it does nothing.

String save(); // This must override TextEditing's save
//method: in addition to returning the characters
//in fileFront and fileEnd, it must also
// delete the characters in fileFront and
// fileEnd, so that they are empty stacks.
// Hint: you should call TextEditing's save method
//before deleting.

4. Write JUnit tests to check your work. For each test state in the comments exactly what the test is for.

5. [HD and CR] Add some exceptions to selectAndCopy and selectAndCut to handle the error cases where i > j, or the buffers are empty. You will need to install your own exception class to do this. Do some research on how to write JUnit tests to test for correctly raised exceptions, and write some JUnit tests to test your exceptions in selectAndCopy and selectAndCut.

This assignment is structured to allow you to decide how much effort you want to expend for the return in marks that you might hope for. You can choose which bits of the functionality of the full application you feel confident about implementing and then only write those parts for a proportion of the maximum possible marks. So you can decide upfront whether you are shooting for a pass or a high distinction and know exactly how much work will be required to obtain that mark.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.