Question

In: Computer Science

In java, write a class that tests the following code. Add screen shots as well. public...

In java, write a class that tests the following code. Add screen shots as well.

public class DoubleStack<E> {
   private E[] elements;
   private int top1, top2;
   private static int DEFAULT_SIZE = 10;
  
   public DoubleStack() {
       elements = (E[])new Object[DEFAULT_SIZE];
       top1 = 0;
       top2 = elements.length - 1;
   }
  
   public boolean isFull(int stack) {
       if(top2 == top1 + 1)
           return true;
       else
           return false;
   }
  

   public boolean isEmpty(int stack) {
       if(stack == 1 && top1 == 0)
           return true;
       else if(stack == 2 && top2 == elements.length-1)
           return true;
       else
           return false;
   }
  
   public void push(int stack, E data) {
       if(isFull(stack))
           resize();
       if(stack == 1) {
           elements[top1] = data;
           top1++;
       }
       else if(stack == 2) {
           elements[top2] = data;
           top2--;
       }
   }
  
   public E peek(int stack) {
       if(!isEmpty(stack))
       {
           if(stack == 1)
               return elements[top1-1];
           else if(stack == 2)
               return elements[top2+1];
       }
       return null;

   }
  
   public E pop(int stack) {
       if(!isEmpty(stack))
       {
           if(stack == 1)
               return elements[--top1];
           else if(stack == 2)
               return elements[++top2];
       }
       return null;

   }
  
   public int size(int stack) {
       if(stack == 1)
           return top1;
       else if(stack == 2)
           return elements.length - 1 - top2;
       else
           return 0;
   }
  
   public void resize() {
       E[] temp = (E[])new Object[elements.length * 2];
       for(int i = 0; i < top1; i++){
           temp[i] = elements[i];
       }
      
       int i, j;
       for(i = elements.length - 1, j = temp.length-1; i > top2; i--, j--) {
           temp[j] = elements[i];
       }
      
       elements = temp;
      
       top2 = j;
      
   }
  
  
}

Solutions

Expert Solution

//Java program

class DoubleStack<E> {
   private E[] elements;
   private int top1, top2;
   private static int DEFAULT_SIZE = 10;
  
   public DoubleStack() {
   elements = (E[])new Object[DEFAULT_SIZE];
   top1 = 0;
   top2 = elements.length - 1;
   }
  
   public boolean isFull(int stack) {
   if(top2 == top1 + 1)
   return true;
   else
   return false;
   }
  

   public boolean isEmpty(int stack) {
   if(stack == 1 && top1 == 0)
   return true;
   else if(stack == 2 && top2 == elements.length-1)
   return true;
   else
   return false;
   }
  
   public void push(int stack, E data) {
   if(isFull(stack))
   resize();
   if(stack == 1) {
   elements[top1] = data;
   top1++;
   }
   else if(stack == 2) {
   elements[top2] = data;
   top2--;
   }
   }
  
   public E peek(int stack) {
   if(!isEmpty(stack))
   {
   if(stack == 1)
   return elements[top1-1];
   else if(stack == 2)
   return elements[top2+1];
   }
   return null;

   }
  
   public E pop(int stack) {
   if(!isEmpty(stack))
   {
   if(stack == 1)
   return elements[--top1];
   else if(stack == 2)
   return elements[++top2];
   }
   return null;

   }
  
   public int size(int stack) {
   if(stack == 1)
   return top1;
   else if(stack == 2)
   return elements.length - 1 - top2;
   else
   return 0;
   }
  
   public void resize() {
   E[] temp = (E[])new Object[elements.length * 2];
   for(int i = 0; i < top1; i++){
   temp[i] = elements[i];
   }
  
   int i, j;
   for(i = elements.length - 1, j = temp.length-1; i > top2; i--, j--) {
   temp[j] = elements[i];
   }
  
   elements = temp;
  
   top2 = j;
  
   }
  
  
   }

public class Stack{
   public static void main(String args[]){
       DoubleStack<Integer> st = new DoubleStack<Integer>();
       st.push(1, 10);
       st.push(1, 20);
       st.push(1, 30);
      
       st.push(2, 40);
       st.push(2, 50);
      
       System.out.println("Peek element in stack 1 "+ st.peek(1));
       System.out.println("Peek element in stack 2 "+ st.peek(2));
       System.out.println("Size of stack 1 : " + st.size(1));
       System.out.println("Size of stack 2 : " + st.size(2));
      
       st.pop(1);
       st.pop(2);
      
       System.out.println("Peek element in stack 1 "+ st.peek(1));
       System.out.println("Peek element in stack 2 "+ st.peek(2));
       System.out.println("Size of stack 1 : " + st.size(1));
       System.out.println("Size of stack 2 : " + st.size(2));
      
       st.pop(1);
       st.pop(2);
      
       System.out.println("Peek element in stack 1 "+ st.peek(1));
       System.out.println("Peek element in stack 2 "+ st.peek(2));
       System.out.println("Size of stack 1 : " + st.size(1));
       System.out.println("Size of stack 2 : " + st.size(2));
   }

}
//sample output


Related Solutions

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
Add comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static...
Add comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static void main(String[] args) {         PriorityQueue<Person> peopleQueue = new PriorityQueue<>();         Scanner s = new Scanner(System.in);         String firstNameIn;         String lastNameIn;         int ageIn = 0;         int count = 1;         boolean done = false;         System.out.println("Enter the first name, last name and age of 5 people.");         while(peopleQueue.size() < 5) {             System.out.println("Enter a person");             System.out.print("First Name: ");             firstNameIn...
SHOW SCREEN SHOTS OF THE INPUT AND OUTPUT AS WELL AS THE RESULTS PROGRAMMING IN PERL...
SHOW SCREEN SHOTS OF THE INPUT AND OUTPUT AS WELL AS THE RESULTS PROGRAMMING IN PERL Task 2: Variables 2.1 Scalar Variables A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing. #!/usr/bin/perl $age = 35; # An integer assignment $name = "Gary Mann"; # A string $salary = 5445.50; #...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT