Part 1: Stacks We will implement a program that will use STACK to do the add,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Part 1: Stacks We will implement a program that will use STACK to do the add, and multiplication. (Do not use the build in function or classes of Java or from the textbook). Create a stack class that takes integer (INTGER Class) Create a main that ask user to input commands If the user put integer value, push it to the stack (you need to check this) If they put any of the following symbols: + or * o Apply that operation on the 2 last items pushed to the stack (Hint make use pop) o If the stack doesn't have 2 items just ignore the operation o Store the result and push it to the stack If the user entered the ? symbol, print the stack- using toString() to print all the stacks items without popping them If the user put P, pop the stack and print the value • If the user put the Z symbol, end the program If they put any other symbol/string - ignore it Write a Test program to: o Add 10 o Print stack Add 12 Print stack (now with 12, 10) o Add 14 Sum last 2 item Pop the stack (should be 24) Use * to multiply top 2 item Then type? O o Then stop the program with Z o Take screen shot of your program running O Part2: LinkedList insert We will implement a program that will use Priority Queues! We will create a list of random integers, create 3 methods of sorting them then sort them. • Use the following code: import java.util.Random; // this creates random for our program public class App { //sample code for methods, create your own public static int mySort(int[] arr) { // YOUR CODE GOES HERE } return 0; public static void main(String[] args) throws Exception { int numitem = 100; // This decide how big you array is int[] myArr = new int[numitem]; Random myRand = new Random(); // creating Random object // Range for random to select from int min = 5; int max = 1000; } int indexMin = 0; for (int i= 0; i < myArr.length; i++) { myArr[i] = myRand.nextInt(max - min + 1) + min; // storing random integers in an array } // Now sort it double startTime = System.nanoTime(); // YOURSORTINGMETHOD(); double endTime= System.nanoTime(); double duration = (endTime - startTime); //divide by 1000000 to get milliseconds. System.out.println("the time it took to sort is " + duration); We provided the code for you to start Create the following Classes, remember you can do them in different classes (You may use LinkedList or array, your choice) (20%) o Heap o Priority Q with sorted Q o Priority Q with not sorted Q Now create sorting algorithm (30%) with their respective classes o Heap sort Insertion sort o Selection sort Answer: o o o O Do the Big-O analysis of each algorithm? Can you comment which one is faster? Do you think if we use array or LinkedList, we could speed the program up? Does the array initial order have any effect on the speed? Submit it in PDF Part 1: Stacks We will implement a program that will use STACK to do the add, and multiplication. (Do not use the build in function or classes of Java or from the textbook). Create a stack class that takes integer (INTGER Class) Create a main that ask user to input commands If the user put integer value, push it to the stack (you need to check this) If they put any of the following symbols: + or * o Apply that operation on the 2 last items pushed to the stack (Hint make use pop) o If the stack doesn't have 2 items just ignore the operation o Store the result and push it to the stack If the user entered the ? symbol, print the stack- using toString() to print all the stacks items without popping them If the user put P, pop the stack and print the value • If the user put the Z symbol, end the program If they put any other symbol/string - ignore it Write a Test program to: o Add 10 o Print stack Add 12 Print stack (now with 12, 10) o Add 14 Sum last 2 item Pop the stack (should be 24) Use * to multiply top 2 item Then type? O o Then stop the program with Z o Take screen shot of your program running O Part2: LinkedList insert We will implement a program that will use Priority Queues! We will create a list of random integers, create 3 methods of sorting them then sort them. • Use the following code: import java.util.Random; // this creates random for our program public class App { //sample code for methods, create your own public static int mySort(int[] arr) { // YOUR CODE GOES HERE } return 0; public static void main(String[] args) throws Exception { int numitem = 100; // This decide how big you array is int[] myArr = new int[numitem]; Random myRand = new Random(); // creating Random object // Range for random to select from int min = 5; int max = 1000; } int indexMin = 0; for (int i= 0; i < myArr.length; i++) { myArr[i] = myRand.nextInt(max - min + 1) + min; // storing random integers in an array } // Now sort it double startTime = System.nanoTime(); // YOURSORTINGMETHOD(); double endTime= System.nanoTime(); double duration = (endTime - startTime); //divide by 1000000 to get milliseconds. System.out.println("the time it took to sort is " + duration); We provided the code for you to start Create the following Classes, remember you can do them in different classes (You may use LinkedList or array, your choice) (20%) o Heap o Priority Q with sorted Q o Priority Q with not sorted Q Now create sorting algorithm (30%) with their respective classes o Heap sort Insertion sort o Selection sort Answer: o o o O Do the Big-O analysis of each algorithm? Can you comment which one is faster? Do you think if we use array or LinkedList, we could speed the program up? Does the array initial order have any effect on the speed? Submit it in PDF
Expert Answer:
Answer rating: 100% (QA)
import javautilScanner public class Stack private Node top private int size private static class Node private int data private Node next private Nodeint data thisdata data thisnext null public Stack t... View the full 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
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Tory Company sells a single product. Troy estimates demand and costs at various activity levels as follows: How much profit will Troy have if a price of $45 is charged? Units Sold Price Total...
-
What happens to capital losses that are not deductible in the current year?
-
The nth term of a geometric progression is (1/3) n . Find the 1st term and the common ratio, r.
-
In 2014, Air Asia Flight No. 8501 crashed in the Java Sea while flying from Indonesia to Singapore. The crash resulted in the deaths of all passengers and crew on board. The plane involved in the...
-
The Wickliffe City Council authorizes the restoration of the city library. The project is to be funded by the issuance of bonds, a reimbursement grant from the state, and property taxes. 1. Prepare...
-
Find a HAMILTONIAN PATH of the graph below (Give a sequence of letters to describe the path (e.g. A, D, E, B, A etc.) B E F G Paragraph B I UA Lato (Recom... v 19px... v L > Ea 5 0
-
Given two arrays each of length n, arr1, and arr2, in one operation, any two elements of an array can be swapped. This can occur any number of times. Find the maximum possible sum of i*(arr2[1]-...
-
The following are part of the account balances for Syarikat Permata as at 31 October 2017, which is at the end of its financial year. These accounts all have normal balances. ii. III. iv. V. Sales...
-
Write a letter to request your company for a new space for the office gym. How will you apply the three-step writing process (prewriting, writing ,revising) to this situationwrite a letter to request...
-
Credit control is very important, especially in small businesses. Explain what credit control is, and provide two examples of what a good credit control policy will look like.
-
8. A ball is thrown vertically upward from the edge of a bridge 22.5 m high with an initial speed of 10.0 m/s. The ball falls all the way down and strikes the water below. Determine the magnitude of...
-
Put this in a checkbook balance? Bank Reconciliation Information: Balance per bank statement as of May 31 was $41,615.95. Balance per Hughes Company's Cash account at May 31 was $41,378.50. A late...
-
You read about stakeholders in your Learning Activity and Reading area of this unit. How might external stakeholders' points of view be different from internal stakeholders? What changes would you...
-
Provide two synthetic routs for the following transformation.
-
Use Stokes' Theorem to evaluate f(y+sin x) dx+(z+cos y) dy+rdz, where C is the rve r(t) = (sint, cost, sin 2t), t = [0, 2].
-
Write a program that inserts 25 random integers from 0 to 100 in order into a linked-list object. For this exercise, youll need to modify the List class (Fig. 21.3) to maintain an ordered list. Name...
-
The grid of #s and dots (.) in Fig. 18.23 is a two-dimensional array representation of a maze. The #s represent the walls of the maze, and the dots represent locations in the possible paths through...
-
Exercises 5.35 5.37 developed a computer-assisted instruction program to help teach an elementary school student multiplication. Modify the program to allow the user to enter a difficulty level. At a...
-
The transfer function of a dynamic system is given by \[G(s)=\frac{s+1}{4 s^{4}+5 s^{3}+2 s^{2}+s+6} \] a. Using Routh's stability criterion, determine the stability of the system. b. Using MATLAB,...
-
Figure 10.40 shows a negative feedback control system. a. Design a P controller such that the damping ratio of the closed-loop system is 0.5 . b. Estimate the rise time, overshoot, and \(2 \%\)...
-
Consider the feedback system shown in Figure 10.26. a. Using Routh's stability criterion, determine the range of the control gain \(K\) for which the closed-loop system is stable. b. Use MATLAB...
Study smarter with the SolutionInn App