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

3 days ago

Assistance In Generalizing Code - Method

DaRafster DaRafster

3 days ago

#
I'm attempting to generalize my code through methods, however since I'm a newly introduced programmer, I need help. Here is my code I'm trying to generalize:
/**
 * A piano that can be played with the computer keyboard.
 * 
 * 
 * @version: 0.1
 */
public class Piano extends World
{
    /**
     * Make the piano.
     */
    public Piano() 
    {
        
        super(800, 340, 1);
        
        Key key1 = new Key("C", "2c.wav");
        addObject(key1, 50, getHeight()/2);
       
        Key key2 = new Key("D", "2d.wav");
        addObject(key2, 115, getHeight()/2);
        
        Key key3 = new Key("A", "2d.wav");
        addObject(key3, 180, getHeight()/2);
        
        Key key4 = new Key("B", "2b.wav");
        addObject(key4, 245, getHeight()/2);
        
    }
I'm trying to generalize the code by putting it into this method:
public void keyPlacement()
    {
        
        
        
        
    }
}
What values would I have to put within the ( ) of this new keyPlacement method in order for this to work?
DaRafster DaRafster

3 days ago

#
I would also like an explanation as to why. I want to improve my understanding of java, so a brief and thorough explanation would be very much appreciated!
danpost danpost

3 days ago

#
DaRafster wrote...
I would also like an explanation as to why. I want to improve my understanding of java, so a brief and thorough explanation would be very much appreciated!
You can move lines 17 thrru 27 into the new method (those lines create the keys and place them in your world). Now, for that code to be executed at the place it was previously at, you will need to "call" that method from where those lines were:
keyPlacement();
DaRafster DaRafster

yesterday

#
danpost wrote...
DaRafster wrote...
I would also like an explanation as to why. I want to improve my understanding of java, so a brief and thorough explanation would be very much appreciated!
You can move lines 17 thrru 27 into the new method (those lines create the keys and place them in your world). Now, for that code to be executed at the place it was previously at, you will need to "call" that method from where those lines were:
keyPlacement();
I understand where you're trying to tell me, and I've thought of that, but I would still like to generalize it EVEN further. Here is my failed attempt of trying to generalize this code further:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. K├Âlling
 * @version: 0.1
 */



public class Piano extends World
{
    private String keyName;
    
    
    /**
     * Make the piano.
     */
    public Piano() 
    {
        
        super(800, 340, 1);
        
        keyPlacement(50, getHeight()/2);
        keyPlacement(115, getHeight()/2);
        keyPlacement(180, getHeight()/2);
        keyPlacement(245, getHeight()/2);
        
    }
    
    
    public void keyPlacement(int width, int height)
    {
        
        Key key1 = new Key("C", "2c.wav");
        addObject(key1, width, height);
       
        Key key2 = new Key("D", "2d.wav");
        addObject(key2, width, height);
        
        Key key3 = new Key("A", "2d.wav");
        addObject(key3, width, height);
        
        Key key4 = new Key("B", "2b.wav");
        addObject(key4, width, height);
        
        
    }
I hope you can see what I'm trying to do, I would just like to call the method and put all the values within the brackets. GOAL: Every time I call the method keyPlacement, I just need to put the values of the height, width, key name, and sound file so if I ever I need to create a new key, I would simply put in these values
danpost danpost

yesterday

#
Then add those parameters to the method signature and use them:
private void keyPlacement(String note, String file, int width, int height)
{
    addObject(new Key(note, file), width, height);
}
DaRafster DaRafster

yesterday

#
danpost wrote...
Then add those parameters to the method signature and use them:
private void keyPlacement(String note, String file, int width, int height)
{
    addObject(new Key(note, file), width, height);
}
Thank you will give it a shot!
You need to login to post a reply.