Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. stutter, modify to return the stuttered Stack, and leave the original Stack u

ID: 3793344 • Letter: 2

Question

2. stutter, modify to return the stuttered Stack, and leave the original Stack unchanged.

5. equals, use the compareTo method for comparisons, leave the original Stacks unchanged.

15. isSorted, use compareTo to evaluate if sorted, and leave the original Stack unchanged.

19. removeMin, use compareTo for evaluating the minimum, if the original Stack is empty you can return null.

here is the test code.

public static void main(String[] args) {

       CalendarDate[] store = {new CalendarDate(1,2,10),

       new CalendarDate(1,1,10), new CalendarDate(12,30,10)};

       Stack testAll = new Stack();

       for (CalendarDate i: store) testAll.push(i);

       System.out.println(Chapter14.stutter(testAll)); // 6 dates

       System.out.println(Chapter14.equals(testAll,testAll)); // true

       System.out.println(Chapter14.isSorted(testAll)); // false

       for (int i=1;i<=9;i++) testAll.push(new CalendarDate(1,1,10));

       Chapter14.removeMin(testAll);

       while (!testAll.empty())

           System.out.println(testAll.pop().longDate()); // only 2 remain  

   }

public class CalendarDate implements Comparable, Comparator {

   // FIELDS

private int month;

private int day;

private int year;

// Constructors

public CalendarDate() {

   // default 0,0 makes no sense, so using 1,1

   this(1,1,1970); // zero epoch UNIX

}

public CalendarDate(int month, int day, int year) {

   if (month<1 || month>12 || day<1 || day>31 || year<5 || year>9999)

       throw new IllegalArgumentException("Invalid month/day/year");

this.month = month;

this.day = day;

this.year = year;

}

// ACCESSORS (getters)

public int getMonth() {

return month;

}

public int getDay() {

return day;

}

public int getYear() {

   return year;

}

public String toString() {

return month + "/" + day + "/" + year;

}

public String longDate() {

   String[] names = {"January","February","March","April","May","June","July","August","September","October","November","December"};

   return names[month-1] + " " + day + ", " + year;

}

// Compares this calendar date to another date.

// Dates are compared by month and then by day.

public int compareTo(CalendarDate other) {

if (this.year != other.year) {

   return this.year - other.year;

} else if (this.month != other.month) {

return this.month - other.month;

} else {

return this.day - other.day;

}

}

// for Comparator

   public int compare(CalendarDate first, CalendarDate second) {

       // Should be the same as compareTo() result

       return first.compareTo(second);

   }

   public boolean equals(CalendarDate other) {

       return (this.compareTo(other)==0);

   }

}

use the Stack Class:  Stack.java. So DO NOT import java.util.Stack;  Other Classes in java.util are OK, like you need those for the Queue's. I will use in each case for testing purposes, so use compareTo as appropriat

public class Stack {

   // avoid blanked import of java.util

   private java.util.Stack secret;

   // default constructor

   public Stack() {

       secret = new java.util.Stack();

   }   

   // empty that collection

   public void clear() {

       secret.clear();

   }

   // should be order constant

   public int size() {

       return secret.size();

   }

   // simply have push call push from API

   public E push(E a) {

       secret.push(a);

       return a;

   }

   // And, empty calls empty from API

   public boolean empty() {

       return secret.empty();

   }

   // And my pop() uses pop() form JAVA API

   public E pop() {

       return secret.pop();

   }

   // My peek uses their peek

   public E peek() {

       return secret.peek();

   }

   // Following are not basic Stack operations

   // but needed to do some simple testing

   // toString is probably not O(constant)

   public String toString() {

       return secret.toString();

   }

}

2. Write a method called stutter that accepts a stack of integers as a parameter and replaces every value in the stack with two occurrences of that value. Preserve the original relative order. For example, if the stack stores [3, 7, 1, 14 9 your method should change it to store C3 3, 7, 7, 1, 1, 14, 14, 9, 9 Use a single queue as auxiliary storage 5. Write a method called equals that accepts two stacks of integers as parameters and returns true if the two stacks store exactly the same sequence of integer values in the same order. Your method must restore the two stacks to their original state before returning. U one stack as auxiliary storage 15. Write a method called issorted that accepts a stack of integers as a parameter and returns t if the elements in the stack occur in ascending (nondecreasing) order from top to bottom. That is, the smallest element should be on top, growing larger toward the bottom. For example, if the stack stores 20, 20, 17, 11, 8, 8, 3, 2 J, your method should return true. An empty or one-element stack is considered to be sorted. Your method must restore the parameter stack to its original state before returning. Use one queue or stack (but not both) as auxiliary storage. 19. Write a method called removeMin that accepts a stack of integers as a parameter and removes and returns the small- est value from the stack. For example, if the stack stores [2, 8, 3, 19, 2, 3, 2, 7, 12, -8, 41, your method should remove and return -8, leaving the stack storing [2, 8, 3, 19, 2, 3, 2, 7, 12, 41. If the min imum value appears more than once, all occurrences of it should be removed. For example, given the same stack, if we again call removeMin on it, the method would return 2 and leave the stack storing (8 3, 19, 3, 7, 12, 4 Use one queue as auxiliary storage.

Explanation / Answer

Question 5)

public boolean equals(Stack<Integer> s1, Stack<Integer> s2) {
/*
*will remove (by pop) an item from s1 and s2, if they are different returns false
*else it will allow while to continue until one stack is empty
*stacks are of same size thus it is enough to just check if s1 is empty
*if both stacks are iterated through without returning they must be equivalent so return true
*/

if (s1.empty() and s2.empty()) {
return true;
}
if (s1.size() != s2.size()) {
return false;
}
while (!s1.empty()) {
//no need to create int variables to store
if ( s1.pop() != s2.pop() ){
return false;
}
}
return true;
}
Question 2)

should give answer as "3 3 7 7 1 1"

Question 15)

Public static boolean issorted(stack<integer>s)

{

if(s.size()<2
{
return true;

}

Boolean sorted=true;

Int preg=s.pop();

Stack(integer>backup=new stack<integer>();

Backup.push(prev);

While(!s.isEmpty())

Int curr=s.pop();

Backup.push(curr);

If(prev>curr)

{

sorted= false;

}

prev=Curr;

}

while(!backup.isEmpty())

S.push(backup.pop());

}

return sorted;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote