Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will create a class similar to the List ADT. Simulate a

ID: 3744877 • Letter: I

Question

In this assignment, you will create a class similar to the List ADT.

Simulate a simple text editor, which stores a string of characters using the List ADT which has been endowed with some additional features. We will call this endowed List ADT as a Cursor class. Normally, you would implement the Cursor class using inheritance, that is, the Cursor class would be a derived class of the List ADT which would serve as the base class. However, in this assignment we will implement the Cursor class from scratch and model it after the List ADT.

You must use a singly linked list. Do not use any iterators to traverse the list. The reason is I want you to manipulate the linked lists directly.

Your editor should support the following operations and redisplay the current text (that is, the list) after performing any one of them.

left : move cursor left one character (or do nothing if at the beginning of the text)

right : move cursor right one character (or do nothing if at the end of the text)

delete : delete the character to the right of the cursor (or do nothing if at the end of

the text)

back : delete the character to the left of the cursor (or do nothing if at the

beginning of the text)

insert c : insert the character c just after the cursor.

home: move cursor to the beginning of the text (or do nothing if at the beginning

of the text)

end: move cursor to the end of the text (or do nothing if at the end of the text)

undo: undo the last insert/delete/back operation

(You can assume that between any two consecutive “undo” commands issued by the user, there will be at least one insert/delete/back operation.)

The Cursor class definition is provided to you. You can only add member variables or member functions to the definition provided. You cannot remove any declaration from the class definition1. You have to write the implementation for the Cursor class and a driver program that will allow the user to enter text to use your “text editor”. The user will input:

• l-forleft • r-forright

• d - for delete

1 If you need to make any deletions because of recent updates to the C++ language, please bring it to my attention immediately.

b-forback

i c - for insert character c

h–forhome

e–forend

u–forundo
(l, r, d, b, i, h, e and u will be input in lower case only)

After each of the above operations you will display the entire text with a ‘|’ indicating where the cursor is currently positioned.

For example, the following table gives a sequence of user commands and what will be displayed after each command:

user command

Displayed text

id

d|

ia

da|

it

dat|

ia

data|

l

dat|a

l

da|ta

b

d|ta

r

dt|a

u

da|ta

b

d|ta

ie

de|ta

il

del|ta

The user will enter the $ character when they are done.

(FIRST EXTRA CREDIT 50 points) Allow the user to undo up to the last 10 insert/delete/back operations. (Hint: You will have to use a stack for this.). For this, you cannot assume that between any two consecutive “undo” commands issued by the user, there will be at least ten insert/delete/back operations.

(SECOND EXTRA CREDIT 50 points) Allow the user to delete/back a contiguous group of characters – this command will be specified by d or b followed by a number. For e.g., d5 will delete 5 characters counting forward from the current position of the cursor. You can assume a valid number will be input. That is, if the user inputs d5, you can assume that there are at least 5 characters counting forward from the current position of the cursor. Note that if you choose to implement this option, the undo operation should work in this case also (just like in a real editor such as Word).

user command

Displayed text

id

d|

ia

da|

it

dat|

ia

data|

l

dat|a

l

da|ta

b

d|ta

r

dt|a

u

da|ta

b

d|ta

ie

de|ta

il

del|ta

Explanation / Answer


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class SimpleTexteditor extends JFrame implements ActionListener
{
MenuBar mbar;
Menu file,edit,format,font,font1,font2;
MenuItem item1,item2,item3,item4;
MenuItem item5,item6,item7,item8,item9,item10;
MenuItem fname1,fname2,fname3,fname4;
MenuItem fstyle1,fstyle2,fstyle3,fstyle4;
MenuItem fsize1,fsize2,fsize3,fsize4;

JPanel mainpanel;
TextArea text;

Font f;
String months[]={
"Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};

GregorianCalendar gcalendar;


String command=" ";
String str=" ";

String str1=" ",str2=" ",str3=" ";
String str4=" ";

String str6=" ";
String str7=" ",str8=" ",str9=" ";

int len1;

int i=0;
int pos1;
int len;

public Texteditor(String str)
{

super(str);

mainpanel=new JPanel();
mainpanel=(JPanel)getContentPane();
mainpanel.setLayout(new FlowLayout());


mbar=new MenuBar();
setMenuBar(mbar);

file=new Menu("File");
edit=new Menu("Edit");
format=new Menu("Format");
font=new Menu("Font");
font1=new Menu("Font Style");
font2=new Menu("Size");

file.add(item1=new MenuItem("New..."));
file.add(item2=new MenuItem("Open"));
file.add(item3=new MenuItem("Save As..."));
file.add(item4=new MenuItem("Exit"));
mbar.add(file);


edit.add(item5=new MenuItem("Cut (Ctrl+X)"));
edit.add(item6=new MenuItem("Copy (Ctrl+C)"));
edit.add(item7=new MenuItem("Paste (Ctrl+V)"));
edit.add(item8=new MenuItem("Delete"));
edit.add(item10=new MenuItem("Select All (Ctrl+A)"));
edit.add(item9=new MenuItem("Time/Date"));
mbar.add(edit);

format.add(font);
format.add(font1);
format.add(font2);

font.add(fname1=new MenuItem("Courier"));
font.add(fname2=new MenuItem("Sans Serif"));
font.add(fname3=new MenuItem("Monospaced"));
font.add(fname4=new MenuItem("Symbol"));

font1.add(fstyle1=new MenuItem("Regular"));
font1.add(fstyle2=new MenuItem("Bold"));
font1.add(fstyle3=new MenuItem("Italic"));
font1.add(fstyle4=new MenuItem("Bold Italic"));

font2.add(fsize1=new MenuItem("12"));
font2.add(fsize2=new MenuItem("14"));
font2.add(fsize3=new MenuItem("18"));
font2.add(fsize4=new MenuItem("20"));

mbar.add(format);

item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
item10.addActionListener(this);
fname1.addActionListener(this);
fname2.addActionListener(this);
fname3.addActionListener(this);
fname4.addActionListener(this);
fstyle1.addActionListener(this);
fstyle2.addActionListener(this);
fstyle3.addActionListener(this);
fstyle4.addActionListener(this);
fsize1.addActionListener(this);
fsize2.addActionListener(this);
fsize3.addActionListener(this);
fsize4.addActionListener(this);


text=new TextArea(26,60);
mainpanel.add(text);

f=new Font("Monotype Corsiva",Font.PLAIN,15);
text.setFont(f);
}


public void actionPerformed(ActionEvent ae)
{


command=(String)ae.getActionCommand();

if(command.equals("New..."))
{
dispose();
Texteditor note1 = new Texteditor("Untitled-Notepad");
note1.setSize(500,500);
note1.setVisible(true);
}

try
{

if(command.equals("Open"))
{

str4=" ";
FileDialog dialog=new FileDialog(this,"Open");
dialog.setVisible(true);

str1=dialog.getDirectory();
str2=dialog.getFile();
str3=str1+str2;
File f=new File(str3);
FileInputStream fobj=new FileInputStream(f);
len=(int)f.length();
for(int j=0;j<len;j++)
{
char str5=(char)fobj.read();
str4=str4 + str5;

}

text.setText(str4);

}
}
catch(IOException e)
{}


try
{

if(command.equals("Save As..."))
{
FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
dialog1.setVisible(true);

str7=dialog1.getDirectory();
str8=dialog1.getFile();
str9=str7+str8;


str6=text.getText();
len1=str6.length();
byte buf[]=str6.getBytes();

File f1=new File(str9);
FileOutputStream fobj1=new FileOutputStream(f1);
for(int k=0;k<len1;k++)
{
fobj1.write(buf[k]);
}
fobj1.close();
}

this.setTitle(str8);

}
catch(IOException e){}

if(command.equals("Exit"))
{
System.exit(0);
}

if(command.equals("Cut (Ctrl+X)"))
{
str=text.getSelectedText();
i=text.getText().indexOf(str);
text.replaceRange(" ",i,i+str.length());
}

if(command.equals("Copy (Ctrl+C)"))
{
str=text.getSelectedText();
}

if(command.equals("Paste (Ctrl+V)"))
{
pos1=text.getCaretPosition();
text.insert(str,pos1);
}
if(command.equals("Delete"))
{
String msg=text.getSelectedText();
i=text.getText().indexOf(msg);
text.replaceRange(" ",i,i+msg.length());
}
if(command.equals("Time/Date"))
{
gcalendar=new GregorianCalendar();
String h=String.valueOf(gcalendar.get(Calendar.HOUR));
String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
String s=String.valueOf(gcalendar.get(Calendar.SECOND));
String date=String.valueOf(gcalendar.get(Calendar.DATE));
String mon=months[gcalendar.get(Calendar.MONTH)];
String year=String.valueOf(gcalendar.get(Calendar.YEAR));
String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
int loc=text.getCaretPosition();
text.insert(hms,loc);
}
if(command.equals("Courier"))
{

String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Courier",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Sans Serif"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Sans Serif",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Monospaced"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Monospaced",fontStyle,fontSize);
text.setFont(f);
}

if(command.equals("Symbol"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Symbol",fontStyle,fontSize);
text.setFont(f);
System.out.println(f.getFamily());
}
if(command.equals("Regular"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.PLAIN,fontSize);
text.setFont(f);
}
if(command.equals("Bold"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD,fontSize);
text.setFont(f);
}
if(command.equals("Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.ITALIC,fontSize);
text.setFont(f);
}
if(command.equals("Bold Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
text.setFont(f);
}

if(command.equals("12"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,12);
text.setFont(f);
}

if(command.equals("14"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,14);
text.setFont(f);
}
if(command.equals("18"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,18);
text.setFont(f);
}
if(command.equals("20"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,20);
text.setFont(f);
}
if(command.equals("Select All (Ctrl+A)"))
{
String strText=text.getText();
int strLen=strText.length();
text.select(0,strLen);
}


}

public static void main(String args[])
{
Texteditor note = new Texteditor("Untitled-Notepad");
note.setSize(500,500);
note.setVisible(true);
}
}