Question

In: Computer Science

Java Question: COSC 2436 Lab 4 (Submit your Word file with answers) If you push the...

Java Question:

COSC 2436 Lab 4 (Submit your Word file with answers)

  1. If you push the objects x, y, and z onto an initially empty stack, in what order will three pop operations remove them from the stack?
  1. What pseudocode statements create a stack of the three strings "Jill", "Jane", and "Joe", in that order with "Jill" at the top?
  1. Suppose that s and t are empty stacks and a, b, c, and d are objects. What do the stacks contain after the following sequence of operations executes?

s.push(a);

s.push(b);

s.push(c);

t.push(d);

t.push(s.pop());

t.push(s.peek());

s.push(t.pop());

t.pop();

  1. What are the contents of the stack pile after the following statements execute? Assume that MyStack is a class that implements the interface StackInterface.

StackInterface pile = new MyStack<>();

pile.push("Jane");

pile.push("Jess");

pile.push("Jill");

pile.push(pile.pop());

pile.push(pile.peek());

pile.push("Jim");

String name = pile.pop();

pile.push(pile.peek());

Solutions

Expert Solution


Before going into explanation and answers, please understand the following stack operations in Java:

A. push : Adds the contents to the top of stack.
B. pop: Removes and return the top most element from the stack.
C. peek : Fetches or return the top most element but not removes it from the stack.

1) If you push the objects x, y, and z onto an initially empty stack, in what order will three pop operations remove them from the stack?

Answer- The order of removal after the three pop operations will be z,y and x.

Explanations- The objects entering the stack will be in the order of x at first then y and at last z. so when the objects are removed the last (i.e) z will come out first, then y and at last x will be removed.

2) What pseudocode statements create a stack of the three strings "Jill", "Jane", and "Joe", in that order with "Jill" at the top?

Answer- pseudocode statements ->

Stack<String> s = new Stack<String>();

s.push("Jane"); or s.push("Joe");

s.push("Joe"); or s.push("Jane");

s.push("Jill")

That is to keep Jill on top we have to insert or push jill in the stack at last.

Explanations- In stack, the object that is pushed first into the stack is at the last in the stack and the object that is pushed last into the stack sits at the top of the stack (i.e) jill is pushed into the stack at last so that jill sits on the top of the stack.

3) Suppose that s and t are empty stacks and a, b, c, and d are objects. What do the stacks contain after the following sequence of operations executes?

s.push(a);

s.push(b);

s.push(c);

t.push(d);

t.push(s.pop());

t.push(s.peek());

s.push(t.pop());

t.pop();

Answer - The contents of stack s after given operations will be [a,b,b] .

                   The contents of stack t after given operations will be [d].

Explanations- In the stack s objects are pushed in order as follows at first a, b, c and in the stack t only one object d is pushed at first. After that we remove (c) from stack s and insert (c) in the stack t with the statement {t.push(s.pop())}.After that the top value (b) from stack s is taken and pushed into stack t with the statement{t.push(s.peek())}. After that, from stack t the top value (b) is removed and pushed into stack s with the statement{s.push(t.pop())} and at last from the stack t the top element is removed which is (c).So, at last stack s contents are [a,b,b] and stack t contents are [d].

4) What are the contents of the stack pile after the following statements execute? Assume that MyStack is a class that implements the interface StackInterface.

StackInterface pile = new MyStack<>();

pile.push("Jane");

pile.push("Jess");

pile.push("Jill");

pile.push(pile.pop());

pile.push(pile.peek());

pile.push("Jim");

String name = pile.pop();

pile.push(pile.peek());

Answers- The contents of stack pile after given operations will be [Jane, Jess, Jill, Jill, Jill].

Explanations- At first, [Jane,Jess,Jill] is pushed into the stack. After that jill is popped and pushed in the stack so we have [Jane,Jess,Jill] in the stack. After this top element Jill is taken and pushed back into the stack so now we have [Jane,Jess,Jill,Jill] in the stack. Now Jim is pushed into the stack so now we have [Jane,Jess,Jill,Jill,Jim] in the stack. Now again pop is called and Jim is removed from the stack so now we have [Jane,Jess,Jill,Jill] in the stack. At last again the top element Jill is taken and pushed back into the stack so now we have [Jane,Jess,Jill,Jill,Jill] in the stack. So the final contents of the pile stack after given operations will be [Jane, Jess, Jill, Jill, Jill].                                   


Related Solutions

a) Submit a copy of your dataset along with a file that contains your answers to...
a) Submit a copy of your dataset along with a file that contains your answers to all of the following questions. b) What the mean and Standard Deviation (SD) of the Close column in your data set? c) If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed at less than the mean for that year? Hint: You do not want to calculate the mean to...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
Review the following scenario, and answer the questions. Create a Word document for your answers, submit...
Review the following scenario, and answer the questions. Create a Word document for your answers, submit via submission link. A 28-year-old primigravida at 41 weeks’ gestation is admitted to the L&D unit for early labor at 2 cm, 70% effaced, and 0 station. How can the nurse best describe to this patient the latent phase of labor? How will the cardinal movements of labor facilitate the birth of the fetus?
. You must use Excel (submit either a pdf, word or Excel file only). . You...
. You must use Excel (submit either a pdf, word or Excel file only). . You must identify the 5 steps (you must address each in detail). Problem: Use the given data to complete a t-test using Excel. Question: Is there a difference in group means between the number of words spelled correctly for two groups of fourth graders? Group Assignment Score 1 3 1 4 1 10 2 14 2 7 2 8 2 10 2 15 2 9...
Instruction: Answer in a Word file and submit. Show the formula and all corresponding numbers you...
Instruction: Answer in a Word file and submit. Show the formula and all corresponding numbers you use in the formula. Do NOT use spreadsheet and submit. 1. (3 points)     You deposit your $10,000 in a new savings account that earn 4 % annually. You don’t take the interest out and keep it in the savings account so that you can earn interest on interest (compounding!). What would be the value of the $10,000 after 40 years? (Show your calculation) 2....
using C thank you Must submit as MS Word file with a screenshot of the 3...
using C thank you Must submit as MS Word file with a screenshot of the 3 outputs. Run your program 3 times. the output must be in a screenshot not typed for the each time you run the program. thank you Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will...
1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.o Write a method called larger that accepts two double parameters and...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
Please submit your Excel file and highlight your answers in color. Brief Case Has Gold Lost...
Please submit your Excel file and highlight your answers in color. Brief Case Has Gold Lost its Luster? In 2011, when the Gallup organization polled investors, 34% rated gold the best long-term investment. However, in April of 2013 Gallup surveyed a random sample of U.S. adults. Respondents were asked to select the best long-term investment from a list of possibilities. Only 241 of the 1005 respondents chose gold as the best long-term investment. A. With 95% confidence, compute the margin...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1: Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today. Option...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT