Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Math Class:
- API Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html
- w3schools HowTo: https://www.w3schools.com/java/java_math.asp
- - Math.max(a, b); //Compares two values, returns bigger value
- - Math.round(); //Rounds a double to the nearest int value. Use Math.floor(); or Math.ceil(); to round to the lowest or highest int
- - Math.abs(); //Enter a variable name or expression to get a non-negative value
- - Math.sqrt();
- - Math.PI; //Gets the value of pi. Can be used in calculations (2 * Math.PI * r;)
- - Math.pow(); //Takes two parameters as the base and exponent ( Math.pow(2, 8); ) 2^8
- String Class:
- API Documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- w3schools HowTo: https://www.w3schools.com/java/java_strings.asp
- - str.length(); //Returns int value based on the size of the string
- - str.substring(0, 3); //0 is the starting index, 3 is the ending index to search, but not inclusive. "Matt" -> "Mat"
- - str.charAt(); //Enter integer of the string's index number and returns a char
- - str.toUpperCase(); //Enter string variable name to make upper case
- - str.toLowerCase();
- - str.isUpperCase();
- - str.indexOf('a'); //returns an index number for where the argument appears in the string. Can be char or string
- - str.startsWith(""); //Checks if the string starts with a certain word or character
- - str.replace("", "") //replaces any part of a string with another string literal. Can be used to remove parts of a string. Left: Replace what, Right: Replace with
- - str1.equals(str2); //Checks if one string has the same characters as another string
- ArrayList Class:
- API Documentation: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- w3schools HowTo: https://www.w3schools.com/java/java_arraylist.asp
- ArrayList<Type> Name = new ArrayList<Type>();
- - var.add() //adds a value to the array list
- - var.remove() //removes a value from the array list
- - var.get(i) //returns the value in the specified index number
- - var.set(i, x) //modified a value in the specified index number with another value
- - var.size() //returns the size of the array list
- - var.clear() //clears the entire array list
- Collections Class:
- API Documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- - Collections.sort() //sorts the elements in order
- - Collections.frequency(arrVar, "") //returns the amount of times an element appears in an array or arraylist
- StringBuilder Class:
- API Documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
- Note: Requires object creation: StringBuilder myObj = new StringBuilder(String);
- - myObj.reverse() //Reverses the order of the characters in the string
- Input/Output Methods:
- - System.out.println(); //Add a + in the parameter to add another string of text or variable ("Hello" + X)
- - System.out.printf("%.1f", varName); //Prints out a decimal to 1 decimal place. Use %d for int, %f for float
- - Scanner scanner = new Scanner(System.in); //Creates a scanner object. To use it, assign a variable of any type to scanner.nextLine(); / scanner.nextInt(); / scanner.nextDouble(); NOTE: Use scanner.nextLine(); after each integer input to clear the empty new line (\n) !
- - scanner.hasNextInt() //Checks if the input has an integer
- - PrintWriter pw = new PrintWriter("output.txt"); //An object to create a text file. Use pw.println(""); to write to the text file, then pw.close(); to close it. Requires java.io.PrintWriter;
- Coding Tricks:
- - Validating user input: //The while loop checks if the input is an integer, repeats if not.
- while( !(hasNextInt()) ) {
- System.out.println("Enter a number");
- scanner.next();
- }
- scanner.nextInt();
Advertisement
Add Comment
Please, Sign In to add comment