Here are the new tools you need for your Java methods quiz
Work with methods
- Define a method
public static return_type method_name
(type parm1, type parm2)
{
...
...
return expression;
}
- 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
- Class with methods
creation step by step guide
- Call a method:
- object_or_class.method_name(parm1,
parm2);
- You need the class or object if the method is not in your own class.
- Then, you need the object if the method needs to know some information
that is not passed in.
- 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);
- Call Math methods (and save their results into variables)
- 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
- 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);
- 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
Here are older tools you will still be using on your methods quiz:
public class MyClassName
{
// you can define a class variable here
public static void main( )
{
// your statements will go here
// your calls to method1 will have
the form: method1()
}
public static void
method1( )
{
// your statements will go here
}
}
- create a variable
- In Java: <type> <variable name> ; example: int myIntVar;
- declare with correct type: int, double, char,
boolean, (not capitalized)
- declare Object variables with types such as : String,
Scanner, Random, (capitalized)
- change variable contents:
- In Java: <variable name> = <some expression>; example: myIntVar = 5 ;.
- 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
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
}
What you need to know:
- loop control - know how to set up a loop to run a certain number of
times
- scope - variables created in a loop are destroyed at the end of the
loop
- only what is in the {} will repeat.
- to accumulate - initialize a variable before the loop, add to itself
during the loop, and then print the total at the end of the loop
- tracing - be able to trace all variables in the program including the
counter. Know that the first time through the loop it executes the starter
statement, and every time follwing it executes the incrementer.) From
this you can tell how many times a loop will run.
- inner loop - should not use same counter as outer loop; know what it
is, but will not have an inner loop question on the quiz.
FOR coding helps: (but not needed explicitly for the test)
- 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.
You probably need to memorize
this