Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 39 of 39

Thread: Calling constructor error

  1. #26
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    What is the function of the interface Comparable<GreetingCard>?

  2. #27
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    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.

  3. #28
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    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?

  4. #29
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    Quote Originally Posted by haliza hadi View Post
    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?
    Why on earth would you want to do that? What motivates this strange question?

  5. #30
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    Ok ignore my questions..its ask by my friends huhu... btw i'm using Collections.sort and my sort does not do the sorting...

  6. #31
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    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

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calling constructor error

    it won't work
    Please explain what "won't work" means.
    If there are error messages, copy the full text and paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    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
    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());
            }
        }
    }

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calling constructor error

    How does the compareTo() method implement the requirements stated in the API doc?
    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.
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    wait i forgot to attach the abstract parent class and the subsequent child class
    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
    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);
            }
        }
    }

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calling constructor error

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    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;
    }
    }
    }

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calling constructor error

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    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

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Beginner assignment - constructor issue / error
    By Jarruda in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2012, 01:29 AM
  2. [SOLVED] Error in calling JFrame's super.
    By CurseControl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 20th, 2012, 09:53 PM
  3. [SOLVED] Error in constructor
    By hello_world in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 14th, 2011, 07:46 PM
  4. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 12:53 PM
  5. calling a constructor
    By turnwellm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 08:46 PM