Sie sind auf Seite 1von 4

Mod (%) gets remainder when you divide two numbers, can be

used to see if number is even


Num++ vs ++num
o Num++ executes line and then increments num by 1
o ++num increments num by 1 and then executes the line
Strings
not a primitive type
indicated with double quotes
length() returns length of a string
indexOf finds first index of a character in a string
o all indices start at 0
string a b c d e f
index - 0 1 2 3 4 5
charAt returns the character at an index in string
substring takes the substring of a string
o first index is inclusive, second index is exclusive
o if you give one number, it will go from that number to end
of string
contains checks if string contains another string
o prints true or false
toLowerCase() makes string lower case
toUpperCase() makes string upper case
convert string to int, double
o Integer.parseInt()
o Double.parseDouble()
NEVER use == to check it two strings are equal- you have to
use .equals() or .equalsIgnoreCase()
Scanner
Used to iterate over text from System.in, string, file
Important methods
o Scan2.next() gets string to next white space
o Scan2.nextLine() gets everything up to new line (\n)
o nextInt() gets the next int
o nextDouble() gets the next double
o hasNext() checks if there is another string
o hasNextLine() checks if there is another line
o there is no nextChar(), you have to do
Math Class
Math.pow(base, exponent)
Math.round(decimal) round normally to whole number
Math.ceil(decimal) round up to whole number
Math.floor(decimal) round down to whole number

Printf

Math.sqrt
All return doubles

Used to format printing


%d for integer
%f for floating points (floats, double)
%s for strings
%.2f 2 digits after decimal
%5f width of number is at least 5 spaces
%08f pad number with 8 leading zeros
o double m = 5.532
o String s= hdsa;
o Printf(%.2f\n %s, m, s);
If statements
Used to evaluate boolean expressions and execute a block of
code if the expression is true
If there are no curly braces around an if statement, then it will
execute only one statement below it
o Just because indented doesnt mean it goes in if statement
if there are no braces
Else relates to closest if
Boolean algebra examples
Memorize truth tables and orders of operation
Swtich statements
They are like if statements but multiple cases can be executed
If there is not break, the program will continue to execute
Default is used when no other case was selected
For loops
Useful when code needs to be repeated and you know how many
times it needs to be repeated
For (int i=0 (initialization), i<5(condition), i++(afterthought))
o Initialization
o Condition
If true, execute body of code
Else, end
o After thought
o Go to step 2
While loops and do while loops
While loops are useful when you dont know how many times to
execute the loop, can be based on user input
Do while loops are useful when you dont know how many times
to execute the loop, but you know you have to execute it at least
once

o Put semicolon after condition of while

Random
Need to import java.util.Random
Generates a random integer or double
For nextInt(), the # in () is exclusive
o Random rand = new Random();
o Int a = rand.nextInt(4) + 4; [range is 4,7]
Read and Write Files
You have to know how to read and write from file
o PrinterWriter toFile = newPrintWriter();
o Scanner fromFile = new Scanner(new
FileReader(filename.txt))
o toFIle.println()
o System.out.println(fromFile.nextLine())
o toFile.close()
you need this on main method
o public static void main (String[] args) throws IOException
import java.io.*;
Methods
if you are calling the method from main, the method has to be
static
a method has to have a return type
o the return types are void, Boolean, int, String, char, double
float
public static double myMethod(int a, double b)
double- return type
myMethod method name
(int a, double b)- parameters
Recursion
when a method calls itself
have to have base cases in order to stop the method from calling
itself
use return; to exit
REVIEW PRACTICE NOTES
if int is divided by int it will come out with if int division even it is
being put into a double
o n1 = 17;
o d1 = n1/4
o d1 prints as 4.0
string.length starts counting the first character as 1 and includes
spaces

import java.util.Scanner
Scanner keyboard = new Scanner (System.in);
Scanner inputFile = new Scanner (new FileReader(input.txt));
PrintWriter outputFile = new PrintWriter (new
FileWriter(output.txt));
When declaring string as variable capitalize s
Cant start variable name with number

Das könnte Ihnen auch gefallen