I need help with these four methods. I need to implement Stack.java CalendarDate
ID: 3793398 • Letter: I
Question
I need help with these four methods. I need to implement Stack.java CalendarDate.java and the test code.java. which I have pasted in this page. 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 appropriate.
These are the four methods. These are excersizes of chapter 14 in Building java programs 4th edition.
1. stutter, modify to return the stuttered Stack, and leave the original Stack unchanged.
2. equals, use the compareTo method for comparisons, leave the original Stacks unchanged.
3. isSorted, use compareTo to evaluate if sorted, and leave the original Stack unchanged.
4. removeMin, use compareTo for evaluating the minimum, if the original Stack is empty you can return null.
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);
}
}
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();
}
}
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
}
Explanation / Answer
public class CalendarDate implements Comparable, Comparator {
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);
}
}
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();
}
}
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
}
stutter:
public void stutterss(Stack<Integer> stu) {
Queue<Integer> que = new LinkedList<Integer>();
while(!stu.isEmpty())
que.add(stu.pop());
while(!que.isEmpty())
stu.push(q.remove());
while(!s.isEmpty())
que.add(stu.pop());
while(!que.isEmpty()) {
int n = que.remove();
stu.push(n);
stu.push(n);
}
}
issorted () can be implemented as:-
issorted(test());
equals:
stack1.equals(stack2) to compare
remove min:
find the smallest element in stack and remove it:
a[top--];