What is the function of the interface Comparable<GreetingCard>?
Printable View
What is the function of the interface Comparable<GreetingCard>?
Read up on generics in the Java tutorial to see how and why they are used. Here it will allow you to create a compareTo method that uses a GreetingCard parameter as you've written it, but mainly they allow the compiler to check for bad code that would otherwise have only been discovered possibly on program run.
Will there be lots of changes if i declare my parent class GreetingCard to private? And what does private class do to the other class in the same package?
Ok ignore my questions..its ask by my friends huhu... btw i'm using Collections.sort and my sort does not do the sorting...
And in addition to the sorting problem...how do i use attributes from another package???I have tried importing the package but it won't work...pls help :(
Please explain what "won't work" means.Quote:
it won't work
If there are error messages, copy the full text and paste here.
There are no error messages...however when i implement the Collections.sort(cards) and then do a println, the output are not sorted by date...below is my code
Code java:import GreetingCard.*; import java.text.ParseException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; public class CardPrinter { public static void main(String [] args)throws ParseException{ DateFormat dfm = new SimpleDateFormat("dd-MM-yyyy"); ArrayList<GreetingCard> cards = new ArrayList<GreetingCard>(); cards.add(new BirthdayCard("Mike, from John",dfm.parse("26-02-1975"),"Kilanas")); cards.add(new BirthdayCard("Fedd, from Tom",dfm.parse("01-11-1978"),"Kuala Belait")); cards.add(new BirthdayCard("Oscar, from Nigel",dfm.parse("06-12-1995"),"Muara")); cards.add(new Anniversary("Joy and Charlie, from Sue","Wedding",dfm.parse("08-02-2000"),"Tutong")); cards.add(new Anniversary("John and Mary, from Henry","Wedding",dfm.parse("20-06-1989"),"Temburong")); Collections.sort(cards); for(int i = 0; i<cards.size(); i++){ System.out.println(cards.get(i).address+"\t"+cards.get(i).arrivalDate+"\t"+cards.get(i).formatMessage()); } } }
How does the compareTo() method implement the requirements stated in the API doc?
The posted code does not test for greater than or less than conditions. Without following the above requirements the sort() method can not do its job.Quote:
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
wait i forgot to attach the abstract parent class and the subsequent child class
Code java:import java.util.Date; public abstract class GreetingCard implements Comparable<GreetingCard> { public String message; public Date arrivalDate; public String address; public GreetingCard(String message, Date arrivalDate, String add){ this.message = message; this.arrivalDate = arrivalDate; this.address = add; } public int compareTo(GreetingCard other){ if(arrivalDate.equals(other.arrivalDate)){ return 0; } else{ if(arrivalDate.equals(other.arrivalDate)){ return 1; } else{ return -1; } } } public abstract String formatMessage(); } import java.util.Date; public class BirthdayCard extends GreetingCard{ public BirthdayCard(String bm, Date dd, String add){ super(bm,dd,add); this.message = bm; this.arrivalDate = dd; this.address = add; } public Date arrivalDate(){ return arrivalDate; } public String formatMessage(){ return ("Happy Birthday " + this.message); } } import java.util.Date; public class Anniversary extends GreetingCard { String occassion; public Anniversary(String am,String oc,Date ad, String add){ super(am,ad,add); this.message = am; this.occassion = oc; this.arrivalDate = ad; this.address = add; } public String formatMessage(){ return("Happy "+this.occassion+" Anniversary "+this.message); } } package cardprinter; import GreetingCard.*; import java.text.ParseException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; public class CardPrinter { public static void main(String [] args)throws ParseException{ DateFormat dfm = new SimpleDateFormat("dd-MM-yyyy"); ArrayList<GreetingCard> cards = new ArrayList<GreetingCard>(); cards.add(new BirthdayCard("Mike, from John",dfm.parse("26-02-1975"),"Kilanas")); cards.add(new BirthdayCard("Fedd, from Tom",dfm.parse("01-11-1978"),"Kuala Belait")); cards.add(new BirthdayCard("Oscar, from Nigel",dfm.parse("06-12-1995"),"Muara")); cards.add(new Anniversary("Joy and Charlie, from Sue","Wedding",dfm.parse("08-02-2000"),"Tutong")); cards.add(new Anniversary("John and Mary, from Henry","Wedding",dfm.parse("20-06-1989"),"Temburong")); Collections.sort(cards); for(int i = 0; i<cards.size(); i++){ System.out.println(cards.get(i).arrivalDate+"\t"+cards.get(i).formatMessage()+"\t"+cards.get(i).address); } } }
--- Update ---
wait i forgot to attach the abstract parent class and the subsequent child class
Code java:import java.util.Date; public abstract class GreetingCard implements Comparable<GreetingCard> { public String message; public Date arrivalDate; public String address; public GreetingCard(String message, Date arrivalDate, String add){ this.message = message; this.arrivalDate = arrivalDate; this.address = add; } public int compareTo(GreetingCard other){ if(arrivalDate.equals(other.arrivalDate)){ return 0; } else{ if(arrivalDate.equals(other.arrivalDate)){ return 1; } else{ return -1; } } } public abstract String formatMessage(); } import java.util.Date; public class BirthdayCard extends GreetingCard{ public BirthdayCard(String bm, Date dd, String add){ super(bm,dd,add); this.message = bm; this.arrivalDate = dd; this.address = add; } public Date arrivalDate(){ return arrivalDate; } public String formatMessage(){ return ("Happy Birthday " + this.message); } } import java.util.Date; public class Anniversary extends GreetingCard { String occassion; public Anniversary(String am,String oc,Date ad, String add){ super(am,ad,add); this.message = am; this.occassion = oc; this.arrivalDate = ad; this.address = add; } public String formatMessage(){ return("Happy "+this.occassion+" Anniversary "+this.message); } } package cardprinter; import GreetingCard.*; import java.text.ParseException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; public class CardPrinter { public static void main(String [] args)throws ParseException{ DateFormat dfm = new SimpleDateFormat("dd-MM-yyyy"); ArrayList<GreetingCard> cards = new ArrayList<GreetingCard>(); cards.add(new BirthdayCard("Mike, from John",dfm.parse("26-02-1975"),"Kilanas")); cards.add(new BirthdayCard("Fedd, from Tom",dfm.parse("01-11-1978"),"Kuala Belait")); cards.add(new BirthdayCard("Oscar, from Nigel",dfm.parse("06-12-1995"),"Muara")); cards.add(new Anniversary("Joy and Charlie, from Sue","Wedding",dfm.parse("08-02-2000"),"Tutong")); cards.add(new Anniversary("John and Mary, from Henry","Wedding",dfm.parse("20-06-1989"),"Temburong")); Collections.sort(cards); for(int i = 0; i<cards.size(); i++){ System.out.println(cards.get(i).arrivalDate+"\t"+cards.get(i).formatMessage()+"\t"+cards.get(i).address); } } }
How does the posted code follow the requirements for sorting that are in post#34.
For example if you wanted to sort these two numbers: 44 and 33
You need more information than they are not equal. You need to know which is larger or smaller.
The posted code only tests for equality.
What if i change my compareTo as below...pls help
public int compareTo(GreetingCard other){
if(arrivalDate.before(other.arrivalDate)){
return -1;
}
else{
if(arrivalDate.after(other.arrivalDate)){
return 0;
}
else{
return 1;
}
}
}
Check that the returned values follows the requirements, especially for when to return 0 and > 0
what happens when you compile and execute the code?
Ok after changing in GreetingCard, i made these changes in ECardPrinter as follows:
for(GreetingCard gc : cards){
System.out.println(gc.formatMessage()+"Date :"+gc.arrivalDate);
}
and it is sorted...
replacing the for interation