1/66
Exam 2 will be on Monday October 28th. 30 Multiple Choice Questions IPO Charts - 2 Questions Binary Numbers - 7 Questions Greenfoot Chapter Notes - 14 questions Python Chapter Notes - 7 questions
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are objects in Greenfoot?
Many objects can be created from a class
What are methods in Greenfoot?
Objects have methods, invoking performs an action
What is an int in Greenfoot?
Integer, whole numbers (numbers without decimal points, can be positive or negative
What is boolean in Greenfoot?
True or False
What is the return type void() in Greenfoot?
Void means that no information is returned when it is invoked, they just carry out their action and then stop.
Methods with void return types are like commands, invoke turn left, and the wombat will turn left. ACTIONS
Methods without return types are usually just commands to the objects that make it do something
What are non- void return types in Greenfoot?
Like questions- boolean canMove() is like asking the wombat “Can you move"?” and because it is boolean, it answers true or false (yes or no)
int getLeavesEaten() using this we get the information of how many leaves the wombat has eaten.
Methods with return types usually just tell us something about the object, but do not change the object
What are parameters in Greenfoot?
int getLeavesEaten() or void setDirection (int direction)
The parentheses after the method name hold the parameters list. This tells us whether the method requires additional information to run.
If there is nothing in the parentheses then it has an empty parameter list, which means it will just run, and does not expect a parameter.
If there is something in the parentheses then it expects parameters; additional information we need to provide. Every parameter is defined by two words, first the type then the name of it- void setDirection (int direction)
Shows us that it expects one parameter of type int which, specifies direction. At the top of the dialogue box, assuming commenting is done correctly it specifies more information. If we enter something that is not expected we get an error
What is a method signature in Greenfoot?
The description of return type, method name and parameter list
What are world classes in Greenfoot?
Always there by default. Every scenario will have at least one below it, in this case Crab World. Crab World is-a World. Also referred to by saying Crab World is a subclass of World
Unless we start without a scenario, Greenfoot automatically creates an object of the world subclass.
What are Actor Classes in Greenfoot?
Objects that can be placed into the world, for example animal, crab, lobsters, worms and pelicans
What are subclasses and Superclasses in Greenfoot?
Subclass is a class that represents a specialization of another. In Greenfoot, this is shown with an arrow in the class diagram
Superclass is conversely to subclasses. We would say that Actor is a superclass to Animal.
Actor clas and animal have certain functions and methods that the Crab, lobster, worm and pelican inherit from them. So since they are below animal, they will inherit and methods or actions that are in the animal class.
What is a source code in Greenfoot?
Every class is defined by a source code. This code defines what objects of this class can do. We can look at the source code by opening the class’s editor.
What is compiling and compilation in Greenfoot?
Compiling- Make the change with an error, make the change correctly.
Compilation- Computers do not understand source code. It needs to be translated to machine code before it can be executed.
What is a method call in Greenfoot?
An instruction that tells an object to perform an action. The action is defined by a method of the object.
What is a parameter in Greenfoot?
Additional information can be passed to some methods within the parentheses, the value passed is called a parameter
What does it mean for something to be in sequence in Greenfoot?
Multiple instructions are executed in sequence, one after the other, in order in which they are written
What is an error message in Greenfoot?
When a class is compiled, the compiler checks to see whether there are any errors. If an error is found, an error message is displayed
True or False: A subclass inherits all the methods from its superclass. That means that is has, and can use, all the methods that its superclass defines?
True
What is an if-statement?
Can be used to write instructions that are executed only when a certain condition is true.
Which statement is NOT a valid Crab Class statement?
A. move(10);
B. turn(-10);
C. move(10.5);
D. turn(5×2);
E. move(-2×5);
move(10.5);
When you input move(10.5); an error comes up saying “incompatible types: possible lossy conversion from double to int” You cannot input decimal numbers since it will lose the decimal portion of the number. Since you can't hold the decimal in the integer variable, because it only allows whole numbers. It requires a whole number integer, not a decimal.
How can you make Crabs move randomly in Greenfoot?
getRandomNumber
What is dot notation in Greenfoot?
When methods are called that were defined in our own class (crab) or inherited (animal), just typing the method name and parameter list was enough. However, because it is a method that is from another class, not inherited from animal, we need to use a notation called dot notation. Since the getRandomNumber method is not in the Crab or Animal class, but in a class called Greenfoot, we write Greenfoot in front of the method call.
Greenfoot.getRandomNumber(20)
What does the parameters in Greenfoot.getRandomNumber(20) mean?
This will give us a random number between 0 and 20. the limit (20) is EXCLUDED, so the number is actually in the range of 0-19.
What are static methods in Greenfoot?
Methods may belong to objects or classes. When methods belong to a class we write:
class-name.method-name(parameters); to call the method
When a method belongs to an object, we write:
object.method-name(parameters); to call it
Both kinds of methods are defined in a class. The method signature tells us whether the method belongs to object of that class, or to the class itself.
Methods that belong to the class itself are marked with the keywords static at the beginning of the method signature. Ex. static in getRandomNumber(int limit)
What do these symbols mean in Greenfoot?
< > <= >= == !=
< less than
> greater than
<= less than or equal
>= greater than or equal
== equal
! = not equal
How do you make a new method in Greenfoot?
/**
*Check whether we have stumbled upon a worm #method description
*If we have, eat it. If not, do nothing #not read by computer, for humans only
/*
public void lookForWorm() #this is the new method
{
. if (canSee(Worm.class))
. {
. eat(Worm.class)
. }
}
The first four lines are a comment. A comment is ignored by the computer, but it is important to human readers. When we define this method, the code does not immediately get executed. We are just defining a new possible action (“looking for a worm”) that can be carried out later. It will only be carried out when this method is called.
How do you call a method in Greenfoot?
Calling it inside the act method
Ex. lookForWorm();
What percentage of the time will the code after this if statement is executed?
if (Greenfoot.getRandomNumber (100) < 7)
A. 700%
B. 70%
C. 7%
D. None of the above
In the original code that I input it was if (Greenfoot.getRandomNumber (100) < 10). And the percentage of the time the crab would turn is 10%. So if it was 100 < 7 it would be 7% of the time. It's almost like a fraction 10 divided by 100 ,multiplied by 100 (for percent) is 10%, so 7 divided by 100 multiplied by 100 would be 7%.
What is API Documentation in Greenfoot?
Lists all classes and methods available in Greenfoot. Often need to look up methods here
What is a string in Greenfoot?
Piece of text (word/sentence), written in double quotes ('““)
“This is a string”
Why should you take a method with a lot of lines and break it up into smaller methods?
You should take a method with a lot of lines and break it up into smaller methods because it is easier to read and understand. Having a method with a lot of lines can become confusing and you may mess up your code. But breaking it up organizes it better and allows you to see each specific method piece in its own area.
What is a constructor in Greenfoot?
Looks similar to a method but has some difference:
-Constructor has no return type specified between the keyword “public” and the name
-the name of a constructor is always the same as the name of the class
It is always automatically executed whenever an instance of this class is created
How do you create new objects in Greenfoot?
Java objects can be created programmatically (from within your code) by using the new keyword Ex new GreenfootImage(“crab2.png”);
How do actors maintain their image in Greenfoot?
By holding an object of type GreenfootIMage. These are stored in an instance variable inherited from class Actor
What are instance variables in Greenfoot?
Also called fields, can be used to store information (objects, or values). Bit of memory that belongs to the object ( the instance of the class) Anything stored on it, will be remembered as long as the object exists.
Is declared in a class by writing the keyword private followed by the type of the variable and the variable name. private variable-type variable-name;
The type of the variable defines what we want to store on it. Always write instance variables declarations at the top of our class, BEFORE constructors and methods
What is an assignment statement in Greenfoot?
Assigns an object or a value to a variable. Enables us to store something into a variable. Written with an equal symbol, variable = expression;
On the left is the name of the variable, on the right is what we want to store. “=” is the assignment symbol. Read it as “becomes”. Variable becomes expression.
image1 = new GreenfootImage(“crab.png”);
What is an if/else statement in Greenfoot?
Executes a segment of code when a given condition is true, and a different segment of code when it is false.
What is binary?
The binary numeral system, or base-2 number system, represents numeric values using two symbols: 0 and 1. More specifically, the usually base-2 system is positional notation with a radix of 2. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers.
How do you count in binary?
0000
0001, (rightmost digit starts over, and next digit is incremented)
0010, 0011, (rightmost two digits start over, and next digit is incremented)
0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented)
1000, 1001, 1011, 1100, ...
How do you convert binary to decimal?
Take the sum of the products of the binary digits and the powers of 2 which they represent. Ex 15 in binary is 1111
(1×2³) + (1×2²) + (1×2^1) + 1×2^0) = 8 +4+2+1 = 15
or 37 in binary is 100101
(1x2^5) + (0x2^4) + (0x2^3) + (1x2^2) + (0x2^1) + (1x2^0) =
(1x32) +(0x16)+(0x8)+(1x4)+(0x2)+(1x1)=
32+0+0+4+0+1=37
OR ANOTHER METHOD
1 0 0 1 1 0 1 1 to decimal
128, 64, 32, 16, 8, 4, 2, 1 (increments of 2 for each digit)
128 0 0 16 8 0 2 1 (write the number if its a 1 above, if its a zero write 0)
Add together for 155
How do you convert decimal to binary?
if its even it ends in 0, if its odd it ends in 1
For example 15 in binary is 1111
15/2 = 7 remainder of 1 —— rightmost digit
7/2 = 3 remainder 1 —— second rightmost digit
1/2= 0.5 gives us a 1 for the leftmost digit
Our quotient got to <1 so we stop, 1111
Or 16 in binary is 10000
16/2=8 remainder 0 -- rightmost digit
8/2= 4 remainder 0 -- second rightmost digit
4/2=2 remainder 0 -- third rightmost digit
2/2=1 remainder 0 -- fourth rightmost digit
1/2=0.5 gives us a 1 for our leftmost digit
Our quotient got to <1 so we stop. Result 10000
What is 11010011 as a decimal?
What is 121 as a binary?
What are variables in Python?
Python uses dynamic variables, meaning the type of the variable can be assigned and changed at will. This also means that the type isnt checked until runtime, resulting in not needing to pre-define the type.
What are the data types in Python?
int: signed whole integer
float: floating point number (decimal)
string: text
booleans: True or False
How can you change data types in Python?
Putting the type you want to convert the variable you want to convert.
str()- changes item to a string
int() - changes item to an integer
float() - changes item to a floating point
Ex. num = 12
num = str(num)
num = float(num)
What are the logical operators in Python?
== equal to statement (same as Java)
!= not equal to statement (same as Java)
or statement (Like | | in Java)
and statement (Like && in Java)
How do you get inputs in Python?
The primary method of user input is “input([prompt])”
myVariable = input(“Please enter your answer: “)
Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, it will still convert it into a string. If you want to accept a number input from a user, you need to perform type conversion on the input value.
How do you output in Python?
The primary method of output in Python is the “print()” function. this is very similar to “System.out.printIn()” in Java, the drawString() method in Greenfoot, and the other various speech tiles in Scratch.
print(“This is a string”) or put it into a variable
myVariable = “This is a string variable”
print(myVariable)
What does \n and \t and + do in Python?
\n creates a new line within the string
\t creates an indent in the string
+ concatenate two strings
How do you end a program?
Once it reaches the bottom of your code, the program will stop/terminate. However, sometimes within a loop, or condition, we want to stop the program sooner. We ca use the exit() function to do so. this operates like the stop(all) tiles in Scratch.
What is an IPO chart?
Identifies a program’s inputs, its output, and the processing steps required to transform the input into the outputs.
I: Input- The information, ideas, and resources used in creating a program
P: Processing- Actions taken upon/using input or stored material
O: Output- Results of the processing that then exit the system
S: Storage- Location(s) where materials inside the system is/are placed for possible use at a later point in time.
Things to consider:
- The text only version of the IPO chart should be made in such a way that it is
general enough that it could work with any user inputs.
- The input section cannot have any math done in it, it is just the raw data inputted
- Any math that needs to be done, should be done and shown in the process section of the chart. Because that is “processing” the data
- If you have multiple calculations as part of your process,
- The output section is reserved for just the one output of the processed information, no further calculations are done here.
What are Python Functions?
Can be used to break down a program into many different, useful parts. if we intend on using the same algorithm in multiple places, a function is likely the best option. However, unlike Java, Python functions do not require a privacy or type reference.
How to do write a Python Function?
Initialize a function using the “def” term
def helloWorld():
. print(“hello world!”)
Can also give a defined function a return value
def myFunction():
. num = 12
. return num
print(myFunction())
Can also set a variable using a return from a function
def question():
. a = input(“what is your lucky number? “)
. return a
luckyNumber = question()
print(luckyNumber + “ is a great lucky number!”
MUST be defined prior to being called (defined higher up in the sequential order of the code)
What are parameters in Python?
Like in Java, we can also pass parameters into a function. To do this, we must define the parameter names in the function definition
def concatenate(strin1, string2):
. newString = string1 + string2
. print(newString)
concatenate(“arch”, “bishop”) so we get archbishop
What does random.randint() do in Python?
Produces a random number between a and b (inclusive of a and b)
What does random.random() do in Python?
Produces a random floating point number between 0 and 1 (exclusive)
What does random.uniform(a, b) do in Python?
Produces a floating point between a and b (inclusive of a and b)
What are while loops in Python?
Formatted similarly to the While loops in Java. they still require the same 3 essential components (counter, condition, increment). The differences are similar to those of using if statements in Python vs Java. We need a : after our condition and indentation of the body instead of curly brackets.
While(condition) repeats until condition is FALSE
What is elif in Python?
A way of saying “if the previous conditions were not meant, then try this condition” The else is not required here, however it is not a bad idea to include one as a “catch all” if none of our conditions are met. There can be multiple elif statements included as part of 1 block of code. Covering several specific conditions
Can you write an elif statement any other way or can you write it differently?
Yes you can write it another way. Instead of having multiple elif statements in one block of code, you can divide it into multiple if statements.
What is possible downside of writing elif statements in seperate if statements in Python?
All three if statements will run(each if statement considered its own block of code) which requires more computing power. In the previous example, as soon as 1 condition is met, the program does not continue checking the other conditions. Furthermore, if we want to make sure that only 1 possible case is executed, keeping it in the if/elif/else format (which is considered one block of code) is a better practice.
What are global variables in Python?
Variables that are created outside of a function are known as global variables. Can be used by everyone, both inside and outside of functions.
What happens if you create a variable with the same name inside a function in Python?
This variable will be local and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
What is the global keyword in Python?
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword. Also use the global keyword if you want to change a global variable inside a function. This is similar to using “this” keyword in Java.
How do you change the value of a global variable inside a function?
Refer to the variable using the global keyword.
REVIEW YOUR GREENFOOT AND PYTHON PROJECTS TO KNOW HOW EVERYTHING PIECES TOGETHER
You got this!!