This site requires JavaScript, please enable it in your browser!
Greenfoot back
Lboogie0208
Lboogie0208 wrote ...

2014/7/4

Please Help. Keep getting errors

Lboogie0208 Lboogie0208

2014/7/4

#
I am trying to do this and I have been struggling with programming.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HomeworkWorld here.
 * 
 * @author () 
 * @version (June 3, 2014)
 */
public class HomeworkWorld extends World
{
    /**
     * INSERT CODE BEOLW
     * Add two instance variables that are of type String[]
     *        one will be an array of images
     *        one will be an array of sounds
     *        DO NOT INCLUDE THE FILE SUFFIX - add that through code later
     *        
     *        Note - each image is 55x55
     */
    private String[] image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};;
    private String[] soundfile = {"hello", "happy", "crying", "ohno", "raspberry"};

    /**
     * Constructor for objects of class HomeworkWorld.
     * 
     */
    public HomeworkWorld()
    {   
        
        // Create a new world with 400x100 cells with a cell size of 1x1 pixels.
        super(400, 100, 1); 
    }
        /**
         * INSERT CODE BELOW
         * Write a "for" loop
         *      It should loop 5 times  (you can either hard code 5 or use code)
         *      It should use the method addObject( Actor object, int x, int y) to add new Emoticons to your world.
         *      Note:  concatinate the ".png" for images  and ".wav" for the sounds
         *      
         *      Determine the x value using a mathematical calculation
         *      The y value can be either hard coded or calculated
         *      
         */
        private void makeImages()
        {
            //make the Emoticon Images
            for(int i = 0; i<5; i++)
            {
                images[i]=new String("smiley1", "smiley2", "smiley3", "smiley4", "smiley5");
                sounds[i]=new String("hello","happy","crying","ohno","raspberry");
                addObject(new Emoticon(images[i], sound[i]), 70*(i+1), 50);
            }
        }
    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Emoticon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Emoticon extends Actor
{
    /**
     *INSERT CODE BELOW
     *    Create two instance varaibles of type String
     *         one named sound
     *         one named image
     */
    private String sound;
    private String image;
   
    
    {
    
    /**
     * INSERT CODE BELOW
     *   Write a construtor method
     *         The method should have two parameters
     *                type:  String     name:   newImage
     *                type:  String     name:   newSound
     *             
     *         The method should
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter
     *               
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image.
     *               
     */
    Emoticon emoticon = new Emoticon(image[i],sound[i]);
    image = newImage;
    sound = newSound;
    setImage(image);
}

    /**
     * Act - do whatever the Emoticon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // When the mouse is click on this object, play the sound.
         if(Greenfoot.mouseClicked(this))
         {
             /**
              * INSERT CODE BELOW
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound
              *     
              */
             Greenfoot.playSound(sound + ".wav");
             
         }
            
    }    
}
danpost danpost

2014/7/4

#
In your HomeworkWorld class, you want to make the Emoticon objects from the world constructor; so, remove lines 32, 44 and 45. Lines 49 and 50 in the 'for' loop do not make any sense. First, your array names are not 'images' and 'sounds'; they are declared on lines 20 and 21 as 'image' and 'soundFile'. Next, you do not need to declare the arrays of Strings a second time -- you can remove lines 49 and 50. Again, on line 51, your arrays are incorrectly addressed. Use the names you gave them on lines 20 and 21. In your Emoticon class, you are missing your constructor declaration line -- the one that should read:
public Emoticon(/* parameters */)
Line 37 in the class is a big mistake. I suggest you stay away from using 'new Anything()' within the code of the Anything class. This is certainly not what you want to be doing here; and until you understand better how things work (mainly, as far as order of execution of code), you should avoid it. Remove line 37 -- that is where your constructor declaration statement line should go.
danpost danpost

2014/7/4

#
Take the following demo class
import greenfoot.*;

public class Demo extends Actor
{
    public Demo()
    {
        Demo demo = new Demo();
    }
}
If we tried to instantiate a new Demo object (let us say from our sub-class of World, using 'new Demo()'), execution is transferred to line 5 above. When line 7 is executed, line 5 is again where execution is transferred to. This will continue forever (until heap space or stack overflow is encountered; or until the project is Terminated by way of the debug window).
Lboogie0208 Lboogie0208

2014/7/4

#
This is what i have now for the HomeworkWold
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HomeworkWorld here.
 * 
 * @author (Lorren Williams) 
 * @version (June 3, 2014)
 */
public class HomeworkWorld extends World
{
    /**
     * INSERT CODE BEOLW
     * Add two instance variables that are of type String[]
     *        one will be an array of images
     *        one will be an array of sounds
     *        DO NOT INCLUDE THE FILE SUFFIX - add that through code later
     *        
     *        Note - each image is 55x55
     */
    private String[] image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};;
    private String[] soundFile = {"hello", "happy", "crying", "ohno", "raspberry"};

    /**
     * Constructor for objects of class HomeworkWorld.
     * 
     */
    public HomeworkWorld()
    {   
        // Create a new world with 400x100 cells with a cell size of 1x1 pixels.
        super(400, 100, 1); 
    }
        /**
         * INSERT CODE BELOW
         * Write a "for" loop
         *      It should loop 5 times  (you can either hard code 5 or use code)
         *      It should use the method addObject( Actor object, int x, int y) to add new Emoticons to your world.
         *      Note:  concatinate the ".png" for images  and ".wav" for the sounds
         *      
         *      Determine the x value using a mathematical calculation
         *      The y value can be either hard coded or calculated
         *      
         */
        private void makeImages()
        {
            //make the Emoticon Images
            for(int i = 0; i<6; i++)
            {
                Emoticon emoticon = new Emoticon(image[], soundFile[i]);
                addObject(emoticon,10*(i+60),50);
               
            }
        }
    }
Lboogie0208 Lboogie0208

2014/7/4

#
I've been fooling with this forever now and I am super frustrated. I appreciate all the help you are giving me.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Emoticon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Emoticon extends Actor
{
   
    /**
     *INSERT CODE BELOW
     *    Create two instance varaibles of type String
     *         one named sound
     *         one named image
     */
    private GreenfootSound sound;
    
    private String[] soundFile;
    
    private void emoticon(String image, String soundFile)
    {
        setImage( image + ".png"); 
        sound = new GreenfootSound(soundFile + ".wav");
    }
    
    
    /**
     * Act - do whatever the Emoticon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the enviroment.
     */
    public void act()
    {
        // When the mouse is clicked on this object, play the sound.
        if(Greenfoot.mouseClicked(this))
        {
            sound.play();
        }
        
    }
}
danpost danpost

2014/7/4

#
For one thing, you did not complete all the changes I suggested above in your HomeworkWorld class. For another, you completely altered your Emoticon class, making it much worse off than it was. Go back to what you had above and make the changes I suggested. Then re-post the classes and copy/paste any remaining errors you may be getting.
Lboogie0208 Lboogie0208

2014/7/4

#
Ok here is what I have now I keep getting illegal start of expression
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class Emoticon here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class Emoticon extends Actor  
{  
    /** 
     *INSERT CODE BELOW 
     *    Create two instance varaibles of type String 
     *         one named sound 
     *         one named image 
     */  
    private String sound;  
    private String image;  
     
      
    {  
      
    /** 
     * INSERT CODE BELOW 
     *   Write a construtor method 
     *         The method should have two parameters 
     *                type:  String     name:   newImage 
     *                type:  String     name:   newSound 
     *              
     *         The method should 
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter 
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter 
     *                
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image. 
     *                
     */  
    public Emoticon();  
    image = newImage;  
    sound = newSound;  
    setImage(image);  
}  
  
    /** 
     * Act - do whatever the Emoticon wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
        // When the mouse is click on this object, play the sound.  
         if(Greenfoot.mouseClicked(this))  
         {  
             /** 
              * INSERT CODE BELOW 
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound 
              *      
              */  
             Greenfoot.playSound(sound + ".wav");  
               
         }  
              
    }      
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class HomeworkWorld here. 
 *  
 * @author ()  
 * @version (June 3, 2014) 
 */  
public class HomeworkWorld extends World  
{  
    /** 
     * INSERT CODE BEOLW 
     * Add two instance variables that are of type String[] 
     *        one will be an array of images 
     *        one will be an array of sounds 
     *        DO NOT INCLUDE THE FILE SUFFIX - add that through code later 
     *         
     *        Note - each image is 55x55 
     */  
    private String[] image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};;  
    private String[] soundfile = {"hello", "happy", "crying", "ohno", "raspberry"};  
  
    /** 
     * Constructor for objects of class HomeworkWorld. 
     *  
     */  
    public HomeworkWorld()  
    {     
          
        // Create a new world with 400x100 cells with a cell size of 1x1 pixels.  
        super(400, 100, 1);   
   
            //make the Emoticon Images  
            for(int i = 0; i<5; i++)  
            {  
                   addObject(new Emoticon(images["smiley1", "smiley2", "smiley3", "smiley4", "smiley5"], soundFile["hello", "happy", "crying", "ohno", "raspberry"]), 70*(i+1), 50);  
            }  
        }  
    }
danpost danpost

2014/7/4

#
In line 37 of the Emoticon class, change the semi-colon to an open curly bracket and add your parameter list inside the parenthesis. I only asked you to change the name of the arrays on line 51 of your HomeworkWorld class code, which is now your line 36. The only thing that should go inside square brackets are integers or an int field name, like 'i'). Please refer to the page on Arrays in the Java tutorial.
Lboogie0208 Lboogie0208

2014/7/4

#
I still get illegal start of expression
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class Emoticon here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class Emoticon extends Actor  
{  
    /** 
     *INSERT CODE BELOW 
     *    Create two instance varaibles of type String 
     *         one named sound 
     *         one named image 
     */  
    private String sound;  
    private String image;  
     
      
    {  
      
    /** 
     * INSERT CODE BELOW 
     *   Write a construtor method 
     *         The method should have two parameters 
     *                type:  String     name:   newImage 
     *                type:  String     name:   newSound 
     *              
     *         The method should 
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter 
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter 
     *                
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image. 
     *                
     */  
    public void emoticon(String image[i], String soundFile[i])
    {  
    image = newImage;  
    sound = newSound;  
    setImage(image);  
}  
  
    /** 
     * Act - do whatever the Emoticon wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
        // When the mouse is click on this object, play the sound.  
         if(Greenfoot.mouseClicked(this))  
         {  
             /** 
              * INSERT CODE BELOW 
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound 
              *      
              */  
             Greenfoot.playSound(sound + ".wav");  
               
         }  
              
    }      
} 
   
danpost danpost

2014/7/4

#
Line 37 does not look like that which I had shown above. Also, the passed values will not longer be array elements, just simple String objects. Your instructions (in the commented area above the line) show exactly what you should use as the parameters. Please refer to the page on Constructors in the Java tutorials.
Lboogie0208 Lboogie0208

2014/7/4

#
Why am I not getting this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class Emoticon here. 
 *  
 * @author (Lorren Williams)  
 * @version (June 3, 2014) 
 */  
public class Emoticon extends Actor  
{  
    /** 
     *INSERT CODE BELOW 
     *    Create two instance varaibles of type String 
     *         one named sound 
     *         one named image 
     */  
    private String sound;  
    private String image;  
     
      
    {  
      
    /** 
     * INSERT CODE BELOW 
     *   Write a construtor method 
     *         The method should have two parameters 
     *                type:  String     name:   newImage 
     *                type:  String     name:   newSound 
     *              
     *         The method should 
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter 
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter 
     *                
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image. 
     *                
     */ 
    public Emoticon(int newImage, int newSound){
    image = newImage;  
    sound = newSound;  
    setImage(image);  
}  
  
    /** 
     * Act - do whatever the Emoticon wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
        // When the mouse is click on this object, play the sound.  
         if(Greenfoot.mouseClicked(this))  
         {  
             /** 
              * INSERT CODE BELOW 
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound 
              *      
              */  
             Greenfoot.playSound(sound + ".wav");  
               
         }  
              
    }      
}
danpost danpost

2014/7/4

#
Please refer to lines 27 and 28; then, correct line 37 accordingly.
danpost danpost

2014/7/4

#
You are still going to have problems with your image files not being found. From your original posting of the HomeworkWorld class, the suffixes for the image and sound strings were to be concatenated when passing the fields to the new Emoticon objects (within the 'for' loop, in the 'new Emoticon(/* here */);').
You need to login to post a reply.