A. Write a method minToFront that takes an ArrayList as a parameter and that mov
ID: 3662819 • Letter: A
Question
A. Write a method minToFront that takes an ArrayList as a parameter and that moves the minimum value in the list to the front, otherwise preserving the order of the elements. (page 697 in text has a similar example using int).
B. Write a method removeDuplicates that takes as a parameter anArrayListand eliminates all later duplicates from the list. Do NOT assume the list is sorted, and do NOT sort the list. (page 697 in text has a similar example using String, but lacks examination of an empty collection, and you should also think about a case with hundreds of duplicates).
C. Write a method called removeInRange that accepts three parameters: an ArrayList a minimum object, and a maximum object. The method's behavior is to remove all objects in the list between the minumum (inclusive) and the maximum (inclusive). A similar example is shown on page 698 of text using Strings.
D. Write a method swapPairs that switches the order of values in an ArrayList in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the list initially stores these value{p1,p2,p3,p4,p5} your method should switch the first pair, p1, p2, etc...., to yield this list: {p2,p1,p4,p3,p5}.
CalendarDate.java
public class CalendarDate implements Comparable {
private int month;
private int day;
public CalendarDate(int month, int day) {
this.month = month;
this.day = day;
}
// Compares this calendar date to another date.
// Dates are compared by month and then by day.
public int compareTo(CalendarDate other) {
if (this.month != other.month) {
return this.month - other.month;
} else {
return this.day - other.day;
}
}
public int getMonth() {
return this.month;
}
public int getDay() {
return this.day;
}
public String toString() {
return this.month + "/" + this.day;
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
class main{
public static void minToFront(ArrayList<CalendarDate> ar){
CalendarDate mn = ar.get(0);
int index = 0;
for (int i = 1; i < ar.size(); i++){
if (ar.get(i).CompareTo(mn) < 0){
mn = ar.get(i);
index = i;
}
}
ar.remove(index);
ar.add(0,mn);
}
public static void removeDuplicates(ArrayList<CalendarDate> ar){
HashMap<CalendarDate,Integer> hm = new HashMap<CalendarDate,Integer>();
int i = 0;
for (i < ar.size()){
if (hm.get(ar.get(i)) == true)
ar.remove(i);
else{
hm.put(ar.get(i),1);
i += 1;
}
}
}
public static void removeInRange(ArrayList<CalendarDate> ar,CalendarDate mn,CalendarDate mx){
int i = 0;
while (i < ar.size()){
if (ar.get(i).CompareTo(mn) >= 0 && ar.get(i).CompareTo(mx) <= 0)
ar.remove(i);
else
i += 1;
}
}
public static void SwapPairs(ArrayList<CalendarDate> ar){
int i = 0;
int n = ar.size();
while (i < n){
if (i + 1 < n){
CalendarDate temp = ar.get(i);
ar.set(i, ar.get(i+1));
ar.set(i+1, temp);
}
i += 2;
}
}
public static void main(String[] args){
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.