In: Computer Science
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.
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();
}
}
}