Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default How do I call a class in another class

    Hi

    Need some help here.. I have a Staff class and MyDate class. How do I call or shall I say return the value for MyDate class in Staff class. Staff class code as follows :-

    import java.util.*;

    public class Staff extends Person
    {
    private int salary;
    private int dateHired;

    public Staff(String name, String nricNo, String phoneNo, int salary)
    {
    super(name, nricNo, phoneNo);
    this.salary = salary;
    }

    public void setSalary(int salary)
    {
    this.salary = salary;
    }

    public int getSalary()
    {
    return salary;
    }

    public String getDateHired()
    {
    return ?
    }

    public String toString()
    {
    super.toString();
    String s = "Salary : " + salary;
    return s;
    }


    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at the following code segment.

    Java Code:
    public class ClassOne {
    
        public static void main(String[] args) {
            // Create an object of ClassTwo
            ClassTwo objClassTwo = new ClassTwo();
            
            // Invoke a public method of ClassTwo
            objClassTwo.displayMessage();
        }
    
    }
    
    class ClassTwo {
    
        public void displayMessage() {
            System.out.println("message");
        }
    
    }
    Please check the spellings with the compiling the code.

  4. #4
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default Where am I to add in the constructor for MyDate() in Staff class

    But now i'm prompted cannot find symbol - constructor MyDate(). So where am i to add in the constructor for MyDate()?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Makes no sense. Please post your most recent code so we can see for ourselves. Also, please use code tags. Best of luck.

  6. #6
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    import java.util.*;

    public class Staff extends Person
    {
    private int salary;
    MyDate objMyDate = new MyDate();

    public Staff(String name, String nricNo, String phoneNo, int salary)
    {
    super(name, nricNo, phoneNo);
    this.salary = salary;
    }

    public void setSalary(int salary)
    {
    this.salary = salary;
    }

    public int getSalary()
    {
    return salary;
    }

    public String getDateHired()
    {
    return objMyDate.toString;
    }

    public String toString()
    {
    super.toString();
    String s = "Salary : " + salary;
    return s;
    }


    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, please edit this post to add code tags, so that your code retains its formatting and is readable. Please read the link in my signature that tells you how to do this.

    Also, please post the MyDate class code.

  8. #8
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    public class MyDate
    {
        private int day;
        private int month;
        private int year;
        
        public MyDate(int D, int M, int Y)
        {
             day = D;
             month = M;
             year = Y;
            
        }
        
        public void setDay(int D)
        {
            day = D;
        }
        
        public int getDay()
        {
            return day;
        }
        
        public void setMonth(int M)
        {
            month = M;
        }
        
        public int getMonth()
        {
            return month;
        }
        
        public void setYear(int Y)
        {
            year = Y;
        }
        
        public int getYear()
        {
            return year;
        }
        
        public String toString()
        {
            String s = "Date : "  + day + "/" + month + "/" + year;
            return s;
        }
     
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Sigh,... you still haven't edited your previous post to add code tags to it as has been asked several times. It's not difficult to do, and it would make it so much easier for others to help you. What is stopping you?

    On to at least one problem, you are trying to create a MyDate object like so:
    Java Code:
    MyDate objMyDate = new MyDate();
    but if you look at the MyDate constructor, it looks like so:
    Java Code:
        public MyDate(int D, int M, int Y)
        {
             day = D;
             month = M;
             year = Y;
        }
    It takes parameters, and you're not passing parameters when calling the constructor, and this is why the compiler is complaining -- it's telling you that it can't find a no-parameter constructor.

    My recommendation: either give MyDate a no-parameter constructor or (probably better) call the MyDate constructor with the appropriate parameters, ... oh, and please make me happy by adding code tags to your other posted code. Best of luck.

  10. #10
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Im sorry but i did add the code tags to MyDate class.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Roselicious View Post
    Im sorry but i did add the code tags to MyDate class.
    And I appreciate that, but you can edit your previous posts and add tags there too. This way all can read your posts and get in on this discussion. Thanks.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Roselicious View Post
    Im sorry but i did add the code tags to MyDate class.
    What Fubarable trying to point is that he ask several times to add code tags, but you waist more post to do that. Actually it's not a big deal.

    Anyway, did you solve your problem now?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  15. #15
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Elo guys

    I believe i did mention in my intro that i need some refresh. Due to my working hours, i got no time to read up that amount of pages of text box nor attend class.. Thus with watever knowledge i have from poly, im using it now and its rusty. Thats the reason i joined this forum . I will appreciate if u guys can guide mi through so i can learn at the same time. This will be helpful to mi in a way. Hope it makes sense to y i joined this forum.

  16. #16
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    seriously im asking for help here and u guys keep telling mi why is this like that and this. doesnt make sense at all. then wats the purpose of this forum then ????????? im rushing for time every day. im just looking for help for the limited time i have.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's why we are helping here too, personally I like to share my knowledge with others, specially with newbies in Java. So people had to have some ethics which are looking here in our forum. And some personal related stuff like cross-posting and stuff. Please pay your attention on that.

    Anyway, Fubarable give you a comment about the constructor mismatch in your code. Did you fix it?

  18. #18
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    yah so im telling u guys i donno where i made the mistake and im not sure where to pass in where and how. i mean simply u guys can just teach mi like this is wrong u shouldnt be doing this , u should do this . in this way i can keep that in mind and i learn sth every day from u guys. i am thankful that u guys r helping mi but im expecting help in a way that it benefits mi faster for a rush environment here.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at the post number 9, last post send by Fubarable, again. There is the solution. Didn't you figure is out yet?

  20. #20
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  2. need help to call another class
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-08-2010, 10:40 AM
  3. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM
  4. How do I call a class in jsp page
    By Greg in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-20-2009, 10:27 PM
  5. Can I call directly the *.class
    By doanhanam in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 07:24 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
  •