Results 1 to 2 of 2
  1. #1
    Jinxes is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Object Interaction

    Greetings,

    I have classes called 'Date' and 'Information'. In date class i have defined how date should be stored as well as in Information. However, when i create an object using (BlueJ) Information, the stored date in the information class does not seem to transfer the details to the Date class, which it should be doing. To clarify what i mean, here are the codes;

    Information (code)

    public class Information
    {

    private int reference;
    private String name;
    private Date Date;

    public Information(int ref, String nam)
    {

    reference = ref;
    name = nam;

    Date = null; //date should be stored as null unless user sets the date manually.

    }

    public void setDateRequired(int day, int month, int year)
    {
    Date = new Date(day, month, year);
    }


    Date

    public class Date
    {
    /** Fields of a Date - just the day, month and year*/
    private int day;
    private int month;
    private int year;

    /**
    * Constructor for objects of class Date, sets a default date of 1/1/2000
    */
    public Date()
    {
    day = 1;
    month = 1;
    year = 2000;
    }

    /**
    * Constructor for objects of class Date
    * @param d - the day part of the date (1 - 31, depending on the month).
    * @param m - the month part of the date (1 - 12).
    * @param y - the year part of the date.
    */
    public Date(int d, int m, int y)
    {
    day = d;
    month = m;
    year = y;
    }



    Could anyone please tell me what I' am doing wrong? :( There are no interaction between the two classes nor objects.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class InformationRx
    {
        private int reference;
        private String name;
        private DateRx date;
    
        public InformationRx(int ref, String nam)
        {
            reference = ref;
            name = nam;
            date = null; //date should be stored as null.
        }
    
        public void setDateRequired(int day, int month, int year)
        {
            date = new DateRx(day, month, year);
        }
    
        public static void main(String[] args)
        {
            InformationRx irx = new InformationRx(25, "testingAgent");
            irx.setDateRequired(2, 11, 2007);
            System.out.println(irx.date);
        }
    }
    
    class DateRx
    {
        /** Fields of a Date - just the day, month and year*/
        private int day;
        private int month;
        private int year;
    
        /**
         * Constructor for objects of class Date, sets a default date of 1/1/2000
         */
        public DateRx()
        {
            this(1, 1, 2000);
        }
    
        /**
         * Constructor for objects of class Date
         * @param d - the day part of the date (1 - 31, depending on the month).
         * @param m - the month part of the date (1 - 12).
         * @param y - the year part of the date.
         */
         public DateRx(int d, int m, int y)
         {
             day = d;
             month = m;
             year = y;
        }
    
        public String toString()
        {
            return "DateRx[day:"   + day +
                         " month:" + month +
                         " year:"  + year + "]";
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Object
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 01-10-2008, 01:31 AM
  3. Object ID
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-26-2007, 12:08 PM
  4. Getting name of object class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:22 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •