Here are the tools you need for your Midterm
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
}
}Reminder: After every line, place a ; or {
Reminder: Do not create a variable more than once in the same method
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
Reminder: Ensure your method is defined to always return the correct type.
- 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
Reminder: Containers cannot reach into other container's values; only pass values using input/output variables.
- 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:
- 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
Methods to know how to use: (You will not be asked to memorize these method names. If you need the method name, the test will give you possible methods to use)
Decimal Format: Library: import java.text.DecimalFormat;
Create instance: DecimalFormat percentage2 = new DecimalFormat(0.00%);
Call methods: System.out.println(percentage2.format(.308)); // will print 30.80
0 = required position
# = show it if you have it
% = make it a % (mult by 100)
E = E notation (scientific)String: stringvar.charAt(3) // will be a character
stringvar.length() // will be an integer
stringvar.equals(somethingOtherString) // will be either true or false (a boolean)
stringvar.toUpperCase() // will be a String that is all uppercase
stringvar.toLowerCase() // will be a String that is all lowercase
stringvar.subString(2) // will be a String starting in position 2 to the end
stringvar.subString(2, 5) // will be a String starting in position 2 and ending in position 4
ex: newString = myOriginalString.toLowerCase();
Picture Creation basics
How to approach a problem
Reminder: code, then trace