Please help with this question, I understand if you cannot do alot of questions,
ID: 667115 • Letter: P
Question
Please help with this question, I understand if you cannot do alot of questions, Although some help or hints would be helpfull. I tried using pop() to delete an item in the stack but it won't allow me for some reason. And searching on the internet for help only leads to dead ends. Thanks!!
package textEditing;
import java.util.*;
public class TextEditing {
Stack<Character> fileFront; // An open file consists of a front and end...
Stack<Character> fileEnd; // The cursor is at the boundary of the fileFront and fileEnd.
int cursorPosition; // This is the length of fileFront
int sizeOfFile; // This is the sum of the lengths of the two halves.
public TextEditing() {
fileFront= new Stack<Character>();
fileEnd= new Stack<Character>();
cursorPosition= 0;
sizeOfFile= 0;
}
void insert(Character c) { // inserts a character c just after the cursor.
fileFront.push(c);
cursorPosition++;
sizeOfFile++;
}
Character delete() { if(cursorPosition > 0){
// Removes the character at cursorPosition (if there is one)
// and returns that character.
// Does nothing if cursorPosition is zero.
// TO IMPLEMENT
}
}
}
void left() { // Moves the cursor one place to the left if
// cursorPosition is greater than zero, (i.e. if
// fileFront is not empty.)
// TO IMPLEMENT
}
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.)
// TO IMPLEMENT
}
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.
// TO IMPLEMENT
}
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
//TO IMPLEMENT
}
String save() {
String temp = null;
for(int i=1; i<= sizeOfFile; i++) {
temp= temp + get(i);
}
return temp;
}
}
...................................................................................................................................................................
// Below is Another section using inheritance to connect to and extend TextEditing...
......................................................................................................................................................................
package textEditing;
import java.io.*;
import java.util.Vector;
public class TextEditor extends TextEditing {
// A Text editor inherits the basic editing functions, and embellishes the class with a buffer to save text.
Vector<Character> savedText;
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.
// TO IMPLEMENT
}
TextEditor(String stored){ // Opens a saved file "stored" with the cursor at the front
super();
for(int k=0; k < stored.length(); k++) {
insert(stored.charAt(k));
}
front();
}
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
// TO IMPLEMENT
}
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.
// TO IMPLEMENT
}
void paste() { // pastes the items in savedText at the cursor
for(int k=0; k < savedText.size(); k++)
insert(savedText.elementAt(k));
}
@Override
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.
// TO IMPLEMENT
}
public static void main(String[] args) {
//String saved= "abc";
// TextEditor Emacs= new TextEditor(saved);
//Emacs.selectAndCopy(1,2);
// System.out.println(Emacs.savedText.toString());
}
}
Explanation / Answer
Please check the below program-
import java.util.*;
public class TextEditing {
Stack<Character> fileFront; // An open file consists of a front and end...
Stack<Character> fileEnd; // The cursor is at the boundary of the fileFront and fileEnd.
int cursorPosition; // This is the length of fileFront
int sizeOfFile; // This is the sum of the lengths of the two halves.
public TextEditing() {
fileFront= new Stack<Character>();
fileEnd= new Stack<Character>();
cursorPosition= 0;
sizeOfFile= 0;
}
void insert(Character c) { // inserts a character c just after the cursor.
fileFront.push(c);
cursorPosition++;
sizeOfFile++;
}
public static String removeCharAt(String s, int pos)
{
return s.substring(0, pos) + s.substring(pos + 1);
// Removes the character at cursorPosition (if there is one)
// and returns that character.
// Does nothing if cursorPosition is zero.
// TO IMPLEMENT
}
}
}
void left() { // Moves the cursor one place to the left if
// cursorPosition is greater than zero, (i.e. if
// fileFront is not empty.)
// TO IMPLEMENT
}
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.)
// TO IMPLEMENT
}
String text = "foo";
char a_char = text.charAt(0);
System.out.println( a_char ); // Prints f
}
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
//TO IMPLEMENT
}
String save() {
String temp = null;
for(int i=1; i<= sizeOfFile; i++) {
temp= temp + get(i);
}
return temp;
}