Here are the new tools you need for your While, Recursion, Graphics
and Random quiz
Random - Random number generating object
import java.util.Random;
create a new Random Generator : Random
myGen = new Random();
Ask your random generator for a new random number: int x = myGen.nextInt(5);
Get a random # starting at 0.
While:
While loop - repeats as long as a condition is met
statement to set up so that the first time through the loop it will run
while (test - if it is true, the code in the braces will run)
{
// do something, including changing some variable so the test will be false
}
- loop control - know how to set up the test you want; make the test pass the first time; make the test fail sometime during the loop
- just like a for loop:
- 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
- zero out a variable before the loop (double sum = 0:)
- add to that variable inside the loop (sum = sum + whateverYouWantToCount;)
- print the total after the loop (System.out.println(sum);)
- 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.
do While - repeats, checking the condition after execution of the loop
- similar to while loop
- always executes once
- checks condition at end of loop instead of beginning
- puts a semi-colon at the end of ()
Recursion - a method repeatedly calls itself until some stop
condition - winds and unwinds
public static void method(int x)
{
if ( condition to stop ) handle bottom of the winding - where you will stop and turn around
{
// do something that does not call method again
}
else
{
// do something and call method passing it a different x
}
}To create:
- Identify the pattern;
- Identify the end of the pattern (when it should stop repeating), and code that to return a value.
- Code the pattern to call itself, passing a parameter different than what the method was called with.
Graphic - writing on Fang or PictureIts canvas -- will not ask you to write
new code for graphic on test
Picture It
import picture knowledge: javalib.worldimages.*;
import color knowledge: java.awt.Color;
Make WorldImages using AImage.make<shape> , AImage.makeFromFile
Place images on top of each other using <your world image>. place (<your other world image> , x,y)
x indicates how far to the right; y indicates how far down (positive)
Show your image with <your world image>.show()
Fang
To set up fang:
import fang2.core.*;
import fang2.sprites.*;
import java.awt.*;
import java.awt.geom.*;Your class must extend GameLoop ex: public class Wackadot extends GameLoop
Steps to add shapes to the panel:
- Override startGame()
@Override
public void startGame()- Create a Sprite (a shape): Sprite yourvar = new OvalSprite(.1,.1) // RectangleSprite(l,w) // LineSprite(x,y, x,y)
- Adjust location - setLocation(x,y)
- Adjust color - setColor(Color.RED)
- Adjust scale - setScale (.1) (1 is larger, .01 is smaller)
- Add your shape to the canvas - canvas.addSprite(yourvar)
Concepts:
- A shape is an object
- That shape object has different extensions, (such as Oval, Rectangle, Line), that all have different parameters
- That shape object has properties such as location, color and scale which you set using methods of the shape
- The shape must be added to the canvas to be seen.
- The location is set as x, y, with y being a positive number.
Characters:
Equality Testing
Error Handling - Exceptions -- will not include on
test
try
{ call the method and maybe other statements that you only want to execute after a successful method call}
catch (Exception e) // this is any Exception; to catch only a particular type of Exception, use that type
{ statements you want to execute only when the method call threw an error}
Existing Knowledge:
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
}
}
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.
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
- Create boolean expressions:
- use when you want to save the result of a true/false question
- use comparison operator: ==, <,>,<=,>=,!=
- use logical indicator
- OR is ||
- AND is &&
- 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)
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:
Here is the sheet you will have for your test
- 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