Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this assignment. It is a bit long but if you put it in the program it's not actually long. import java.util.Comparator;

Please help me with this assignment. It is a bit long but if you put it in the program it's not actually long.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

import java.util.Comparator;

// The CalendarDate class stores information about a single calendar date

// (upgraded from BJP text Chapter 10)

//

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 (month12 || day31 || year9999 || year

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;

}

// simple quick output

public String toString() {

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

}

// I thought a long date was dinner with a preson you don't like?

// But we'll also use the January 1, 1970 format instead

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 interface

public int compare(CalendarDate first, CalendarDate second) {

// Should be the same as compareTo() result

return first.compareTo(second);

}

@Override

public boolean equals(Object other) {

// Note: must override equals(Object)

if (other instanceof CalendarDate) {

CalendarDate test = (CalendarDate)other;

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

} else

return false;

}

@Override

public int hashCode() {

// days since 0/0/0 assuming 31 in each month

// number is strange, but works to achieve unique hash code

return (day+31*month+366*year);

}

}

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 class Post {

public static void main(String[] args) {

// store some dates so they can be reused

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); // build a Stack

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

Chapter14.removeMin(testAll);

while (!testAll.empty())

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

3rd Edition

0760049041, 978-0760049044

More Books

Students also viewed these Databases questions