This is what i have so far. I am getting an Error in Part two .. please see belo
ID: 667130 • Letter: T
Question
This is what i have so far. I am getting an Error in Part two .. please see below for the error. I need help getting it to run.
Part 1:
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author
*
*/
public class StackWAS {
public StackWAS(){ //constructor
}
static void push(Stack st, int num) { //method to push element to stack
if(!isFull(st)){
st.push(new Integer(num));
}
else{
System.out.println("Stack is full");
}
}
static void pop(Stack st) { //method to pop element from stack
st.pop();
}
static boolean isEmpty(Stack st){ //checking stack empty or not
boolean res = false;
if(st.capacity() == 0){
res = true;
}
return res;
}
static boolean isFull(Stack st){//checking stack full or not
boolean res = false;
if(st.capacity() == 20){
res = true;
}
return res;
}
static void toString (Stack st){ //string representation
System.out.println("Stack contains: "+st);
}
public static void main(String args[]) {
Stack st = new Stack();//creating object
Scanner sc = new Scanner(System.in);
while(true){
//menu dispalying
System.out.println("1. Push 2. Pop 3. isEmpty 4. isFull 5. Display 6. Exit");
int choice = sc.nextInt(); //reading choice
if(choice == 1){
System.out.println("Enter number to push");
int number = sc.nextInt();
push(st,number);
}
else if(choice == 2){
pop(st);
System.out.println("Number Popped from stack");
}
else if(choice == 3){
System.out.println("Stack Empty? " + isEmpty(st));
}
else if(choice == 4){
System.out.println("Stack Full? " + isFull(st));
}
else if(choice==5){
System.out.println("Stack contains: "+st);
}
else{ //safely exiting
break;
}
}
}
}
Part 2 I am having issues getting it to compile and run. Can you please help fix it according to what is needed from above:
My Error is: Cannot find symbol - class LinkedQueueList
import java.util.LinkedQueueList;
class QueueWAS {
private LinkedQueueList QueueList = new LinkedQueueList(); //linkedlist to store data
//methods to insert , dequeue, isEmpty
public void enqueue(int num) {
QueueList.addLast(num);
}
public E dequeue() {
return QueueList.poll();
}
public boolean isEmpty() {
return !QueueList.isEmpty();
}
}
public class GenericQueueTest {
public static void main(String[] args) {
GenericQueue empQueueList = new GenericQueue(); //queue declaring
//menu dispalying
System.out.println("1. Enqueue 2.Eequeue 3.isEmpty 4.Display 6.Exit");
int choice = sc.nextInt(); //reading choice
if(choice == 1){
System.out.println("Enter number to enqueue");
int number = sc.nextInt();
empQueueList.enqueue(number);
}
else if(choice == 2){
System.out.println("Number Dequeued from Queue: "+empQueueList.dequeue());
}
else if(choice == 3){
System.out.println("Queue Empty? " + empQueueList.isEmpty());
}
else if(choice==4){
//printing elements
while (empQueueList.isEmpty()) {
System.out.println("Elements in Queue are: "+empQueueList.dequeue());
}
}
else{ //safely exiting
break;
}
}
}
Explanation / Answer
public function LinkedQueue(list:SLinkedList = null)
Initializes a new queue. You can pass an existing singly linked list to provide queue-like access.
Parameters
list:SLinkedList (default = null) — An existing list to become the queue.
import java.util.LinkedQueueList; there is no LinkedQueueList better to import java.util.*;
First save this class in a seperate file named with StackWAS.java
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author
*
*/
public class StackWAS {
public StackWAS(){ //constructor
}
static void push(Stack st, int num) { //method to push element to stack
if(!isFull(st)){
st.push(new Integer(num));
}
else{
System.out.println("Stack is full");
}
}
static void pop(Stack st) { //method to pop element from stack
st.pop();
}
static boolean isEmpty(Stack st){ //checking stack empty or not
boolean res = false;
if(st.capacity() == 0){
res = true;
}
return res;
}
static boolean isFull(Stack st){//checking stack full or not
boolean res = false;
if(st.capacity() == 20){
res = true;
}
return res;
}
static void toString (Stack st){ //string representation
System.out.println("Stack contains: "+st);
}
public static void main(String args[]) {
Stack st = new Stack();//creating object
Scanner sc = new Scanner(System.in);
while(true){
//menu dispalying
System.out.println("1. Push 2. Pop 3. isEmpty 4. isFull 5. Display 6. Exit");
int choice = sc.nextInt(); //reading choice
if(choice == 1){
System.out.println("Enter number to push");
int number = sc.nextInt();
push(st,number);
}
else if(choice == 2){
pop(st);
System.out.println("Number Popped from stack");
}
else if(choice == 3){
System.out.println("Stack Empty? " + isEmpty(st));
}
else if(choice == 4){
System.out.println("Stack Full? " + isFull(st));
}
else if(choice==5){
System.out.println("Stack contains: "+st);
}
else{ //safely exiting
break;
}
}
}
}
/*{
public static void main(String[]args){
System.out.println("Hello World!");
}
}*/
second file named with QueueWAS.java
import java.util.*;
class QueueWAS implements Queue <QueueList> {
private LinkedQueue <QueueList> = new LinkedQueue(); //linkedlist to store data
//methods to insert , dequeue, isEmpty
public void enqueue(int num) {
QueueList.addLast(num);
}
public E dequeue() {
return QueueList.poll();
}
public boolean isEmpty() {
return !QueueList.isEmpty();
}
}
third file GenericQueueTest.java
public class GenericQueueTest {
public static void main(String[] args) {
GenericQueue empQueueList = new GenericQueue(); //queue declaring
//menu dispalying
System.out.println("1. Enqueue 2.Eequeue 3.isEmpty 4.Display 6.Exit");
int choice = sc.nextInt(); //reading choice
if(choice == 1){
System.out.println("Enter number to enqueue");
int number = sc.nextInt();
empQueueList.enqueue(number);
}
else if(choice == 2){
System.out.println("Number Dequeued from Queue: "+empQueueList.dequeue());
}
else if(choice == 3){
System.out.println("Queue Empty? " + empQueueList.isEmpty());
}
else if(choice==4){
//printing elements
while (empQueueList.isEmpty()) {
System.out.println("Elements in Queue are: "+empQueueList.dequeue());
}
}
else{ //safely exiting
break;
}
}
}
I made some changes to your program this code works perfectly
LinkedQueue () constructorRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.