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

2014/5/6

Questions about constructor

getreides getreides

2014/5/6

#
Hello, I am dealing with chapter 5 at the moment and the concept of constructor popped up some questions. As suggested in the chapter, I added all the keys using a string array/loop via the world class. It all works fine. Now, I'd like to understand why :-) In the classes for both white and black keys, I used the following variables/ constructor (here's the one for my white keys in the class called Key)
1
2
3
4
5
6
7
8
9
    private boolean isDown;
    private String key;
    private String sound;
 
public Key (String keyName, String soundFile)
{
        key = keyName;
        sound = soundFile;
}
What I don't quite understand in this concept is the following: - How does the system "know" that when I press i.e. the a-key this very key on the piano is pressed and the respective sound must be played? I understand that using the string array/loop a respective key-/sound-combination is created; but how can the key class "know" which key is currently pressed? I mean, I don't see the connection between the world class and the key class - Why do I need four string variables (key, keyName, sound, soundfile) to make it work? Why isn't key/sound enough? - Why do I need to create the variables keyName/soundFile in the constructor itself and not in the same place like key and sound? And why do I have to create keyName/soundFile in the "( )"? Thanks for your help in advance!
danpost danpost

2014/5/6

#
There should be more to the Key class than what you are showing. You should have an 'act' method that will check for the key stored in the 'key' field and play the sound stored in the 'sound' field if that key is down. It will at the same time set the 'isDown' field to true. Another check will look for the key to NOT be down and reset the 'isDown' field back to false. The world does not need to know when these events occur; only the Key objects do. The argument values brought into the constructor ('keyName' and 'soundFile') are only local variables (only the constructor is aware of them). The Key object being created needs to save those values in its fields ('key' and 'sound') so that it can use them later (after the Key object is added into the world). The two String objects, 'keyName' and 'soundFile', must be declared as parameters to the constructor call in order for those values to be passed from your world object to the Key object being created. Having them in the "( )" informs the compiler as to how many variables and of what type are to be passed when using that construct. Please refer to Java tutorial page on Passing arguments to methods and constructors for more detailed information. You may want to check out other pages within the tutorial by selecting them from the list on the left side of the window.
getreides getreides

2014/5/10

#
Dear Danpost, first of all, thanks for your helpful reply, I begin to understand the concept of passing arguments to constructors. I've tried this concept with the piano scenario with a third string array ("test") just for testing purposes, and it went well:
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
private String[] whiteKeys =
        {
            "a", "s", "d", "f", "g", "h", "j", "k"
        };
     
private String[] whiteNotes =
        {
            "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c"
        };
 
private String [] test =
        {
            "1", "2", "3", "4", "5", "6", "7", "8"
        };
 
....code omitted...
 
        public void makewhitekeys()
        {
            int i = 0;
            while (i < whiteKeys.length)
            {Key key = new Key (whiteKeys[i],   
             whiteNotes[i] + ".wav", test[i]);
             addObject (key, 175 + (i*63), 140);
             i = i + 1
            }                                                                
         }
However, whenever I try to add a different argument in the Key key = new Key ...)-line, like a private int I declared on top of the world class, Greenfoot won't compile, stating "constructor Key in class Key cannot be applied to given types; required: java.lang.String.java.lang.String.java.lang.String.int; found: java.lang.String.java.lang.String.jaa.lang.string; int; int; reason: actual formal argument lists differ in length" My world class looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    private String[] whiteKeys =
        {
            "a", "s", "d", "f", "g", "h", "j", "k"
        };
     
    private String[] whiteNotes =
        {
            "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c"
        };
 
    private int number;
 
...code omitted...
 
Key key = new Key (whiteKeys[i], whiteNotes[i] + ".wav", number);
in my Key class, it looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
private String key;
private String sound;
private int number;
 
 public Key(String keyName, String soundFile,
 String testKey, int numberKey)
 {
 key = keyName;
 sound = soundFile;
 test = testKey;
 number = numberKey;
 }
numberKey doesn't serve any useful purpose; it is just a trial to implement an int in the loop/constructor. It seems the loop in the world class only accepts Strings as arguments and no int. But why? Thanks in advance!
danpost danpost

2014/5/10

#
The problem is that line 5, 'public Key(String keyName, String soundFile, String testKey, int numberKey)', tells the compiler that three Strings and one int are to be passed to create a new Key. If you pass anything other than that, like only two Strings and one int, it will complain.
getreides getreides

2014/5/12

#
Ah yes indeed, thank you so much for your help! I might get back once more after having tried some stuff:
You need to login to post a reply.