Question: Hello, I am having issues displayed the queue. Its printing out 'Arrays@7d417077'. In Main class, when I call System.out.println(queue); it prints out Arrays@7d417077' instead
Hello,
I am having issues displayed the queue. Its printing out 'Arrays@7d417077'. In Main class, when I call " System.out.println(queue);" it prints out Arrays@7d417077' instead of the input the user typed in. This also occurs when I dequeue the data.
- - - - - -
interface GenericQueue { void enqueue(T t); T dequeue(); boolean isEmpty(); boolean isFull(); int size();
}
- - --- -
import java.lang.reflect.*;
public class Arrays implements GenericQueue {
private T elements[];
private int front, rear;
private int size, max;
Arrays(Class element, int max)
{
front =0;
rear = -1;
size = 0;
this.max = max;
elements = (T[])Array.newInstance(element, max);
}
@Override
public void enqueue(T t) {
if (rear == max - 1) {
int j = 0;
for (int i = front; i <= rear; i++)
{
elements[j++] = elements[i];
}
front = 0;
rear = size - 1;
}
elements[++rear] = t;
size++;
}
@Override
public T dequeue() {
T saved = elements[front];
elements[front] = null;
front++;
size--;
return saved;
}
@Override
public boolean isEmpty() {
return(size == 0);
}
@Override
public boolean isFull() {
return size == max;
}
@Override
public int size() {
return size;
}
}
- - - - - -
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void showMainMenu (GenericQueue queue) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter the following selections");
System.out.println("1. Enqueue ");
System.out.println("2. Dequeue ");
System.out.println("0. Quit");
String choice = scan.nextLine();
while (true)
{
switch (choice) {
case "":
showMainMenu(queue);
break;
case "0":
System.exit(0);
break;
case "1":
Add(queue, choice);
break;
case "2":
Remove(queue, choice);
break;
default: showMainMenu(queue);
break;
}
}
}
public static void Add (GenericQueue queue, String n)
{
T name;
T lastname;
T title;
T id;
Scanner scan = new Scanner(System.in); // creates scanner
//LinkedQueue queue = new LinkedQueue(); // declares linkedlist
/** Add names **/
System.out.println(" Enter the first and last name of the Employee:" );
name = (T) scan.next();
while(!((String) name).matches("[a-zA-Z]+"))
{
System.out.println(" Invalid first name: Enter a valid first name" );
name = (T) scan.next();
}
queue.enqueue(name);
lastname = (T) scan.next();
queue.enqueue(lastname);
System.out.println(" Enter the job title of the Employee:" );
title = (T) scan.next();
while(!((String) title).matches("[a-zA-Z]+"))
{
System.out.println(" Invalid first name: Enter a valid first name" );
title = (T) scan.next();
}
queue.enqueue(title);
System.out.println(" Enter the ID of the Employee:" );
id = (T) scan.next();
queue.enqueue(id);
System.out.println(" You added Employee: ");
System.out.println(queue);
showMainMenu(queue);
}
public static void Remove (GenericQueue queue, String n)
{
try {
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
System.out.println(" Employee list is Dequeue: ");
System.out.println(queue);
showMainMenu(queue);
}catch(QueueException ex) {
System.out.println(" There are no Employee in the database to Dequeue " + " Please enter in Employee information:");
Add(queue, n);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
GenericQueue queue = new Arrays(String.class, 4);
System.out.println("Please Enter the following selections");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("0. Quit");
String choice = scan.nextLine();
String name = null;
while (true) {
switch (choice) {
case "":
showMainMenu(queue);
break;
case "0":
System.exit(0);
break;
case "1":
Add(queue,name);
break;
case "2":
Remove(queue, name);
break;
default: showMainMenu(queue);
break;
}
}
}
}
- -- - -
public class QueueException extends RuntimeException {
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
