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

2012/3/15

Help with abstraction

Sneaky4296 Sneaky4296

2012/3/15

#
I need help with a piano I'm making for my programming class. Every time I try to compile I get the error "found int but expected boolean" on my methods with "if" used. Can you guys help me fix this?
Sneaky4296 Sneaky4296

2012/3/15

#
Here's the code for you guys...
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: Jonah Verner
 * @version: 0.1
 */
public class Piano extends World
{
    /**
     * Make the piano.
     */
    public Piano() 
    {
        super(800, 340, 1);
        makeKeys();
    }
    public void makeKeys() 
    {
        int i;
        int keyWidth;
        int keyHeight;
        int spaceAtEdge; 
        int KeyNum = 0;
        Key key = new Key(" ", " ");
        keyWidth = key.getImage().getWidth();
        keyHeight = key.getImage().getHeight();
        spaceAtEdge = (800 - keyWidth*12) / 2;
        String Note = "";
        String Letter = "";
        if (KeyNum = 0)
        {
            Note = "2a#.wav";
            Letter = "a";
            KeyNum++;
        }
        if (KeyNum = 1)
        {
            Note = "2a.wav";
            Letter = "s";
            KeyNum++;
        }
        if (KeyNum = 2)
        {
            Note = "2b.wav";
            Letter = "d";
            KeyNum++;
        }
        if (KeyNum = 3)
        {
            Note = "2c#.wav";
            Letter = "f";
            KeyNum++;
        }
        if (KeyNum = 4)
        {
            Note = "2c.wav";
            Letter = "g";
            KeyNum++;
        }
        if (KeyNum = 5)
        {
            Note = "2d#.wav";
            Letter = "h";
            KeyNum++;
        }
        if (KeyNum = 6)
        {
            Note = "2d.wav";
            Letter = "j";
            KeyNum++;
        }
        if (KeyNum = 7)
        {
            Note = "2e.wav";
            Letter = "k";
            KeyNum++;
        }
        if (KeyNum = 8)
        {
            Note = "2f.wav";
            Letter = "l";
            KeyNum++;
        }
        for (i=0; i<12; i++)
        {
                addObject (new Key (Letter, Note), keyWidth*i + spaceAtEdge + keyWidth/2, keyHeight / 2);
        }
    }
}
Duta Duta

2012/3/15

#
replace if(KeyNum = x) with if(KeyNum == x). = is used when you're setting something equal to something else (int score = 7 makes a variable "score" and sets it equal to 7. == is used when you're checking if something is equal to something else (score == 7 checks if the variable "score" is equal to 7, and returns a boolean, meaning that when score is equal to 7, score == 7 is exactly the same as true) Oh and in case you didn't know (I assume you do but just on the off chance) a boolean is either true or false (just like an int is a whole number) Finally instead of all those if statements, you could use a switch statement, like so:
switch(KeyNum)
{
	case 0:
		Note = "2a#";
		Letter = "a";
		break;
	case 1:
		Note = "2a";
		Letter = "s";
		break;
	case 2:
		Note = "2b";
		Letter = "d";
		break;
	//...Etc (fill in the rest)...
	case 7:
		Note = "2e";
		Letter = "k";
		break;
	case 8:
		Note = "2f";
		Letter = "l";
	}
}
Note += ".wav";
I can see something else wrong with your code but I'll post about that later if no-one points it out before me as I have stuff to do right now (Here's a clue: The code setting Note and Letter is outside the for loop).
You need to login to post a reply.