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

2021/6/17

Colored Code Questions

Gabe1098 Gabe1098

2021/6/17

#
I see a lot of colored words and I don't know what they do. 1: "new" I use it a lot but... I don't... really know what it does... 2: "return" I REALLY want to know what this does. 3: "difference between void and boolean" I know boolean has a return statement but I don't even know what return does. 4: "is int and a variable the same thing?" I do a coding language called scratch and there is no such thing as an int and in greenfoot SOMETIMES ints do the same thing as variable. 5: "true and false" by the name of it I can tell that it just means is it true? or is it false? but I don't know when or how to use it. 6: "null" I'm pretty sure it means like nothing or NaN or just "not" and I sometimes see it using it like this "=! null". 7: "static" I literally never used this and I literally have no idea what this is... 8: "import" I see this on the top of the code and I sometimes use it when I'm doing a tutorial but I I don't know what it does... I'm pretty sure it adds extensions?
danpost danpost

2021/6/17

#
1. new 2 & 3. return 4. No. int is a primitive type (for object type Integer), while a variables can hold values (or references) of any type. 5. Just like 1 and 5 are (or can be) literal int values, true and false are literal boolean values. 6. Like true and false, null is also a literal value. It is assigned to any object reference variable that is unassigned. 7. See variables link above (for static). 8. import statements allow access to classes outside your scenario folder that java does not automatically make available (java.lang).
Gabe1098 Gabe1098

2021/6/17

#
Thank You!!! but I don't understand 2, 3, 7 but I might know what return means. If I do this?
private int (int test) {
   int number=0;
(some code);
return number;
}
does that just set the int test to the int number?
Gabe1098 Gabe1098

2021/6/17

#
also what about 1: do 2: long 3: double 4: while 5: final
danpost danpost

2021/6/17

#
Gabe1098 wrote...
I might know what return means. If I do this?
private int (int test) {
   int number=0;
(some code);
return number;
}
does that just set the int test to the int number?
First off, line 1 is totally wrong. It does not have a name; also, usually (at least, for "getter" and "setter" methods), you would not have both a parameter and a return value. A method declaration line consists mainly of three parts -- a return type followed by the method name, followed by a list of parameters within parenthesis separated by commas. Each parameter, if any are given, will be its type followed by its provided name: returnType methodName(<< parameterType1 parameterName1 << , parameterType2 parameterName2 << ... >> >> >>) If the method does not return a value, then the void keyword is used for returnType. Method declaration lines may begin with one or more modifiers Access modifiers: public protected, or private; Static modifier: static (makes method a class method instead of an instance method) Final modifier: final; (prevents overriding the mehod) A normal "getter" and "setter":
// with some field
fieldType fieldName;

public fieldType getFieldName()
{
    return fieldName;
}

public void setFieldName(fieldType parameterName)
{
    fieldName = parameterName;
}
Basically, return means to exit the method and resume execution of the method calling this method. If a value is returned, it must be dealt with on the calling line of code or it will be lost.
danpost danpost

2021/6/17

#
Gabe1098 wrote...
what about 1: do 2: long 3: double 4: while 5: final
1. do is a program flow looping operation. Code within its brackets will repeat continuously until a break statement is encountered or until a while conditional expression fails. 2. long is a primitive type meant for quite large integer values. 3. double is also a primitive type meant for modest non-integer values. 4. while(conditionalExpression) controls looping. It can be used alone to start a loop with the condition being checked BEFORE each pass or it can be appended to a do loop to check the condition AFTER each pass. 5. final modifies a method so that it cannot be overridden or modifies a variable to make it a constant (its value cannot be changed). Note that as an object reference, the object can still be modified, but it cannot be replaced by another object.
Gabe1098 Gabe1098

2021/6/18

#
Thank you but wouldn't
public void act() {
   (code);
}
do the same thing as "do"?
Super_Hippo Super_Hippo

2021/6/18

#
The act method is executed once per act cycle for the active world and all actors in the world. A (do-)loop executes until it finishes. Nothing else is happening in the meantime.
Gabe1098 Gabe1098

2021/6/18

#
Super_Hippo wrote...
The act method is executed once per act cycle for the active world and all actors in the world. A (do-)loop executes until it finishes. Nothing else is happening in the meantime.
Thank you so much!!!
You need to login to post a reply.