mattkimura

Java Classes & Methods

May 19th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. Math Class:
  2. API Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html
  3. w3schools HowTo: https://www.w3schools.com/java/java_math.asp
  4.  
  5. - Math.max(a, b); //Compares two values, returns bigger value
  6. - Math.round(); //Rounds a double to the nearest int value. Use Math.floor(); or Math.ceil(); to round to the lowest or highest int
  7. - Math.abs(); //Enter a variable name or expression to get a non-negative value
  8. - Math.sqrt();
  9. - Math.PI; //Gets the value of pi. Can be used in calculations (2 * Math.PI * r;)
  10. - Math.pow(); //Takes two parameters as the base and exponent ( Math.pow(2, 8); ) 2^8
  11.  
  12.  
  13.  
  14. String Class:
  15. API Documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
  16. w3schools HowTo: https://www.w3schools.com/java/java_strings.asp
  17.  
  18. - str.length(); //Returns int value based on the size of the string
  19. - str.substring(0, 3); //0 is the starting index, 3 is the ending index to search, but not inclusive. "Matt" -> "Mat"
  20. - str.charAt(); //Enter integer of the string's index number and returns a char
  21. - str.toUpperCase(); //Enter string variable name to make upper case
  22. - str.toLowerCase();
  23. - str.isUpperCase();
  24. - str.indexOf('a'); //returns an index number for where the argument appears in the string. Can be char or string
  25. - str.startsWith(""); //Checks if the string starts with a certain word or character
  26. - 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
  27. - str1.equals(str2); //Checks if one string has the same characters as another string
  28.  
  29.  
  30.  
  31. ArrayList Class:
  32. API Documentation: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
  33. w3schools HowTo: https://www.w3schools.com/java/java_arraylist.asp
  34.  
  35. ArrayList<Type> Name = new ArrayList<Type>();
  36.  
  37. - var.add() //adds a value to the array list
  38. - var.remove() //removes a value from the array list
  39. - var.get(i) //returns the value in the specified index number
  40. - var.set(i, x) //modified a value in the specified index number with another value
  41. - var.size() //returns the size of the array list
  42. - var.clear() //clears the entire array list
  43.  
  44.  
  45.  
  46. Collections Class:
  47. API Documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
  48.  
  49. - Collections.sort() //sorts the elements in order
  50. - Collections.frequency(arrVar, "") //returns the amount of times an element appears in an array or arraylist
  51.  
  52.  
  53. StringBuilder Class:
  54. API Documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
  55. Note: Requires object creation: StringBuilder myObj = new StringBuilder(String);
  56.  
  57. - myObj.reverse() //Reverses the order of the characters in the string
  58.  
  59.  
  60.  
  61. Input/Output Methods:
  62. - System.out.println(); //Add a + in the parameter to add another string of text or variable ("Hello" + X)
  63. - System.out.printf("%.1f", varName); //Prints out a decimal to 1 decimal place. Use %d for int, %f for float
  64. - 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) !
  65. - scanner.hasNextInt() //Checks if the input has an integer
  66. - 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;
  67.  
  68.  
  69.  
  70. Coding Tricks:
  71. - Validating user input: //The while loop checks if the input is an integer, repeats if not.
  72. while( !(hasNextInt()) ) {
  73. System.out.println("Enter a number");
  74. scanner.next();
  75. }
  76. scanner.nextInt();
Advertisement
Add Comment
Please, Sign In to add comment