Question

In: Computer Science

Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add...

Question 1
Refer to the operations below:
Add (10 + 5)
Add (4+8)
Add (7*2)
Add (90 – 3)
Print list
Print peek
Remove an item from the list
Print list
1.1 Implement the operations above into a Queue structure called q1.
1.2 Implement the operations above into a Stack structure called s1.

Name your program Question1_1 for the queue structure and Question1_2 for the stack structure

JAVA Language to be used.

Please give step by step explanation on how to save and run the programs

Solutions

Expert Solution

In Java, we have available data structures for queue and stack.

1. Using Queue data structure.

- Create a file named Question1_1.java and add below code into it.

import java.util.*;
public class Question1_1 {
    public static void main(String[] args) {
        
        //We can't create instance of a Queue since it is an interface
        Queue<Integer> q1 = new LinkedList<Integer>();
        
        //Adding elements to the Queue
        //it will add 15
        q1.add(10+5);
        
        //it will add 12
        q1.add(4+8);
        
        //it will add 14
        q1.add(7*2);
        
        //it will add 87
        q1.add(90-3);
        
        //to print a list
        System.out.println("List :"+q1);
       
        //peek() method - it returns the head of queue and null if list is empty
        System.out.println("peek(): "+q1.peek());
        
        //remove() - it will remove first element from queue
        System.out.println("Removed element: "+q1.remove());
        
        //to print a list
        System.out.println("List :"+q1);
    }
}

Save your file and in cmd, go to the directory where you saved your program Question1_1.java

Type javac Question1_1.java and press enter to compile you code. If there is no errors in your code, It will take to next line

Now type java Question1_1.java to run your program and you can see the output on cmd window.

You can refer this screenshots for indentation and output of the code

Output :

2. Using a stack

Create a file named Question1_2.java and add below code into it.

import java.util.*;
public class Question1_2 {
    public static void main(String[] args) {
        
        //create instance of a Stack as s1
        Stack<Integer> s1 = new Stack<Integer>(); 
        
        //Adding elements to the Stack using push method
        //it will add 15
        s1.push(10+5);
        
        //it will add 12
        s1.push(4+8);
        
        //it will add 14
        s1.push(7*2);
        
        //it will add 87
        s1.push(90-3);
        
        //to print a list
        System.out.println("List :"+s1);
       
        //peek() method - it returns the top of stack and null if stack is empty
        System.out.println("peek(): "+s1.peek());
        
        //pop() - it will remove top element from stack
        System.out.println("Removed element: "+s1.pop());
        
        //to print a list
        System.out.println("List :"+s1);
    }
}

Save your file and in cmd, go to the directory where you saved your program Question1_2.java

Type javac Question1_2.java and press enter to compile you code. If there is no errors in your code, It will take to next line

Now type java Question1_2.java to run your program and you can see the output on cmd window.

You can refer this screenshots for indentation and output of the code

Output :

NOTE : make sure that you have set path variable to run java code in your system.


Related Solutions

Question 1 (20 marks) Refer to the operations below: Add (10 + 5) Add (4+8) Add...
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add (90 – 3) Print list Print peek Remove an item from the list Print list 1.1 Implement the operations above into a Queue structure called q1. (10) 1.2 Implement the operations above into a Stack structure called s1. (10 Guidelines: Please take note! (a) Name your program Question1_1 for the queue structure and Question1_2 for the stack structure (b) The Java Programmer is required...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
1-Consider the below data for ALL PARTS of this question: 4 2 5 7 8 14...
1-Consider the below data for ALL PARTS of this question: 4 2 5 7 8 14 1   5 23 17 What is the sample MEAN? ------------------------------------ Note: Enter X.X AT LEAST ONE DIGIT BEFORE THE DECIMAL, ONE AFTER and round up AFTER all calculations. Thus, 7 is entered as 7.0; 3.562 is entered as 3.6; 0.3750 is entered as 0.4; 17.351 is entered as 17.4 2-Consider the below data for ALL PARTS of this question: 4 2 5 7 8...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question 's scores, If three points were added to every score in this distribution, what would be the new mean? If three points were added to every score in this distribution, what would be the new standard deviation. Remember, you have already calculated population standard deviation in a previous problem. This problem requires two answers.
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1...
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1 9 2 13 3 14 4 13 5 18 What are the confidence limits (alpha = 0.05) for the true mean value of Y when X = 3?
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5]...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5] D = [1, -9, 5, -3] E = [3, 3, -1] Uz = 1/|z| ^z d(X,Y) = (Rθ) d = diameter R = Radius θ = Theta Find a. Uc b. d (D, C) c. Let P = B + 3E, UP = d. A x B e. 3B x E f. C x D
5.) For the data set 2 4 4 5 7 8 9 10 12 12 13...
5.) For the data set 2 4 4 5 7 8 9 10 12 12 13 13 16 16 16 16 17 19 19 20 23 24 24 24 25 26 26 27 28 28 29 31 32 34 34 36 37 38 42 44 45 46 47 47 48 50 52 53 53 54 55 56 56 57 58 (a) Find the 80th percentile. The 80t percentile is =    (a) Find the 42nd percentile. The 42nd percentile is...
PROBLEM 5. A box contains 10 tickets labeled 1, 2, 3, 4, 5, 6, 7, 8,...
PROBLEM 5. A box contains 10 tickets labeled 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Draw four tickets and find the probability that the largest number drawn is 8 if: (a) the draws are made with replacement. (b) the draws are made without replacement. PROBLEM 6. Suppose a bakery mixes up a batch of cookie dough for 1,000 cookies. If there are raisins in the dough, it's reasonable to assume raisins will independently have a .001 chance...
Year 1 2 3 4 5 6 7 8 9 10 A 1 1.01 1.0201 1.0303...
Year 1 2 3 4 5 6 7 8 9 10 A 1 1.01 1.0201 1.0303 1.0406 1.0510 1.0615 1.0721 1.0829 12.0305 B 1 .9900 .9801 .9703 .9606 .9510 .9415 .9321 .9227 10.0487 Assume a purchase price of $10 Million for both properties. (a) What is the expected total return (IRR) on a 10-year investment in each property? Use a financial calculator or equation solver for this. (b) If the 10% cap rate represents a fair market value for each...
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3....
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3. 9.   3.      4. 5.   6.      5. 7.   2.      6. 6.   4.   Test of hypothesis for difference of two means by using 1. t Test 2. ANOVA Take 5% type 1 error, then compare the results. Show that t² is equal to f.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT