Results 1 to 6 of 6

Thread: Void problem

  1. #1
    Ricus is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Void problem

    I need this for school and the book is not helping me. So I got this code and whenever I want to compile it says: incompatible types - found void but expected TEST

    Java Code:
    class TEST
    {
    //Attribute
    int positionX;
    int positionY;
    String ampelphase;
    String farbe;
    //Methoden
    void positionSetzen(int xNeu, int yNeu)
    {
        positionX = xNeu;
        positionY = yNeu;
        lampeOben.positionSetzen(positionX, positionY);
        lampeMitte.positionSetzen(positionX, positionY+1);
        lampeUnten.positionSetzen(positionX, positionY+2);
    }
    void farbeSetzen(String farbeNeu)
    {
        farbeNeu = farbe;
        lampeOben.farbeSetzen(farbe);
        lampeMitte.farbeSetzen(farbe);
        lampeUnten.farbeSetzen(farbe);
    }
    void gruenSetzen()
    {
        ampelphase = "gruen";
        lampeOben = farbeSetzen("schwarz");
        lampeMitte = farbeSetzen("schwarz");
        lampeUnten = farbeSetzen("gruen");
    }
    void gelbSetzen()
    {
        ampelphase = "gelb";
        lampeOben = farbeSetzen("schwarz");
        lampeMitte = farbeSetzen("gelb");
        lampeUnten = farbeSetzen("schwarz");
    }
    void rotSetzen()
    {
        ampelphase = "rot";
        lampeOben = farbeSetzen("rot");
        lampeMitte = farbeSetzen("schwarz");
        lampeUnten = farbeSetzen("schwarz");
    }
    void rotgelbSetzen()
    {
        ampelphase = "rotgelb";
        lampeOben = farbeSetzen("rot");
        lampeMitte = farbeSetzen("gelb");
        lampeUnten = farbeSetzen("schwarz");
    }
    //Referenzattribute
    TEST lampeOben;
    TEST lampeMitte;
    TEST lampeUnten;
    //Konstruktor
    TEST()
    {
        positionX = 1;
        positionY = 1;
        ampelphase = "rot";
        
        lampeOben=new TEST();
        lampeMitte=new TEST();
        lampeUnten=new TEST();
        lampeOben=positionSetzen(1, 1);
        lampeMitte=positionSetzen(1, 2);
        lampeUnten=positionSetzen(1, 3);
        lampeOben=farbeSetzen("rot");
        lampeMitte=farbeSetzen("schwarz");
        lampeUnten=farbeSetzen("schwarz");
    }
    
    void Weiterschalten()
    {
        if (ampelphase == "rot")
        {
                rotgelbSetzen();
            }
            else
            {
                if(ampelphase == "rotgelb")
                {
                    gruenSetzen();
                }
                else
                {
                    if (ampelphase == "gruen")
                    {
                        gelbSetzen();
                    }
                    else
                    {
                        rotSetzen();
                    }
                }
            }
        }
            
    }
    From this part:
    Java Code:
    void gruenSetzen()
    {
        ampelphase = "gruen";
        lampeOben = farbeSetzen("schwarz");
        lampeMitte = farbeSetzen("schwarz");
        lampeUnten = farbeSetzen("gruen");
    It shows the mistake at the end of:
    Java Code:
    lampeOben = farbeSetzen("schwarz");

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Void problem

    the method farbeSetzen does not return a TEST object but void, so you cant assign void to lampeOben!
    Change the method or/and think about the whole class. Why has a TEST object three references to TEST objects?
    --> constructor:
    lampeOben = new TEST();
    lampeMitte = new TEST();
    lampeUnten = new TEST();

    this will end in a continuous loop (StackOverflow)!
    Last edited by eRaaaa; 04-24-2012 at 07:16 PM.

  3. #3
    Ricus is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Void problem

    Quote Originally Posted by eRaaaa View Post
    the method farbeSetzen does not return a TEST object but void, so you cant assign void to lampeOben!
    Change the method or/and think about the whole class. Why has a TEST object three references to TEST objects?
    --> constructor:
    lampeOben = new TEST();
    lampeMitte = new TEST();
    lampeUnten = new TEST();

    this will end in a continuous loop (StackOverflow)!
    Thanks for the reply, but I really am a newbie to java and I do not exactly know how to fix it. So I have to remove the "void", but what should I add instead?

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Void problem

    Quote Originally Posted by Ricus View Post
    but what should I add instead?
    TEST ! But the whole class makes no sense. You have to think about it and try to learn the basics. Begin with simpler classes.
    What's the aim of the class?

  5. #5
    Ricus is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Void problem

    Well it was supposing to be a traffic light. I did everything the book said so far, doesn't work out somehow...

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,813
    Rep Power
    19

    Default Re: Void problem

    Call your class TrafficLight then.
    TEST is not exactly a meaningful name if you know what the class is supposed to represent.

    A TrafficLight has three states, Red/Amber/Green. You seem to have modelled this as three separate TrafficLights.
    That's not really whata traffic light is is it? It is a single thing that displays one of three colours.

    So try and think along those lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. problem with void
    By exltus in forum New To Java
    Replies: 3
    Last Post: 12-17-2011, 02:38 PM
  2. Replies: 1
    Last Post: 12-04-2010, 05:41 PM
  3. Int equals void
    By Insomniac Riot in forum New To Java
    Replies: 14
    Last Post: 05-06-2010, 03:44 PM
  4. Replies: 5
    Last Post: 01-24-2008, 11:53 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
  •