Question
QueueList.java: package MyQueue; public class QueueList { private List a_queue; public QueueList() { a_queue = new List( queue ); } public Object peek() throws EmptyListException
QueueList.java:
package MyQueue;
public class QueueList
{
private List a_queue;
public QueueList()
{
a_queue = new List( "queue" );
}
public Object peek() throws EmptyListException
{
if (a_queue.isEmpty())
return null;
else
return a_queue.getFirstObject();
}
public void print()
{
a_queue.print();
}
//implement the enqueue(), dequeue() and empty() methods below:
}
EmptyListException.java:
package MyQueue;
public class EmptyListException extends RuntimeException { // no-argument constructor public EmptyListException() { this( "List" ); // call other EmptyListException constructor } // end EmptyListException no-argument constructor
// one-argument constructor public EmptyListException( String name ) { super( name + " is empty" ); // call superclass constructor } // end EmptyListException one-argument constructor } // end class EmptyListException
List.java:
package MyQueue;
public class List
{
private ListNode firstNode;
private ListNode lastNode;
private String name; // string like "list" used in printing
// constructor creates empty List with "list" as the name
public List()
{
this( "list" );
} // end List no-argument constructor
// constructor creates an empty List with a name
public List( String listName )
{
name = listName;
firstNode = lastNode = null;
} // end List one-argument constructor
public Object getFirstObject(){
return firstNode.getData();
}
public Object getLastObject() {
return lastNode.getData();
}
// insert Object at front of List
public void insertAtFront( Object insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode( insertItem );
else // firstNode refers to new node
firstNode = new ListNode( insertItem, firstNode );
} // end method insertAtFront
// insert Object at end of List
public void insertAtBack( Object insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same Object
firstNode = lastNode = new ListNode( insertItem );
else // lastNode's nextNode refers to new node
lastNode = lastNode.nextNode = new ListNode( insertItem );
} // end method insertAtBack
// remove first node from List
public Object removeFromFront() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
Object removedItem = firstNode.data; // retrieve data being removed
// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem; // return removed node data
} // end method removeFromFront
// remove last node from List
public Object removeFromBack() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
Object removedItem = lastNode.data; // retrieve data being removed
// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else // locate new last node
{
ListNode current = firstNode;
// loop while current node does not refer to lastNode
while ( current.nextNode != lastNode )
current = current.nextNode;
lastNode = current; // current is new lastNode
current.nextNode = null;
} // end else
return removedItem; // return removed node data
} // end method removeFromBack
// determine whether list is empty
public boolean isEmpty()
{
return firstNode == null; // return true if list is empty
} // end method isEmpty
// output list contents
public void print()
{
if ( isEmpty() )
{
System.out.println( "Empty " + name );
return;
} // end if
System.out.print( "The " + name + " is: ");
ListNode current = firstNode;
// while not at end of list, output current node's data
while ( current != null )
{
System.out.print(current.data + " " );
current = current.nextNode;
} // end while
System.out.println( " " );
} // end method print
// ListNode inner class definition.
// class to represent one node in a list
private class ListNode
{
//List can access these directly
public Object data; // data for this node
public ListNode nextNode; // reference to the next node in the list
// constructor creates a ListNode that refers to object
public ListNode( Object object )
{
this( object, null );
} // end ListNode one-argument constructor
// constructor creates ListNode that refers to
// Object and to next ListNode
public ListNode( Object object, ListNode node )
{
data = object;
nextNode = node;
} // end ListNode two-argument constructor
// return reference to data in node
public Object getData()
{
return data; // return Object in this node
} // end method getObject
// return reference to next node in list
public ListNode getNext()
{
return nextNode; // get next node
} // end method getNext
} // end class ListNode
} // end class List
PigLatin.java
import MyQueue.QueueList; import java.util.Scanner;
public class PigLatin { public static void main (String[] args) { Scanner scan = new Scanner (System.in);
QueueList word = new QueueList();
String message; int index = 0; char firstch;
System.out.println ("Enter an English sentence: "); message = scan.nextLine(); System.out.println ("The equivalent Pig Latin sentence is: ");
// complete the code below to convert input message to Pig Latin sentence
} }
Decode.java:
import java.util.*; import MyQueue.QueueList; //QueueList is inside a package named MyQueue
public class Decode {
public static void main (String[] args) { Scanner scan = new Scanner (System.in);
QueueList myQ = new QueueList();
String message; int index = 0;
System.out.println ("Enter the message:"); message = scan.nextLine(); System.out.println ("The message is:");
while (index
// Print word in original order while (!myQ.empty()) System.out.print (((Character)myQ.dequeue()).charValue()); System.out.print (" "); index++; }
System.out.println(); } }
Open the folder Lab8_part3 and then the program file QueueList.java, implement your own Queue data type named QueueList, with the Linked List (EmpyListExceptoion.java and List.java) provided by your instructor. Your QueueList class must have the following three methods: enqueue), dequeue0, empty Use the modified version of decode java program inside Lab8part3 folder for testing your QueueList class Next is to write a program (PigLatinjava) to transfom an English sentence into Pig Latin format. Without worrying about punctuations and other special characters, our simplified rules for the transformation are as follow: The first letter of each word is placed at the end of the word. If that first letter is a vowel, append "y" at the end, otherwise append "ay" at the end. See a sample run of the program below: Enter an English sentence: This Is a java program The equivalent Pig Latin sentence is: hisTay sly ay avajay rogrampay Open the folder Lab8_part3 and then the program file QueueList.java, implement your own Queue data type named QueueList, with the Linked List (EmpyListExceptoion.java and List.java) provided by your instructor. Your QueueList class must have the following three methods: enqueue), dequeue0, empty Use the modified version of decode java program inside Lab8part3 folder for testing your QueueList class Next is to write a program (PigLatinjava) to transfom an English sentence into Pig Latin format. Without worrying about punctuations and other special characters, our simplified rules for the transformation are as follow: The first letter of each word is placed at the end of the word. If that first letter is a vowel, append "y" at the end, otherwise append "ay" at the end. See a sample run of the program below: Enter an English sentence: This Is a java program The equivalent Pig Latin sentence is: hisTay sly ay avajay rogrampayStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started