Answer to Question #276597 in Java | JSP | JSF for Alma

Question #276597

a)

Define ADT class MyQUEUE and MyStack implemented using Linked List data structure.

b)

Using class Book defined earlier, complete the AppQueueBook class :

In the main program:

a.

Declare a queue of books object named BookQ

b.

ask user to enter 10 Book objects into queue BookQ

c.

Display all books details in BookQ

d.

Display all books which were published before year 2000.

e.

Search and display a book with the lowest price.

f.

Declare another queue of books object named BookQOld, all books published before year

2000 should be copied into this queue

c)

Using class MyStack defined earlier, complete the AppStack class :

In the main program:

a. Evaluate the postfix expressions entered by user and display the result.


1
Expert's answer
2021-12-08T17:18:50-0500
import java.util.Iterator;
import java.util.LinkedList;

public class MyQueue<T> implements Iterable<T>{
    private LinkedList<T> queue;

    public MyQueue() {
        queue = new LinkedList<>();
    }

    public void enqueue(T element) {
        queue.add(element);
    }

    public T dequeue() {
        return queue.pop();
    }

    @Override
    public Iterator<T> iterator() {
        return queue.iterator();
    }
}


import java.util.Iterator;
import java.util.LinkedList;

public class MyStack<T> implements Iterable<T> {
    private LinkedList<T> stack;

    public MyStack() {
        stack = new LinkedList<>();
    }

    public void push(T element) {
        stack.push(element);
    }

    public T pop() {
        return stack.pop();
    }

    @Override
    public Iterator<T> iterator() {
        return stack.iterator();
    }
}


public class Book {
    private String name;
    private int published;
    private double price;

    public Book(String name, int published, double price) {
        this.name = name;
        this.published = published;
        this.price = price;
    }

    public int getPublished() {
        return published;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return name + " " + published + " year $" + price;
    }
}


import java.util.Scanner;

public class AppQueueBook {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        MyQueue<Book> bookQ = new MyQueue<>();
        for (int i = 0; i < 10; i++) {
            System.out.println((i + 1) + " Enter a name:");
            String name = in.nextLine();
            System.out.println("Published year:");
            int year = Integer.parseInt(in.nextLine());
            System.out.println("Price:");
            double price = Double.parseDouble(in.nextLine());
            bookQ.enqueue(new Book(name, year, price));
        }
        for (Book book : bookQ) {
            System.out.println(book);
        }

        System.out.println("\nBooks published before year 2000:");
        for (Book book : bookQ) {
            if (book.getPublished() < 2000) {
                System.out.println(book);
            }
        }
        System.out.println("\nThe cheapest book:");
        Book cheapest = null;
        double price = Double.MAX_VALUE;
        for (Book book : bookQ) {
            if (book.getPrice() <= price) {
                cheapest = book;
                price = book.getPrice();
            }
        }
        System.out.println(cheapest);
        MyQueue<Book> bookQOld = new MyQueue<>();
        for (Book book : bookQ) {
            if (book.getPublished() < 2000) {
                bookQOld.enqueue(book);
            }
        }
    }
}



import java.util.Scanner;

public class AppStack {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        MyStack<Integer> stack = new MyStack<>();
        System.out.println("Enter an expression:");
        String[] data = in.nextLine().split(" ");
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals("+") || data[i].equals("-") || data[i].equals("*") || data[i].equals("/")) {
                switch (data[i]) {
                    case "+":
                        stack.push(stack.pop() + stack.pop());
                        break;
                    case "-":
                        stack.push(stack.pop() - stack.pop());
                        break;
                    case "*":
                        stack.push(stack.pop() * stack.pop());
                        break;
                    case "/":
                        stack.push(stack.pop() / stack.pop());
                        break;
                }
            } else {
                stack.push(Integer.parseInt(data[i]));
            }
        }
        System.out.println(stack.pop());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS