Use the Assignment7.java (code at the end of the question) and write your code where designated. There will be three me
Posted: Fri May 20, 2022 4:27 pm
Use the Assignment7.java (code at the end of the
question) and write your code where designated. There
will be three methods.
Problem #1
Write a method called "seeingThree" that takes
a stack of integers as a parameter and replaces every value in the
stack with three occurrences of that value. For example, suppose
the stack stores these values:
bottom [3, 7, 1, 14, 9] top
Then the stack should store these values after the method
terminates:
bottom [3, 3, 3,7,7, 7, 1,1, 1, 14,14, 14,9, 9, 9] top
Notice that you must preserve the original order. In the
original list the 9 was at the top and would have been popped
first. In the new stack the two 9s would be the first values popped
from the stack. You may use a single queue as auxiliary storage to
solve this problem.
Problem #2
Write a method called "twoStacksAreEqual" that
takes as parameters two stacks of integers and
returns true if the two stacks are equal
and that returns false otherwise. To be
considered equal, the two stacks would have to store the same
sequence of integer values in the same order. Your method is to
examine the two stacks but must return them to their original state
before terminating. You may use one stack as auxiliary storage.
Problem #3
Write a method called "isMirrored" that takes a
queue of integers as a parameter and
returns true if the numbers in the queue
represent a palindrome
(and false otherwise). A sequence of
numbers is considered a palindrome if it is the same in reverse
order. For example, suppose a queue
called q stores these values:
Then the call
of isMirrored(q); should
return true because this sequence is the
same in reverse order. If the queue had instead stored these
values:
The call on isMirrored would instead
return false because this sequence is
not the same in reverse order (the 9 and 4 in the middle don't
match). The empty queue should be considered a palindrome. You may
not make any assumptions about how many elements are in the queue
and your method must restore the queue so that it stores the same
sequence of values after the call as it did before. You may use one
stack as auxiliary storage.
______________________________________________________________________________________
Assignment7.java code:
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// Name:
// Class:
// Date:
// Instructor:
// Description:
public class Assignment7 {
// the entry point of the class calling the test methods which in
turn
calling the assigned methods
public static void main(String[] args) {
//testSeeingThreeMethod();
//testTwoStacksAreEqualMethod();
//testIsMirrored();
}
// pre : a stack of integers
// post: every value in the stack is replaced with three
occurrences of that
value
public static void seeingThree(Stack<Integer> s) {
/*********Write Code Here************/
}
// pre : two stacks of integers
// post: a boolean result of true if the stacks are equal or false
if the
stacks are not equal
public static boolean twoStacksAreEqual(Stack<Integer> s1,
Stack<Integer> s2)
{
/*********Write Code Here************/
}
// pre : a queue of integers
// post: returns true if the numbers in the queue represent a
palindrome (and
false otherwise).
// A sequence of numbers is considered a palindrome if it is the
same
in reverse order
public static boolean isMirrored(Queue<Integer> q) {
/*********Write Code Here************/
}
// This is a test method testing twoStacksAreEqual method. It test
both the
true case and the false case
private static void testIsMirrored() {
Queue<Integer> myQueueP = new
LinkedList<Integer>();;
for (int i = 0; i < 5; i++)
{
System.out.print(i);
myQueueP.add(i);
}
for (int i = 3; i >= 0 ; i--)
{
System.out.print(i);
myQueueP.add(i);
}
System.out.println();
System.out.println(isMirrored(myQueueP) + " isMirrord");
}
//test method to test the testTwoStacksAreEqualMethod.
//It tests cases of the same stack and not the same stack.
private static void testTwoStacksAreEqualMethod() {
Stack<Integer> myStack1 = new Stack<Integer>();
Stack<Integer> myStack2 = new Stack<Integer>();
Stack<Integer> myStack3 = new Stack<Integer>();
Stack<Integer> myStack4 = new Stack<Integer>();
for (int i = 0; i < 5; i++)
{
myStack1.push(i);
myStack2.push(i);
myStack4.push(i);
}
for (int i = 0; i < 6; i++)
{
myStack3.push(i);
}
System.out.println(twoStacksAreEqual(myStack1,myStack2) + " Same
Stack
");
System.out.println(twoStacksAreEqual(myStack3, myStack4) + " Not
Same
Stack");
}
//Method to test the SeeingThree method
private static void testSeeingThreeMethod() {
Stack<Integer> myStack = new Stack<Integer>();
for (int i = 0; i < 5; i++)
{
myStack.push(i);
}
System.out.println();
print(myStack);
seeingThree(myStack);
print(myStack);
}
// pre : a stack of integers
// post: prints out the stack of integers
private static void print(Stack<Integer> s) {
Enumeration<Integer> e = s.elements();
while ( e.hasMoreElements() )
System.out.print( e.nextElement() + " " );
System.out.println();
}
} //end of Assignment7
question) and write your code where designated. There
will be three methods.
Problem #1
Write a method called "seeingThree" that takes
a stack of integers as a parameter and replaces every value in the
stack with three occurrences of that value. For example, suppose
the stack stores these values:
bottom [3, 7, 1, 14, 9] top
Then the stack should store these values after the method
terminates:
bottom [3, 3, 3,7,7, 7, 1,1, 1, 14,14, 14,9, 9, 9] top
Notice that you must preserve the original order. In the
original list the 9 was at the top and would have been popped
first. In the new stack the two 9s would be the first values popped
from the stack. You may use a single queue as auxiliary storage to
solve this problem.
Problem #2
Write a method called "twoStacksAreEqual" that
takes as parameters two stacks of integers and
returns true if the two stacks are equal
and that returns false otherwise. To be
considered equal, the two stacks would have to store the same
sequence of integer values in the same order. Your method is to
examine the two stacks but must return them to their original state
before terminating. You may use one stack as auxiliary storage.
Problem #3
Write a method called "isMirrored" that takes a
queue of integers as a parameter and
returns true if the numbers in the queue
represent a palindrome
(and false otherwise). A sequence of
numbers is considered a palindrome if it is the same in reverse
order. For example, suppose a queue
called q stores these values:
Then the call
of isMirrored(q); should
return true because this sequence is the
same in reverse order. If the queue had instead stored these
values:
The call on isMirrored would instead
return false because this sequence is
not the same in reverse order (the 9 and 4 in the middle don't
match). The empty queue should be considered a palindrome. You may
not make any assumptions about how many elements are in the queue
and your method must restore the queue so that it stores the same
sequence of values after the call as it did before. You may use one
stack as auxiliary storage.
______________________________________________________________________________________
Assignment7.java code:
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// Name:
// Class:
// Date:
// Instructor:
// Description:
public class Assignment7 {
// the entry point of the class calling the test methods which in
turn
calling the assigned methods
public static void main(String[] args) {
//testSeeingThreeMethod();
//testTwoStacksAreEqualMethod();
//testIsMirrored();
}
// pre : a stack of integers
// post: every value in the stack is replaced with three
occurrences of that
value
public static void seeingThree(Stack<Integer> s) {
/*********Write Code Here************/
}
// pre : two stacks of integers
// post: a boolean result of true if the stacks are equal or false
if the
stacks are not equal
public static boolean twoStacksAreEqual(Stack<Integer> s1,
Stack<Integer> s2)
{
/*********Write Code Here************/
}
// pre : a queue of integers
// post: returns true if the numbers in the queue represent a
palindrome (and
false otherwise).
// A sequence of numbers is considered a palindrome if it is the
same
in reverse order
public static boolean isMirrored(Queue<Integer> q) {
/*********Write Code Here************/
}
// This is a test method testing twoStacksAreEqual method. It test
both the
true case and the false case
private static void testIsMirrored() {
Queue<Integer> myQueueP = new
LinkedList<Integer>();;
for (int i = 0; i < 5; i++)
{
System.out.print(i);
myQueueP.add(i);
}
for (int i = 3; i >= 0 ; i--)
{
System.out.print(i);
myQueueP.add(i);
}
System.out.println();
System.out.println(isMirrored(myQueueP) + " isMirrord");
}
//test method to test the testTwoStacksAreEqualMethod.
//It tests cases of the same stack and not the same stack.
private static void testTwoStacksAreEqualMethod() {
Stack<Integer> myStack1 = new Stack<Integer>();
Stack<Integer> myStack2 = new Stack<Integer>();
Stack<Integer> myStack3 = new Stack<Integer>();
Stack<Integer> myStack4 = new Stack<Integer>();
for (int i = 0; i < 5; i++)
{
myStack1.push(i);
myStack2.push(i);
myStack4.push(i);
}
for (int i = 0; i < 6; i++)
{
myStack3.push(i);
}
System.out.println(twoStacksAreEqual(myStack1,myStack2) + " Same
Stack
");
System.out.println(twoStacksAreEqual(myStack3, myStack4) + " Not
Same
Stack");
}
//Method to test the SeeingThree method
private static void testSeeingThreeMethod() {
Stack<Integer> myStack = new Stack<Integer>();
for (int i = 0; i < 5; i++)
{
myStack.push(i);
}
System.out.println();
print(myStack);
seeingThree(myStack);
print(myStack);
}
// pre : a stack of integers
// post: prints out the stack of integers
private static void print(Stack<Integer> s) {
Enumeration<Integer> e = s.elements();
while ( e.hasMoreElements() )
System.out.print( e.nextElement() + " " );
System.out.println();
}
} //end of Assignment7