piccros.blogg.se

Greenfoot piano array
Greenfoot piano array




greenfoot piano array
  1. #GREENFOOT PIANO ARRAY HOW TO#
  2. #GREENFOOT PIANO ARRAY FULL#
  3. #GREENFOOT PIANO ARRAY CODE#

#GREENFOOT PIANO ARRAY CODE#

What does this code do if the g key is not pressed, each time the act method is called? Test your code by running, and pressing “g”.Īdd a couple more piano keys and check that it works for all keys. If you answered the question above “it changes it to the same image (“white-key.png”) each time” then you were correct. This means that Greenfoot is changing the image even when it doesn’t need to do so. IF the piano key is down AND the g key isn’t pressed THENĬhange the image to white key up (normal) IF the piano key isn’t down AND the g key is pressed THEN Writing what we want to actually happen in pseudocode: It becomes obvious that we need to store some information about whether the piano key is down or not. We can do this using a state variable.Ī state variable is just a normal variable, but it stores some information about the state of something in the application. This state is then used to make some decisions about actions in the code. In this case, we want to store whether the piano key is already up or down, so we can make a decision about whether to swap the image.Ī sensibly named variable would be “isDown”. IF not isDown AND the g key is pressed THENĪctivity 7-3 Using states (from Chapter 6 in your text) We can declare it initially (in the constructor) as “false” and then change the state if we need to change the image.Add a new private boolean variable to your class called isDown.Initialise this variable to false in the constructor.

#GREENFOOT PIANO ARRAY HOW TO#

  • To alter our IF … ELSE code to match the pseudocode above, we need to know how to implement “NOT” and “AND” in our conditions.
  • However, now, at the end of the week it is starting to make a bit more sense. As one can see by the length of this post, there was a lot of new information for a beginner like me. This week was a bit overwhelming for me at first. The computer does not know what to do for the 12 position.

    greenfoot piano array

    This could occur, for example, when you have an array that has 12 positions but we have only put 11 assigned variables. Since there are a near infinite number of things that fall in this category, there are a near infinite number of ways to write programs that cause runtime errors. The computer understands the code, it is just that program instructs the computer to do something that it is either incapable or unwilling to do. A run time error, on the other hand, will compile. A syntax error will prevent the code from compiling, as the computer does not understand it. This could include misspelling a word, forgetting a semicolon, or forgetting a line of code. One final thing that was leaned this week was the difference between a syntax error and a run time error. A syntax error or format error is when you make a mistake in writing your code.

    #GREENFOOT PIANO ARRAY FULL#

    The full six video playlist can be found here. These two videos are the last two of six videos that explain everything that is taught in chapter 5 of the Greenfoot textbook and is explained above.

    greenfoot piano array

    Each item in an array is called an element, and each element is accessed by its numerical index. Instead of having a separate code for each object that is needed, an array groups similar variables together. The length of an array is established when the array is created. An array is a container object that holds a fixed number of values of a single type. An array is an object that can hold many variables, and thus can store many values. This can be accomplished through a concept called an array. Now, we have the task of being able to control each piano key with its own keyboard key and to produce its own sound. If it is always true you will create an infinite loop, which means the instructions will be repeated an infinite amount of time. This is important because without it, the condition will always be true or never be true. It is important to note that a counter that increases the variable must be included in the statements portion. The general for a while loop is as follows: This is similar to the for loop, although it has a few differences. It can describe the characteristics of the object.Īnother new concept is the while loop. A constructor is called automatically when a new instance of an object is created. It constructs the values (provides data for the object). It is invoked at the time of object creation. Secondly, it has no return type (such as void, boolean, int, ect.). A constructor is a special type of method that is used to initialize the object. First of all, it has the same name as the class name. It looks a lot like a method, however, there are two big differences. This introduces a new topic, the constructor.






    Greenfoot piano array