Question

In: Computer Science

public class Book{     public String title;     public String author;     public int year;    ...

public class Book{

    public String title;
    public String author;
    public int year;
    public String publisher;
    public double cost;
      
    public Book(String title,String author,int year,String publisher,double cost){
       this.title=title;
        this.author=author;
        this.year=year;
        this.publisher=publisher;
        this.cost=cost;
    }

    public String getTitle(){
        return title;
    }
  
     public String getAuthor(){
        return author;
    }
    public int getYear(){
        return year;
    }
    public String getPublisher(){
        return publisher;
    }
    public double getCost(){
        return cost;
    }
    public String toString(){
        return "Book Details: " + title + ", " + author + ", " + year + ", " + publisher + ", " + cost;
    }
}

public interface MyQueue {
   public abstract boolean enQueue(int v);
   public abstract int deQueue();
   public abstract boolean isFull();
   public abstract boolean isEmpty();
   public abstract int size();
   public abstract int peek();
}

public interface MyStack {
   public abstract boolean isEmpty();
   public abstract boolean isFull();
   public abstract boolean push(T v);
   public abstract T pop();
   public abstract T peek();
  
}

public class MyQueueImpl implements MyQueue {
   private int capacity;
   private int front;
   private int rear;
   private int[] arr;
   public MyQueueImpl(int capacity){
       this.capacity = capacity;
       this.front = 0;
       this.rear = -1;
       this.arr = new int[this.capacity];
   }
   @Override
   public boolean enQueue(int v) {      
           if(this.rear == this.capacity - 1) {
               //Perform shift
               int tempSize = this.size();
               for(int i=0; i < tempSize; i++) {
                   arr[i] = arr[front];
                   front++;
               }
               front = 0;
               rear = tempSize - 1;
           }
           this.rear ++;
           arr[rear] = v;
           return true;
   }
   @Override
   public int deQueue() {
       return arr[front++];
      
   }
   public String toString() {
       String content = "Queue :: ";
      
       for(int i=front; i<= rear; i++) {
           content += "\n" + arr[i];
       }
       return content;
   }
   @Override
   public boolean isFull() {
       return (this.size() == this.capacity);
   }
   @Override
   public int size() {
       return rear - front + 1;
   }
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return (this.size() == 0);
   }
   @Override
   public int peek() {
       // TODO Auto-generated method stub
       return this.arr[this.front];
   }
}

import java.lang.reflect.Array;
public class MyStackImpl implements MyStack {
   // TODO write your code here
  
  
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean isFull() {
       // TODO Auto-generated method stub
       return false;
   }
  
   @Override
   public boolean push(T v) {
       // TODO write your code here
      
       return true;
   }

   @Override
   public T pop() {
       // TODO write your code here
      
       return null;
      
   }
  
   public String toString() {
       // TODO write your code here
      
      
      
       return "";
   }

   @Override
   public T peek() {
       // TODO Auto-generated method stub
       return null;
   }

}

make test classs   

write your code here
        Create a queue object.
        insert 5 Books in the Queue
        Create a stack object.
        Use the stack to reverse order of the elements in the queue.

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

MyStack.java

package c13;
public interface MyStack<T> {
public abstract boolean isEmpty();
public abstract boolean isFull();
public abstract T pop();
public abstract T peek();
public abstract boolean push(T v);
  
}

MyStackImpl.java

package c13;
public class MyStackImpl<T> implements MyStack {
   private int capacity;
   private int top;
   private T[] arr;
   public MyStackImpl(int capacity){
       this.capacity = capacity;
       this.top = -1;
       this.arr = (T[])new Object[this.capacity];
   }

   @Override
   public T pop() {
       if(top>-1)
           return arr[top--];
       else
           return null;

   }
   public String toString() {
       String content = "Stack :: ";
       for(int i=top; i> 0; i--) {
           content += "\n" + arr[i];
       }
       return content;
   }

   @Override
   public boolean isEmpty() {
       if(top==-1) {
           return true;
       }
       return false;
   }

   @Override
   public boolean isFull() {
       if(top==capacity) {
           return true;
       }
       return false;
   }
  
   @Override
   public boolean push(Object v) {
       if(top<capacity) {
           top++;
           arr[top] =(T) v;
           return true;
       }
       return false;
   }

   @Override
   public Object peek() {
       return arr[top];
   }
  
  
   public static void main(String[] args) {
       MyStackImpl<Book> books = new MyStackImpl<>(5);
       books.push(new Book("Title1", "author1", 2000, "srk", 1));
       books.push(new Book("Title2", "author2", 2001, "srk1", 2));
       books.push(new Book("Title3", "author3", 2002, "srk2", 3));
       books.push(new Book("Title4", "author4", 2003, "srk3", 4));
       books.push(new Book("Title5", "author5", 2004, "srk4", 5));
       while(!books.isEmpty()) {
           System.out.println(books.peek());
           books.pop();
       }
   }


}


Related Solutions

Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status : boolean + Book() Book(String, String, String, int , int)+ + setName(String) : void + setSerialNum(String) : void + setAuthor(String) : void + setEdition(int) : void + setYear(int) : void + getName():String + getAuthor():String + getYear(): int + getSerialNum(): String + getEdition():int + setAvailable() : void + setUnavailable(): void checkAvailability(): boolean ----------------------------------------------------------------------------------- Library Name : String Address: String Static NUMBEROFBOOKS: int BooksAtLibrray : ArrayList...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); };...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); }; And this code in main: vector<Issue> tickets; Issue issu; a) tickets.push_back(-99); State whether this code is valid, if not, state the reason b) Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like: (string problem) c) Write code that will output the problem attribute for every element in the...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
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 Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT