g 2022CSCI 211Week 6Queues] - Main.java 3 6 7 8 9 Main.java Queues.java X Stacks.java X...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
g 2022\CSCI 211\Week 6\Queues] - Main.java 3 6 7 8 9 Main.java Queues.java X Stacks.java X public class Main { H public static void main(String[] args) { B 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 8 32 A 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 A B B A B A B A B A A } //array declaration String[] cities = {"Philadelphia, PA", "Harrisburg, PA", "Pittsburgh, PA", "Cleveland, OH", "Toledo, OH", "Gary, IN", "Chicago, IL"}; //create stack class object Stacks stack = new Stacks(); System.out.println("Destination from Philadelphia to Chicago"); for (String city cities) { System.out.println(city); } //call push for (String city cities) { stack.push(city); } //call pop for (int i = 0; i < cities.length; i++) { cities[i] = stack.pop(); } //print the reverse array System.out.println(); System.out.println("Destination from Chicago to Philadelphia"); for (String city cities) { System.out.println(city); } //queue object //adding element to the queue Queues queue = new Queues (cities.length); System.out.println(" "); // adding elements to the queue for (int i=0; i < cities.length; i++) { queue.enqueue (cities[i]); System.out.println(cities[i]+" being added, queue size = "+queue.size()); } // removing elements from the queue System.out.println(" "); for(int i=0; i < cities.length; i++) { } queue.dequeue (); System.out.println(cities[i]+" removed, queue size = "+queue.size()); Main CH Q x A2 A Event Log 30:1 CRLF UTF-8 4 spaces g 2022\CSCI 211\Week 6\Queues] - Stacks.java 1 2 3 4 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22 23 WWW.NNNNNNN 24 27 28 29 30 25 26 A 32 34 Main.java X Queues.java X Stacks.java /*Stacks create software for a stack as an object and create a simple stack class and then test it by using the stack to reverse the items in an array of Strings. Edited by V. Tuon Feb 28,2022 35 31 B 36 public class Stacks { # 37 B B 33 A A } public static final int MAX = 100; int top; String[] stringStack = new String [MAX]; //stack public Stacks() { this.top = -1; //set top to -1 every time new data adds } //push method to push element in stack public boolean push (String newEntry) { //check if stack is overflow if (top >= (MAX - 1)) { } } System.out.println("Stack Overflow!"); } else { return false; stringStack [++top] = newEntry; return true; //pop method to pop element in stack public String pop() { } //check if stack is empty if (top < 0) { return "Stack is Empty!"; } else { String entryPopped = stringStack [top--]; return entryPopped; Main C Q x A2 1 ^ V Event Log 6:14 CRLF UTF-8 4 spaces 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 60 61 Main.java X Queues.java Stacks.java X public class Queues 62 65 66 67 E A A A A A A # + + private String arr[]; // String array to store city names private int front; // points to front element of the queue private int rear; // points to the last element of the queue private int capacity; // maximum capacity private int count; // current size of the queue // Constructor to initialize the max size of the queue Queues (int size) { arr = new String[size]; } } // function to remove the first element from the queue public void dequeue () { capacity = size; front = 0; } rear = -1; count = 0; } // checking for queue underflow if (isEmpty()) { } // function to add the element to the queue public void enqueue (String item) { // checking for queue overflow if (isFull()) { System.out.println("UnderFlow Program Terminating..."); System.exit( status: 1); front = (front + 1) % capacity; count--; } System.out.println("OverFlow Program Terminating"); System.exit( status: 1); rear = (rear + 1) % capacity; arr[rear] = item; count++; // peek () will return front element of the queue public String peek() { if (isEmpty()) { System.out.println("UnderFlow Program Terminated"); System.exit( status: 1); } return arr[front]; // funtion to return the current size of the queue public int size() { return count; } // function to check if the queue is empty or not public Boolean isEmpty() { return (size() == 0); } // function to check if the queue is full or not public Boolean isFull() { return (size() == capacity); } A4 2 ^ V O Event Log 16:25 CRLF UTF-8 4 spaces g 2022\CSCI 211\Week 6\Queues] - Main.java 3 6 7 8 9 Main.java Queues.java X Stacks.java X public class Main { H public static void main(String[] args) { B 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 8 32 A 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 A B B A B A B A B A A } //array declaration String[] cities = {"Philadelphia, PA", "Harrisburg, PA", "Pittsburgh, PA", "Cleveland, OH", "Toledo, OH", "Gary, IN", "Chicago, IL"}; //create stack class object Stacks stack = new Stacks(); System.out.println("Destination from Philadelphia to Chicago"); for (String city cities) { System.out.println(city); } //call push for (String city cities) { stack.push(city); } //call pop for (int i = 0; i < cities.length; i++) { cities[i] = stack.pop(); } //print the reverse array System.out.println(); System.out.println("Destination from Chicago to Philadelphia"); for (String city cities) { System.out.println(city); } //queue object //adding element to the queue Queues queue = new Queues (cities.length); System.out.println(" "); // adding elements to the queue for (int i=0; i < cities.length; i++) { queue.enqueue (cities[i]); System.out.println(cities[i]+" being added, queue size = "+queue.size()); } // removing elements from the queue System.out.println(" "); for(int i=0; i < cities.length; i++) { } queue.dequeue (); System.out.println(cities[i]+" removed, queue size = "+queue.size()); Main CH Q x A2 A Event Log 30:1 CRLF UTF-8 4 spaces g 2022\CSCI 211\Week 6\Queues] - Stacks.java 1 2 3 4 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22 23 WWW.NNNNNNN 24 27 28 29 30 25 26 A 32 34 Main.java X Queues.java X Stacks.java /*Stacks create software for a stack as an object and create a simple stack class and then test it by using the stack to reverse the items in an array of Strings. Edited by V. Tuon Feb 28,2022 35 31 B 36 public class Stacks { # 37 B B 33 A A } public static final int MAX = 100; int top; String[] stringStack = new String [MAX]; //stack public Stacks() { this.top = -1; //set top to -1 every time new data adds } //push method to push element in stack public boolean push (String newEntry) { //check if stack is overflow if (top >= (MAX - 1)) { } } System.out.println("Stack Overflow!"); } else { return false; stringStack [++top] = newEntry; return true; //pop method to pop element in stack public String pop() { } //check if stack is empty if (top < 0) { return "Stack is Empty!"; } else { String entryPopped = stringStack [top--]; return entryPopped; Main C Q x A2 1 ^ V Event Log 6:14 CRLF UTF-8 4 spaces 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 60 61 Main.java X Queues.java Stacks.java X public class Queues 62 65 66 67 E A A A A A A # + + private String arr[]; // String array to store city names private int front; // points to front element of the queue private int rear; // points to the last element of the queue private int capacity; // maximum capacity private int count; // current size of the queue // Constructor to initialize the max size of the queue Queues (int size) { arr = new String[size]; } } // function to remove the first element from the queue public void dequeue () { capacity = size; front = 0; } rear = -1; count = 0; } // checking for queue underflow if (isEmpty()) { } // function to add the element to the queue public void enqueue (String item) { // checking for queue overflow if (isFull()) { System.out.println("UnderFlow Program Terminating..."); System.exit( status: 1); front = (front + 1) % capacity; count--; } System.out.println("OverFlow Program Terminating"); System.exit( status: 1); rear = (rear + 1) % capacity; arr[rear] = item; count++; // peek () will return front element of the queue public String peek() { if (isEmpty()) { System.out.println("UnderFlow Program Terminated"); System.exit( status: 1); } return arr[front]; // funtion to return the current size of the queue public int size() { return count; } // function to check if the queue is empty or not public Boolean isEmpty() { return (size() == 0); } // function to check if the queue is full or not public Boolean isFull() { return (size() == capacity); } A4 2 ^ V O Event Log 16:25 CRLF UTF-8 4 spaces
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
(a) Let Find vectors -12 -5] -- -- [18-[1] 1 = = S = -5 to U = A = U = in R such that S is the transition matrix from (v1, v2} to {u, u}- (b) Let P4 be the vectors space of all polynomials of degree...
-
A road building company is purchasing an excavator for $500,000. The corporate tax rate is 45% and the expected after-tax rate of return is 12%. What would be the present value of the future tax...
-
Chunky Cheese Pizza has $60 million in bonds payable. The bond indenture states that the debt to equity ratio cannot exceed 3.0. Chunky's total assets are $200 million, and its liabilities other than...
-
Thornton Industries began construction of a warehouse on July 1, 2016. The project was completed on March 31, 2017. No new loans were required to fund construction. Thornton does have the following...
-
What other companies might be affected by the change in the price of Peloton stock? was it positive or negative?
-
The accounts of Cascade Consulting, Inc., follow with their normal balances at December 31, 2010. The accounts are listed in no particular order. Requirements 1. Prepare the companys trial balance at...
-
A bicyclist traveling with speed v = 4.2 m/s on a flat road is making a turn with a radius r = 6.4 m. The forces acting on the cyclist and cycle are the normal force (Fn) and friction force (Ffr)...
-
Spend Analysis Objective: The objective of this assignment is to analyze personal spending habits and gain insights into individual financial management. Instructions: Data Collection: Compile a...
-
The following picture shows four routers, A,B,C,D. Packets travel from A and B to D. All routers including C operate in FIFO order. If two packets arrive at C at the same time, give priority to...
-
XXX is a TTT company that distributes electronic components in xxx imported from the YYY. Its main supplier, AMERICAN TECH has made a shipment of components amounting 1,200,000 USD to be paid within...
-
Financial Software Inc. (FSI) provides ERP software to large corporations. Big Corporation signs a contract with FSI on January 1, 2023. FSI will provide the ERP, including installation and testing,...
-
Perfect Pet Collar Company makes custom leather pet collars. The company expects each collar to require 2.35 feet of leather and predicts leather will cost $4.20 per foot. Suppose Perfect Pet made 85...
-
What is the present values of Team's Offer using a 1 4 percent discount rate? ( Round answer to 2 decimal places, e . g . 1 5 . 2 5 . Do not round factor values. ) Present value. Explain
-
6 Tiff Corporation has two production departments, Casting and Assembly. The company uses a job-order costing system and computes a predetermined overhead rate in each production department. The...
-
Present an example Project Charter of your own creation. Apply it to a potential real-world scenario. Define, Measure, Analyze, Improve, and Control (DMAIC) How would you recommend DMAIC be applied,...
-
*A wholesaler sells mandarin oranges in boxes of 60. The probability that there are X bad oranges in a box are given in the following table. Value of X 0 1 2 Probability 5k 4k 8k Calculate the value...
-
Suppose the market is semistrong form efficient. Can you expect to earn excess returns if you make trades based on? a. Your brokers information about record earnings for a stock? b. Rumors about a...
-
Write an application that reads a line of text, tokenizes the line using space characters as delimiters, and outputs only the words beginning with capital letters.
-
Assuming GClass is a generic class, what is wrong with the following statement: GClass obj = new GClass();
-
Show that braces do not have to occur in matching pairs inside a string literal.
-
STAR Co. provides paper to smaller companies with volumes that are not large enough to warrant dealing directly with the paper mill. STAR receives 100-feet-wide paper rolls from the mill and cuts the...
-
Brooks Development Corporation (BDC) faces the following capital budgeting decision. Six real estate projects are available for investment. The net present value and expenditures required for each...
-
Morgan Inc. is planning the purchase of one of the component parts it needs for its finished product. The anticipated demands for the component for the next 12 periods are shown in the following...
Study smarter with the SolutionInn App