Hello, I have taken only some programming and am just beginning to really learn some little more advanced Java. When I was programming this I took some code I was supplied with and modified it.
The only problem I am really having is that I cannot figure out why my enqueue() method won't accept string arrays and why the NewArray() method is not found. Can anybody help me?
Here is my code:
ListQueue.java
// ListQueue class
//
// CONSTRUCTION: with no initializer
//
// ******************PUBLIC OPERATIONS*********************
// void enqueue( x ) --> Insert x
// String[] getFront( ) --> Return least recently inserted item
// String[] dequeue( ) --> Return and remove least recent item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS************************** ******
// getFront or dequeue on empty queue
/**
* List-based implementation of the queue.
*/
import java.util.Arrays;
import java.io.*;
import java.util.Scanner;
import java.lang.Object;
public class ListQueue implements QueueInterface{
/**
* Construct the queue.
*/
public ListQueue( ) {
front = back = null;
}
/**
* Test if the queue is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( ) {
return front == null;
}
/**
* Insert a new item into the queue.
* @param x the item to insert.
*/
public void Enqueue( String[] x ) {
if( isEmpty( ) ) // Make queue of one element
back = front = new ListNode( x );
else // Regular case
back = back.next = new ListNode( x );
}
/**
* Return and remove the least recently inserted item
* from the queue.
* @return the least recently inserted item in the queue.
* @throws UnderflowException if the queue is empty.
*/
public String[] dequeue( ) {
if( isEmpty( ) )
throw new UnderflowException( "ListQueue dequeue" );
String[] returnValue = front.element;
front = front.next;
return returnValue;
}
/**
* Get the least recently inserted item in the queue.
* Does not alter the queue.
* @return the least recently inserted item in the queue.
* @throws UnderflowException if the queue is empty.
*/
public String[] getFront( ) {
if( isEmpty( ) )
throw new UnderflowException( "ListQueue getFront" );
return front.element;
}
/**
* Make the queue logically empty.
*/
public void makeEmpty( ) {
front = null;
back = null;
}
private ListNode front;
private ListNode back;
//New stuff
class NewArray{
String [] arr;
public String [] NewArray() {
arr = new String [10];
fill();
return arr;
}
public void sort(){
Arrays.sort(arr);
}
public void fill(){
for(int i=0; i<=10; i++){
arr[i] = ReadThread(); // have program read words from file eventually
// arr[i] = names.next(); ?
}
}
public String ReadThread(){
String dummy = "missing";
try{
File numbersFile = new File("numbers.txt");
File namesFile = new File("names.txt");
Scanner numbers = new Scanner(numbersFile);
Scanner names = new Scanner(namesFile);
dummy = names.next();
}
catch(FileNotFoundException e){
System.out.println("Not Found");
}
return dummy;
}
}
}
/**
* Exception class for access in empty containers
* such as stacks, queues, and priority queues.
*/
class ListNode {
// Constructors
public ListNode( String[] theElement ) {
this( theElement, null );
}
public ListNode( String[] theElement, ListNode n ) {
element = theElement;
next = n;
}
public String[] element;
public ListNode next;
}
QueueInterface.java
// Queue interface
//
// ******************PUBLIC OPERATIONS*********************
// void enqueue( x ) --> Insert x
// Object getFront( ) --> Return least recently inserted item
// Object dequeue( ) --> Return and remove least recent item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS************************** ******
// getFront or dequeue on empty queue
/**
* Protocol for queues.
* @author Mark Allen Weiss
*/
public interface QueueInterface {
/**
* Insert a new item into the queue.
* @param x the item to insert.
*/
void Enqueue( String[] x );
/**
* Get the least recently inserted item in the queue.
* Does not alter the queue.
* @return the least recently inserted item in the queue.
* @exception UnderflowException if the queue is empty.
*/
String[] getFront( );
/**
* Return and remove the least recently inserted item
* from the queue.
* @return the least recently inserted item in the queue.
* @exception UnderflowException if the queue is empty.
*/
String[] dequeue( );
/**
* Test if the queue is logically empty.
* @return true if empty, false otherwise.
*/
boolean isEmpty( );
/**
* Make the queue logically empty.
*/
void makeEmpty( );
}
TestQueue.java
import java.lang.Object;
public class TestQueue{
public static void main(String [] args){
//Add code here to start threads
String [] arrayname = new String [10];
ListQueue queue = new ListQueue();
Enqueue(arrayname);
arrayname = NewArray();
Enqueue(NewArray());
Enqueue(arrayname);
}
}
UnderflowException.java
public class UnderflowException extends RuntimeException {
/**
* Construct this exception object.
* @param message the error message.
*/
public UnderflowException( String message ) {
super( message );
}
}