Question

In: Computer Science

public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); //...

public class StackTest
{
    public static void main(String[] args)
    {
        StackX theStack = new StackX(10);  // make new stack
        theStack.push(20);               // push items onto stack
        theStack.push(30);
        theStack.push(40);
        theStack.push(40);
        theStack.push(60);
        theStack.push(80);

        theStack.showStack();

        System.out.println("removeDownTo(40)");
        theStack.removeDownTo(40);

        theStack.showStack();

    }  // end main()
}
public class QueueTest
{
   public static void main(String[] args)
      {
      Queue theQueue = new Queue(20);  // queue holds 5 items

      theQueue.insert(10);            // insert 4 items
      theQueue.insert(20);
      theQueue.insert(30);
      theQueue.insert(40);

      theQueue.showQueue();
      
      System.out.println("Removing 3 items");
      theQueue.remove();              // remove 3 items
      theQueue.remove();              //    (10, 20, 30)
      theQueue.remove();
      
      theQueue.showQueue();

      System.out.println("Inserting 4 more items");
      theQueue.insert(50);            // insert 4 more items
      theQueue.insert(60);            //    (wraps around)
      theQueue.insert(70);
      theQueue.insert(80);
      theQueue.showQueue();

      System.out.println("Calling removeSecond()");
      long second = theQueue.removeSecond();
      theQueue.showQueue();

      }  // end main()
   }  

Attached Files:

  • QueueTest.java (942 B)
  • StackTest.java (507 B)

You may not use the java.util.Stack class.
You may not use the java.util.Queue class.

Ex1: Write a program that converts decimal to binary using a stack.

StackX class:
Ex2: Write method showStack(): It displays the contents of the stack starting with the first inserted element to the last. The stack would contain the same elements after showStack(). . If for example if to an empty stackx we push(20) then push(30) showStack() would print 20 30

Ex3: Write a method public static void removeDownTo (long n): It pops all values off the stack down to but not including the first element it sees that is equal to the second parameter. If none are equal, leave the stack empty.

Attach your StackX.java. I will use StackTest.java to test the above methods.

Queue class:
Ex4: Write method showQueue() to display the contents of the queue from first to last. The queue would contain the same elements after showQueue().

Ex5: Write a method public static Object removeSecond (): It removes and returns the element just behind the front element. Precondition: The queue has at least two elements.

Attach your Queue.java. I will use QueueTest.java to test the above methods.

Solutions

Expert Solution

Answer :2.,3

package stackOperations;
import java.util.Stack;

public class StackX {

   int st_elements[] = new int[10];
   int tos;

   StackX() {
       tos = -1; // indicate it's empty
   }

   public void push(int item) // for inserting element into stack in stack order
   {
       if (tos == 10) {
           System.out.println("stack is full");
           return;
       } else {
           st_elements[++tos] = item;
       }
   }

   public boolean empty_stack() // check that stack is empty or not
   {
       if (tos == -1)
           return true;
       else
           return false;
   }

   public int pop() // for retrieving element from stack
   {
       if (tos < 0) {
           System.out.println("stack underflow");
           return 0;
       } else
           return st_elements[tos--];
   }

   public boolean isEmpty() {
       return tos == -1;
   }

   public void showStack() // for printing the stack contains
   {
       int temp = 0;

       if (!empty_stack()) {
           temp = pop();
           System.out.println(temp);
           showStack();
       }
       push(temp);
   }

   public long peek() {
       return st_elements[tos];
   }

   static void removeDownTo(final StackX stack, final long downTo) {
       while (!stack.isEmpty()) {
           if (stack.peek() == downTo) {
               return;
           }

       }
   }
}


Related Solutions

public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT