Here are the tools you need in your Java Toolbox in your head. The red
words cannot be changed. The green can be changed by you:
- Create a new program (like
creating a new world with myfirstmethod in Alice):
public class MyClassName
{
public static void main( )
{
// your statements will go
here
}
}
- create a variable
- In Java: <type> <variable name> ; example: int
myIntVar;
- declare with correct type: int,
double, char, String, Scanner, boolean, (only String and
Scanner captialized)
- In Alice: Click the new variable
or new parameter button and choose number, String or boolean or other
- change variable contents:
- In Java: <variable name> = <some expression>; example: myIntVar = 5 ;
- In Alice: Drag the variable to a
blank line to create a set statement.
- Say something on the screen:
- In Java: System.out.println(<whatever you want
to print> + <whatever
you want to print> );
- example: System.out.println("Hello, my name is " + myName +
"My number is " + myNumber);
- Remember: the semi-colon; capitalize System, use both
parentheses;
- Remember quotes around text and not around variable names
- variations: printf and print
- In Alice: Drag the say
command over.
- Work with methods
- Define a method
- add a method right before the class closing curly braces
- a method header is immediately before the method's curly
braces
- ex: public static double myMethod(int x){ // your statements go here}
- a method's parameter variables are created inside
the parentheses (ex: in the statement above, int x is the parameter
creation
- a method's return type is defined before the
method name (ex: in the statement above, double is the return type)
- a method with a return type needs to end with a return
statement.
- a method with no return type (void) cannot have a
return statement
- Call a method:
- for static methods you create: <method name> (any
parms). ex: myMethod(3);
- for static methods that are not in your class:
<class>.<method name> (any parms) ex: Math.pow(5,2);
- for instance methods that are not in your class:
<object>.<method name> (any parms) ex: myScanner.nextDouble();
- parameters must match in type and number
- for methods that return a variable: set it = to something.
ex: myDoubleVar =
myMethod(3);
- In Alice
- Define a method by clicking the new method button
- Define a function by clicking the new function button
and filling in the return statement
- Call a method by dragging the method or functionfrom
the character or world into your statement area and fill in the
parameters
- Create boolean expressions:
- use when you want to save the result of a true/false question
- use comparison operator: ==,
<,>,<=,>=,!=
- use logical indicator
- example: myBooleanVar = x > 5 && x < 10 (x can
be between 5 and 10)
- example: myBooleanVar2 = x <5 || x >10 (x can be less
than 5 or greater than 10)
For the midterm, you need to understand how to read from a screen,
but do not need to memorize the commands. The commands in red will be
available to you on the day of the test. You may be asked to create a
program that reads from a screen, so you will need to know the order to
place the commands.
- Setup to read something from the screen:
- In Java: there are 2 commands needed:
- before the class starts, import the scanner class
using: import java.util.Scanner;
- inside the method, create a scanner object using: Scanner kbd = new
Scanner(System.in);
- In Alice: Not needed, but
similar to importing a character using file / import
- Read something from the screen:
- In Java: <your variable> = < name of the scanner you
created above>.nextLine();
- Ask the user for something first, or the system will appear
to hang waiting for the user to answer a question you never asked.
- Use the scanner method that matches your variable type.
- example to get the whole line: myStringVar
= kbd.nextLine();
- example to get the next word: myStringVar
= kbd.next();
- example to get the next double: mydoubleVar
= kbd.nextDouble();
- example to get the next int: myIntVar
= kbd.nextInt();
- See
Sun's page on scanner for much more
- In Alice: Drag the variable
down to create a set command and drag over the world's "Ask user for a
number" function.
You need to know how to use these for the midterm, but you do not
need to memorize the commands. If you are asked to call these methods,
you will be given the method name and parameters.
- Math
- absolute value ex: myDoubleVar= Math.abs(#);
- random ex: myDoubleVar = Math.random();
- minimum: Math.min(#,#)
- maximum: Math.max(#,#)
- raise to a power: Math.pow(#,#) : Note: The first number is
raised to the second number
- knows PI and E
- Strings
- stringvar.charAt(3) // will return a character
- stringvar.length() // will return an integer
- stringvar.toUpperCase() // will return a String that is all uppercase
- stringvar.toLowerCase() // will return a String that is all lowercase
- stringvar.subString(2) // will return a String starting in position
2 to the end
- stringvar.subString(2, 5) // will return a String starting in
position 2 and ending in position 4
- stringvar.equals(somethingOtherString) // will return either
true or false (a boolean)
-
ex: newString = myOriginalString.toLowerCase();
Also, strings can be put together into another string with the +
ex: newString = string1 + string2;
Creating methods:
public static return_type method_name (type
parm1, type parm2)
{
...
...
return expression;
}
Calling methods:
object_or_class.method_name(parm1,
parm2);
If / if else / else
if (condition that evaluates to true or false)
{
// do something
}
else if (condition that evaluates to true or false) // remember that everything
that met first criteria doesn't get here
{
// do something else
}
.
. // repeat else if as needed
.
else // remember to put no condition
{
// do something
}
If facts:
- else is not needed
- when the condition is met, it executes inside the parentheses and then goes
to the end of the if and continues on down
- You can have ifs inside the brackets for another if
Loops
For loop - repeats when you know the number of times to repeat
for (statement to start the loop; condition that evaluates to true or false;
statement to run when the loop repeats)
{
// do something
}
For facts:
- Don't put a semi-colon at the end of the for condition
- Remember to create the variable used to count the loop with an int statement
somewhere in your program. (either in the for statement or before it)
- You can add to the counter between the brackets as well.
- After the first time through, the for loop does the "statement to
run when the loop repeats" first, and then checks whether the condition
fails.
- When the condition fails, it goes to the bottom bracket of the loop and
continues on
- You can have loops inside loops
- Stopping when equal to a single number is dangerous - go with < or
> instead when you can.
While loop - repeats until some condition is met
while (condition that evaluates to true or false)
{
// do something
// at some point, set the condition to stop
}
- Don't put a semi-colon at the end of the while condition
- Remember to have something inside your loop that will make the condition
fail sometime, or it will NEVER END
- When the condition fails, it goes to the bottom bracket of the loop and
continues on
- The condition is only checked when it gets to the bottom bracket. Reaching
the bottom bracket causes it to go back up to the top and check
- You can have loops inside loops
Page you will have available during the midterm
test
Page you will have available during the
decision and loop test