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

A. Write a public static method max0ccurrences that accepts a list of CalendarDa

ID: 3915372 • Letter: A

Question

A. Write a public static method max0ccurrences that accepts a list of CalendarDatesas a parameter and returns the number of times the most frequent occurring date (the "mode") occurs in the list. Solve this problem using a Map as auxiliary storage. If the list is empty, return O (Hint: part A is basically what you did for the homework exercises, so done in just minutes?) (Hint2: part B is NOT in exercises, and l'm checking to see you practiced using iterators?) B. Write a public static method maxUsingIterator that accepts a list of CalendarDates and returns the maximum date as defined by compareTo. The empty list returns default zero date of Unix epoch zero 1/1/1970. You MUST use an iterator and.next to loop over the list (e.g. p.721 in text) Examples shown below Some starter code import java.util.* // starter code: public class Quiz11 public static void main(Stringl] args) ArrayList list1 = new ArrayList(); list1.add(new CalendarDate(5, 5, 2018)); list1.add(new CalendarDate(1, 2, 2018)); list1.add(new CalendarDate(5, 5, 2018)); list1.add(new CalendarDate(10, 7, 2011)) list1.add(new CalendarDate(5, 5, 2018)); System.out.println(maxOccurrences(list1); // 3 System.out.println(maxUsinglterator(list1); // 5/5/2018 // solutions go below here public static int maxOccurrences(List quiz) {// etc...

Explanation / Answer

Quiz11.java

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

public class Quiz11 {

public static void main(String[] args) {

ArrayList<CalendarDate> list1 = new ArrayList<CalendarDate>();

list1.add(new CalendarDate(5,5,2018));

list1.add(new CalendarDate(1,2,2018));

list1.add(new CalendarDate(5,5,2018));

list1.add(new CalendarDate(10, 7, 2011));

list1.add(new CalendarDate(5,5,2018));

System.out.println(maxOccurrences(list1));

System.out.println(maxUsingIterator(list1));

}

public static int maxOccurrences(List<CalendarDate> quiz) {

if(quiz == null || quiz.isEmpty()) {

return 0;

}

HashMap<CalendarDate,Integer> map = new HashMap<CalendarDate,Integer>();

for(CalendarDate date: quiz) {

if(map.get(date)!=null) {

map.put(date,map.get(date) + 1);

} else {

map.put(date, 1);

}

}

Set<CalendarDate> keySet = map.keySet();

Iterator<CalendarDate> itr = keySet.iterator();

int max = map.get(itr.next());

while(itr.hasNext()) {

CalendarDate current = itr.next();

if(max<map.get(current)) {

max = map.get(current);

}

}

return max;

}

public static CalendarDate maxUsingIterator(List<CalendarDate> quiz) {

Iterator<CalendarDate> itr = quiz.iterator();

CalendarDate max = itr.next();

while(itr.hasNext()) {

CalendarDate current = itr.next();

if(current.compareTo(max)==1) {

max = current;

}

}

return max;

}

}

CalendarDate.java

public class CalendarDate {

private int day, month, year;

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

this.day = day;

this.month = month;

this.year = year;

}

public int compareTo(CalendarDate date) {

if(year > date.getYear()) {

return 1;

} else if(year == date.getYear()) {

if(month > date.getMonth()) {

return 1;

} else if(month == date.getMonth()) {

if(day > date.getDay()) {

return 1;

}

else if(day == date.getDay()) {

return 0;

} else {

return -1;

}

}

else {

return -1;

}

} else {

return -1;

}

}

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + day;

result = prime * result + month;

result = prime * result + year;

return result;

}

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

CalendarDate other = (CalendarDate) obj;

if (day != other.day)

return false;

if (month != other.month)

return false;

if (year != other.year)

return false;

return true;

}

public String toString() {

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

}

public int getDay() {

return day;

}

public int getMonth() {

return month;

}

public int getYear() {

return year;

}

}

Output:

3
5/5/2018

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote