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

Department of Computer Science. Georgia State University CSc 2720 Fall 2017 - As

ID: 3876704 • Letter: D

Question

Department of Computer Science.

Georgia State University

                           CSc 2720                                                        

Fall 2017 - Assignment #1

Due Thursday, September 14th 2017

Note: (1) For all assignments, always use comments to explain your program.

          (2) No copying allowed. If it is found that students copy from each other, all of these programs will get 0.

Documentation: 20%

Explain the purpose of the program as detail as possible 3%.

Develop a solution for the problem and mention algorithm used -12%

List data structures to be used in the solution. - 2%.

Give a description of how to use the program and expected input/output - 3%

Programming: 80%

For each method, give the pre and post conditions and invariants, if any - 5%

Program execution according to the requirements 50%

Naming of program as required 5%

Useful comments and readability: 20%

Description of program:

Write a program to implement a queue using stack. The file name should be MyQueue.java. Upload the .java file into A1 folder of icollege.

Hint: You can use two stacks and use one as the incoming stack and the other as the outgoing stack. A queue is a FIFO structure, so when we enqueue something, we need to make sure that it does not get popped off before something that was already there. Similarly, when we dequeue something, we have to make sure that elements that were inserted earlier get removed first.

Explanation / Answer

import java.util.*;

/* Class arrayStack */

class arrayStack

{

protected int array[];

//using int array for the implementation of stack

protected int topElement, sizeOfStack, length;

/* Constructor for arrayStack */

public arrayStack()

{

sizeOfStack = 5;

// I am using 5 as the default size of stack you can change as you want

length = 0;

array = new int[sizeOfStack];

topElement = -1;

}

/* Function to check if stack is empty */

public boolean isEmpty()

{

return topElement == -1;

}

/* Function to check if stack is full */

public boolean isFull()

{

return topElement == sizeOfStack -1 ;

}

/* Function to get the size of the stack */

public int getSize()

{

return length ;

}

/* Function to add an element to the stack */

public void push(int i)

{

if(topElement + 1 >= sizeOfStack)

throw new IndexOutOfBoundsException("Overflow Exception");

if(topElement + 1 < sizeOfStack )

array[++topElement] = i;

length++ ;

}

/* Function to delete an element from the stack */

public int pop()

{

if( isEmpty() )

throw new NoSuchElementException("Underflow Exception");

length-- ;

return array[topElement--];

}

/* Function to display the status of the stack */

public void display()

{

System.out.print(" Stack = ");

if (length == 0)

{

System.out.print("Empty ");

return ;

}

for (int i = topElement; i >= 0; i--)

System.out.print(array[i]+" ");

System.out.println();

}

}

/* Class StackImplement */

public class MyQueue

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.println("Stack Test ");

//int n = scan.nextInt();

/* Creating object of class arrayStack */

arrayStack stk = new arrayStack();

/* Perform Stack Operations */

char ch;

do{

System.out.println("Stack Operations");

System.out.println("1. push");

System.out.println("2. pop");

System.out.println("3. check empty");

int choice = scan.nextInt();

switch (choice)

{

case 1 :

System.out.println("Enter integer element to push");

try

{

stk.push( scan.nextInt() );

}

catch (Exception e)

{

System.out.println("Error : " + e.getMessage());

}

break;

case 2 :

try

{

System.out.println("Popped Element = " + stk.pop());

}

catch (Exception e)

{

System.out.println("Error : " + e.getMessage());

}

break;

case 3 :

System.out.println("Empty status = " + stk.isEmpty());

break;

default :

System.out.println("Wrong Entry ");

break;

}

/* display stack */

stk.display();

System.out.println(" Type any key to continue");

ch = scan.next().charAt(0);

} while (true);

}

}//End of MyQueue.java

Sample output:

Stack Test

Stack Operations

1. push

2. pop

3. check empty

1

Enter integer element to push

12

Stack = 12

Type any key to continue

1

Stack Operations

1. push

2. pop

3. check empty

1

Enter integer element to push

12

Stack = 12 12

Type any key to continue

1

Stack Operations

1. push

2. pop

3. check empty

1

Enter integer element to push

23

Stack = 23 12 12

Type any key to continue

r

Stack Operations

1. push

2. pop

3. check empty

1

Enter integer element to push

45

Stack = 45 23 12 12

Type any key to continue

33

Stack Operations

1. push

2. pop

3. check empty

1

Enter integer element to push

56

Stack = 56 45 23 12 12

Type any key to continue

2

Stack Operations

1. push

2. pop

3. check empty

2

Popped Element = 56

Stack = 45 23 12 12

Type any key to continue

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote