Results 1 to 10 of 10
  1. #1
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Having problem working with two classes

    edit :
    i have two classes in two different files.

    i have this class:
    Java Code:
    public class Color
    {
       private int red;
       private int green;
       private int blue;
    
    
      public Color(){
        red = 0;
        green = 0;
        blue = 0;
      }
    and i have this class :
    Java Code:
    public class Light
    {
        private Color color1;   
        private boolean switchedon;
    
        public Light(int red, int green, int blue){
           //dont know what to write here . how can i use the members of the Color class here ? without using extends.
        }
      }
    Last edited by d2idan; 11-05-2014 at 12:31 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Having problem working with two classes

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Re: Having problem working with two classes

    Quote Originally Posted by Norm View Post
    i edited the question.
    i know how to create constructor . but this time i need to use the member of the other class in side the current class Light

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Having problem working with two classes

    need to use the member of the other class in side the current class Light
    Add a getter method to the class and have it return the requested value.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Re: Having problem working with two classes

    Quote Originally Posted by Norm View Post
    Add a getter method to the class and have it return the requested value.
    but i cant do this because the parameter red is within the braces.
    also for the excersice i cant use any more attributes. if it wasnt the case i could just create private members red ,green and blue. cant do this

    public class Light
    {
    private Color color1;
    private boolean switchedon;

    public Light(int red, int green, int blue){
    ...
    }

    public int getRed(){
    return red; //this gives me an error "cannot find symbol
    }
    }
    Last edited by d2idan; 11-05-2014 at 12:59 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Having problem working with two classes

    What is the constructor supposed to do with the 3 values it receives? Can it put them some place that the get method can access them?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Re: Having problem working with two classes

    Quote Originally Posted by Norm View Post
    What is the constructor supposed to do with the 3 values it receives? Can it put them some place that the get method can access them?
    the 3 parameters represent the RGB colors. and i need to initilize them to the RGB colors .

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Having problem working with two classes

    What will the code in the constructor do with the 3 values passed to it?
    Can the get method get those values after the constructor is done with them? See the Color class and its methods.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Having problem working with two classes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Re: Having problem working with two classes

    ok, sorry you can close this

Similar Threads

  1. Replies: 10
    Last Post: 11-23-2013, 06:47 PM
  2. Classes Problem
    By FreeQy in forum New To Java
    Replies: 5
    Last Post: 11-05-2011, 03:37 PM
  3. Replies: 59
    Last Post: 05-22-2011, 08:37 AM
  4. HELP working with 2 classes!!! URGENT!
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 02:58 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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