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

2014/6/28

Assistance on constructor method with two parameters

jagregg jagregg

2014/6/28

#
Having problem on line Emoticon emoticon = new Emoticon(image, sound) This error I get ';' expected error so my world does work either. Here is the one for Emoticon and here is the one for World. Please assistance me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[code]
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Emoticon here.Create 5 different images and sounds to run.
 *
 * @author ()
 * @version (
 */
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");
         }
             
    }   
}
[code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[code]
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class HomeworkWorld here.  This is use to create 5 different
 * scenarios creating images and sounds together.
 *
 * @author ()
 * @version ()
 */
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 [] sound = {"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
         *     
         */
        for(int i = 0; i<5; i++)
        {
            while(i<5)
            {Emoticon emoticon = new Emoticon();
                addObject(newEmoticon(image[i] + sound[i]));
            }
        
    }
}
[code]           
danpost danpost

2014/6/28

#
You are missing the following: * a semi-colon, ';', at the end of line 36 of the Emoticon class; * a constructor declaration statement between the field declarations on lines 18 and 19 and the constructor code, lines 36 through 39 of the Emoticon class; * a dot, '.', before the file name suffix on line 55 of the Emoticon class; Then, lines 37 and 38 are trying to set arrays to String values. I think you want to save individual Strings in each of the Emoticon objects created, not arrays of Strings. Lint 48 of the HomeworkWorld class is not needed. That condition is accounted for in the 'for' statement on line 46. And line 50 has an unknown method 'newEmoticon' being called using a concatenated String value as an argument (which I doubt you want to do). Also, the 'addObject' method accepts an Actor and two ints as arguments (not just an actor -- if that is what is returned by 'newEmoticon'). Please refer to the Java tutorial page on Classes for information on class constructors and how they are used.
You need to login to post a reply.