Results 1 to 6 of 6
  1. #1
    JVassie is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default Semantic Error: Message not understood by class

    Hi everyone, I am new to these forums, however have done a search, and looked at the results on others with this problem, however none seem to solve it, so here goes.

    I am trying to write a method that asks the user for a number of cycles to repeat for, and also a size for each cycle. The size is used in the call for another method. Ill put the method that I have a problem with here, and if the entire code below.

    Java Code:
     public void runLight(int numCycles,int sizePerCycle)
       {  
          for (int doneCycles = 0; doneCycles <= numCycles; doneCycles++)
          {
             this.lightCycle(sizePerCycle);
          }
       }
    The error that I get when I try to execute the method above is as follows:

    Java Code:
    Semantic error: Message runLight() not understood by class'DiscoLight'
    And the full code listign for both classes is below:

    Java Code:
    import ou.*;
    /**
     * The class Circle defines a shape with the characteristics of a circle.
     *
     * @author M255 Course Team
     * @version 1.0
     */
     
    public class Circle extends OUAnimatedObject
    {
    /* Instance variables */
    
       private int diameter;
       private OUColour colour;
       private int xPos;
       private int yPos;
       
       /**
        * Constructor for objects of class Circle with the default characteristics.
        */
       public Circle()
       {
          this.diameter = 20;
          this.colour = OUColour.BLUE;
          this.xPos = 30;
          this.yPos = 30;
       }
       
       /* Instance methods */    
       
       /**
        * Sets the diameter of the receiver to the value of the argument aDiameter
        */
       public void setDiameter(int aDiameter)
       {
          this.diameter = aDiameter;
          this.update();
       }
       
       /**
        * Returns the diameter of the receiver.
        */
       public int getDiameter()
       {
          return this.diameter;
       }
       
       /**
        * Sets the colour of the receiver to the value of the argument aColour.
        */
       public void setColour (OUColour aColour)
       {
          this.colour = aColour;
          this.update();
       }
       
       /**
        * Returns the colour of the receiver.
        */
       public OUColour getColour ()
       {
          return this.colour;
       }
       
       /**
        * Sets the horizontal position of the receiver to the value of the argument x.
        */
       public void setXPos(int x)
       {
          this.xPos = x;
          this.update();
       }
       
       /**
        * Returns the horizontal position of the receiver.
        */
       public int getXPos()
       {
          return this.xPos;
       }
       
       /**
        * Sets the vertical position of the receiver to the value of the argument y.
        */
       public void setYPos(int y)
       {
          this.yPos = y;
          this.update();
       }
       
       /**
        * Returns the vertical position of the receiver.
        */
       public int getYPos()
       {
          return this.yPos;
       }
       
       /**
        * Returns a string representation of the receiver.
        */
       public String toString()
       {
          return "An instance of class "+ this.getClass().getName() 
                 + ": position (" + this.getXPos() + ", " + this.getYPos() 
                 + "), diameter " + this.getDiameter() + ", colour " + this.getColour();
       }
       
       
        }
    The above is the class provided to me by the teacher, and hasn't been edited. Just copied for reference. Below is the class I am working on, all methods I have written aside from runLight() have been tested thoroughly and work as expected.

    Java Code:
    import ou.*;
    import java.util.*;
    /**
     * Class DiscoLight
     * This class uses the Circle class, and the Shapes window 
     * to simulate a disco light, that grows and shrinks and 
     * changes colour. 
     * 
     * @author M255 CT
     * @version 1.0
     */
    
    public class DiscoLight
    {
       /* instance variables */
       private Circle light;   // simulates a circular disco light in the Shapes window
       private Random randomNumberGenerator;
       
       /**
        * Default constructor for objects of class DiscoLight
        */
       public DiscoLight()
       {
          super();
          this.randomNumberGenerator = new Random();           
       }
    
    
       /**
        * Returns a randomly generated int between 0 (inclusive) 
        * and number (exclusive). For example if number is 6,
        * the method will return one of 0, 1, 2, 3, 4, or 5.
        */
       public int getRandomInt(int number)
       {
          return this.randomNumberGenerator.nextInt(number);
       }
    
       public void setLight(Circle aCircle)
       {
          this.light = aCircle;
       }
       
       public Circle getLight()
       {
          return this.light;
       }
        
       /** 
        * Sets the argument to have a diameter of 50, an xPos 
        * of 122, a yPos of 162 and the colour GREEN.
        * The method then sets the receiver's instance variable
        * light, to the argument aCircle.
        */ 
       public void addLight(Circle aCircle)
       {
          //Student to write code here, Q4(ii)
          this.light = aCircle;
          this.light.setDiameter(50);
          this.light.setColour(OUColour.GREEN);
          this.light.setXPos(122);
          this.light.setYPos(162);
          this.setLight(aCircle);
       }
    
    
       /** 
        * Randomly sets the colour of the instance variable 
        * light to red, green, or purple.
        */  
       public void changeColour()
       {
           int randomInt = getRandomInt(3);
           if (randomInt == 0)
           {
              this.light.setColour(OUColour.RED);
           }
           else if (randomInt == 1)
           {
              this.light.setColour(OUColour.GREEN);
           }
           else
           {
              this.light.setColour(OUColour.PURPLE);
           }
       }
    
          
       /** 
        * Grows the diameter of the circle referenced by the 
        * receiver's instance variable light, to the argument size.  
        * The diameter is incremented in steps of 2,
        * the xPos and yPos are decremented in steps of 1 until the
        * diameter reaches the value given by size. 
        * Between each step there is a random colour change.  The message 
        * delay(anInt) is used to slow down the graphical interface, as required.
        */   
       public void grow(int size)
       {   
           int curDiameter = this.light.getDiameter();
           while(curDiameter < size)
           {
              this.light.setDiameter(curDiameter + 2);
              this.light.setXPos(this.light.getXPos() - 1);
              this.light.setYPos(this.light.getYPos() - 1);
              this.changeColour();
              curDiameter += 2;
              this.delay(100);
           }
       }
     
       
       /** 
        * Shrinks the diameter of the circle referenced by the 
        * receiver's instance variable light, to the argument size.  
        * The diameter is decremented in steps of 2,
        * the xPos and yPos are incremented in steps of 1 until the
        * diameter reaches the value given by size. 
        * Between each step there is a random colour change.  The message 
        * delay(anInt) is used to slow down the graphical interface, as required.
        */     
       public void shrink(int size)
       {   
           int curDiameter = this.light.getDiameter();
           while(curDiameter > size)
           {
              this.light.setDiameter(curDiameter - 2);
              this.light.setXPos(this.light.getXPos() + 1);
              this.light.setYPos(this.light.getYPos() + 1);
              this.changeColour();
              curDiameter -=2;
              this.delay(100);
           }
       }
    
      
       /** 
        * Expands the diameter of the light by the amount given by
        * sizeIncrease (changing colour as it grows).
        * 
        * The method then contracts the light until it reaches its
        * original size (changing colour as it shrinks).
        */     
       public void lightCycle(int sizeIncrease)
       {
          int oldDiameter = this.light.getDiameter();
          this.grow(sizeIncrease);
          this.shrink(oldDiameter);
       }
       
       
       /** 
        * Prompts the user for number of growing and shrinking
        * cycles. Then prompts the user for the number of units
        * by which to increase the diameter of light.
        * Method then performs the requested growing and 
        * shrinking cycles.
        */     
       public void runLight(int numCycles,int sizePerCycle)
       {  
          for (int doneCycles = 0; doneCycles <= numCycles; doneCycles++)
          {
             this.lightCycle(sizePerCycle);
          }
       }  
    
       
       /**
        * Causes execution to pause by time number of milliseconds
        */
       private void delay(int time)
       {
          try
          {
             Thread.sleep(time); 
          }
          catch (Exception e)
          {
             System.out.println(e);
          } 
       }
      
    }
    As said above, I have searched for a while on the thread title error, and haven't come up with an answer to whats causing the problem, if anyone could shed some light on whats causing the error in my code, that would be wonderful.

    Many Thanks
    James

  2. #2
    JVassie is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Upon doing some more investigation, I have found that if I use the following code, it executes just fine, but it offers no flexibility and doesn't answer the question (to allow user to choose number of cycles and size of cycle)

    Java Code:
    public void runLight()
       {  
          int sizeCycle = 100;
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
       }
    Code above works, does four cycles each of size 100, what it needs to do though is prompt the user for these values.

    On the other hand, if I slightly modify the code, like below, I get the same error as my first post.

    Java Code:
    public void runLight(int sizeCycle)
       {  
          //int sizeCycle = 100;
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
          this.lightCycle(sizeCycle);
       }
    Hope this helps understanding my problem :)

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Where is the runLight() method being called?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Like StormyWaters, I cannot see where your runLight() method is being called, but from what you're describing, it seems like it's being called without parameters, and so the compiler complains when the only version it can find is one with parameters.

    -Gary-

  5. #5
    JVassie is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    I'm calling it in a Workspace, but yes, without parameters.

    I tested calling it with parameters, eg runLight(5,120) and it worked fine. Only problem is that the solution called for me to test it by using just runLight(), it should prompt the user for the parameters, how might I go about doing that?

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    That all depends on how you want to prompt the user. Are you using a GUI or are you doing it all on the command line?

    If using a GUI, I would suggest taking a look at the JOptionPane class (http://java.sun.com/j2se/1.4.2/docs/...ptionPane.html) as it already has some handy predefined methods for doing this.

    If going by the command line, take a look at the Scanner class(http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html).

Similar Threads

  1. Help with servlet error message
    By ricky_40 in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 07:01 AM
  2. Replies: 1
    Last Post: 02-15-2010, 11:32 PM
  3. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:46 PM
  4. Replies: 8
    Last Post: 07-22-2009, 02:16 AM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 03:10 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
  •