CS2 Final Exam – Review Notes Unit 1
Total Page:16
File Type:pdf, Size:1020Kb
CS2 Final exam – Review notes Unit 1 Java is an object-oriented language. A Class in GreenFoot is a definition of “something” that you can create and manipulate in your program. Objects are created from a class. You can think of the class as a blueprint and an object as a house made from the blueprint, or the class as the cookie-cutter and the object as the cookie. You can make many objects from one class. All Greenfoot worlds have a World and Actor class when they are first created. Things that an object can do are called methods. The Crab class below has one method, act(). The Act method executes many times when the Run button is clicked. The Crab class is an Animal and an Actor. It can also run the methods from Actor and Animal. The class hierarch below shows the relationship between the classes: 1 CS2 Final exam – Review notes Below is an example of method definitions: o public void move() o public boolean canSee() The word in front of the method name tells us if the method just does a task but does not report a value or if it reports a value. The word void means that the method does not report a value. It just performs a task (similar to a command block in BYOB). The word boolean means that the method reports a boolean value of true or false (similar to a predicate block in BYOB). The word int means that the method reports a whole number value (similar to a reporter block which can return type other than Boolean). This word in front of the method name is referred to as the return type of the method. We can change how an Actor acts by changing the statements in the act() method. The code for the Crab (above) makes the Crab move 1 pixel and turn 5 degrees. move() and turn() are methods that the Crab class inherited from the Actor class. The turn method requires an input value to do its job. An input to a method is called a parameter. The code in the CrabWorld class determines the size of the world display and the unit of movement for actors in the world. A CrabWorld is 600 x 400 cells in size. In this case, a cell is 1 pixel. 2 CS2 Final exam – Review notes The method of Animal: boolean atWorldEdge() returns true if the Animal is at the edge of the world. Otherwise, it returns false. Below is an if statement that uses the atWorldEdge() method: In Java, the boolean condition to be checked by the if statement is enclosed in parentheses (). In this case, the method atWorldEdge() is called. The method, atWorldEdge() is a predicate method. A predicate method is a method that returns a boolean value, true or false. The Greenfoot environment has a method that generates a random number: Greenfoot.getRandomNumber(limit); will generate a random number between 0 and limit – 1. The limit is not included. For instance Greenfoot.getRandomNumber(20) will return a number between 0 and 19. This notation is called dot notation. To use a method of another class, the class or object name followed by a “.” Is used before the method name. The method getRandomNumber() is in a class called, Greenfoot. Depending on how the method is written, it belongs to either a class or an object. From the API, we can tell if the method belongs to a class or an object. Below is the documentation for the getRandomNumber(int limit) method of the Greenfoot class: 3 CS2 Final exam – Review notes The word, static, means belonging to the class. If the header for a method contains the keyword, static, the method belongs to the class. It must be called using dot notation and prefaced by the class name. Greenfoot.getRandomNumber(20); Java has comparison operators which return true or false and can be used in the condition of an if statement. They are: < less than <= less than or equal to > greater than >= greater than or equal to == equal != not equal For example: if (x < 6) if (x==9) Class names always start with capital letters by convention. Spaces are not allowed in names in Java. If you want more than one word in the class name, each word is strung next to each other and capitalized. For example, LittleBear. This naming convention is called PascalCase. It is good programming to break down methods into smaller methods so they are easier to understand and find errors. The Crab’s act method below can be broken down: 4 CS2 Final exam – Review notes Three new methods definitions can be added to breakdown the act method of the Crab: The methods name are: lookForWorms(), randomTurn(), and turnAtEdge(): These methods are new methods of the Crab class. New methods are made after the closing bracket “}” for the act method and before the closing bracket “}” for the class. All of these methods are public The next keyword is void, boolean, int (and other types we haven’t discussed yet), depending on whether the method does a task but does not return a value (void, like a command block), returns a boolean (boolean, like a predicate block) or returns another value (so far, int, like a reporter block). All three of the methods we created are void. Writing the definition for the methods does not run them. In order to run the code inside the methods, the methods must be called. 5 CS2 Final exam – Review notes We delete all the statements from inside the act() method and added method calls: Note that by convention, method names start with lowercase letters. Spaces are not allowed in names. If multiple words are used, they are written in camelCase. Names written using the camelCase naming convention, start with lowercase. Every other word in the name starts with an capital letter. For example: turnAtEdge(); The Greenfoot class has a method, boolean isKeyDown(String key) It is a static method which means that it belongs to the class and must be called using the name of the class with dot notation: if (Greenfoot.isKeyDown(“left”)) It requires a parameter of type, String. A String is a data type like int or boolean. It is the name given to a sequence of characters following each other and enclosed in double quotes, “left”, “123Hello”, “Hello World” are all examples of Strings. In this case, the String parameter is the name of a key on the keyboard, “up”, “down”, “left”, “right”, “a”, etc. 6 CS2 Final exam – Review notes If the key is down (has been pressed) the method will return true. Otherwise, it will return false. We added a method checkKeyPress() to the Crab class. If the “left” key is pressed, it makes the crab turn left (negative degrees). If the “right” key is pressed, it makes the crab turn right (positive amount): We changed the act method to call the methods: This made the crab turn left or right depending on the key pressed. The Greenfoot class has a method which ends a project. We changed the lobster class to end the project when it ate a Crab. : 7 CS2 Final exam – Review notes The Greenfoot class has a method which plays a sound wav file. We changed the Crab class to play a sound when it ate a Worm: In order for this to work, the slurp.wav file must be in a sub-folder, sounds, in your project. Whenever a change is made to your code, the class and project must be compiled. When a Java program is compiled, it is run through a program called a compiler. Many programming languages have compilers. A compiler looks at the code to see if there are any syntax errors. Examples of common syntax errors are: a grammar error, misspelled word, missing semicolon, unbalanced parenthesis, unbalanced brackets, missing parameter values. All the syntax errors in a program must be eliminated before a program can be run. The compiler converts the program written in a high-level language, like Java, that humans can understand into a digital format that your computer can understand and run. 8 CS2 Final exam – Review notes Unit 2 Structure of a class According to our class convention, classes have the following format: public class classname { Instance variables Constructors Methods } The first line is the class header. o It is the start of the program. o The class name is followed by begin and end brackets { } o All the statements in the program are written inside the brackets. If a class is a subclass of another class (for example, Crab is a subclass of Animal), the keyword, “extends” is used. o General form: public class subClassName extends superClassName o Specific example: public class Ball extends Actor An object is created from a class. o The class is the definition for an object type. o Many objects can be made from one class. o The class is the “cookie-cutter”. The cookies are the objects. The term, instance, means object. An instance of a class is an object of the class. An instance variable is used to store information about objects. The Instance variables of the class are defined right after the class header: The Ball class has one instance variable, imageNumber. 9 CS2 Final exam – Review notes Instance variables are created using the keyword, private. One class cannot access the instance variables of another class. The type of the variable follows the keyword, private. All variables in Java must have a type. Variable types fall into two categories: primitives and objects.