Csci final

  1. How to create file objects?
    Import java.util.io; File file = new File(“TestFileClass.java”);
  2. How to read data from a file?
    • Create file object with RELATIVE path (“src/a.txt”); not (“/home/me/src/a.txt”); ...
    • Then import Scanner input = new Scanner(file); ...
    • While(input.hasNext()) { String firstName= input.next(); }
  3. How to close a file after you’re finished
    scannerName.close(); ... input.close();
  4. How to write data to a file?
    • After creating file object,
    • PrintWriter output = new PrintWriter(file); ....
    • Then... output.print(“John T Smith “); ...
    • Don’t forget to close writing with output.close();
  5. Benefits of exception handling?
    Allows program to continue past runtime errors. Must import java.util.*;
  6. How is a try catch block used
    • It surrounds code that can potentially throw an exception,
    • try { // code like num1/num2}
    • catch (ArithmeticException ex)
    • { sysout(“error canhot be divided by zero”);
    • sysout(input.next()); // clear data}
    • ALL EXCEPTION HANDLING MUST HAVE java.util.*;
  7. Trying to put a double in an int value
    InputMismatchException
  8. Three major exception types
    • System errors
    • Exceptions
    • Runtime errors
  9. System errors
    Can’t do anything anout them
  10. Exceptions
    • Include runtime exceptions, class not found exceptions, etc...
    • Can all be caught and handled
  11. Runtime exceptions
    • Subset of exceptions,
    • Result of bad casting, accessing out of bounds array, numeric errors,
    • ArithmeticException, NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, many more.
  12. NullPointerException
    Thrown if you try to access an object through a reference variable before an object is assigned to it
  13. Major pieces of exception handling
    Declare exception, throw exception, catch exception
  14. Catching general exceptions
    • Use on top of other possible exceptions,
    • catch (Exception ex) {
    • handle; }
  15. How does finally block work in try catch? Difference between catch?
    Always executed whether exceptions are thrown or not, where catch is only executed if exceptions are thrown- finally will still execute
  16. Hi to throw and catch an ArithmeticException without try catch?
    • Use if statement...
    • if (num2 != 0) {
    • ans = num1/num2; } else {
    • sysout(“cannot divide by zero”); }
  17. Throw catch a null pointer exception without try catch
    • if (rVar != null)
    • sysout(rVar);
    • else
    • sysout(“rVar is null”);
  18. Why use a do while
    When the program needs to execute at least once before checking continuation condition
  19. Why use while loop
    When number of repitions are unknown, ie. until a zero is entered
  20. Why use a for loop
    When the number of repetitions are known
  21. How does a nested loop execute
    Every iteration of the outer loop executes all iterations of the inner loop
  22. What does break and continue mean
    • Break- breaks out of loop,
    • Continue- skips rest of current iteration and continues loop
  23. How to avoid using break
    Include value of Boolean condition in the continuation condition of the loop
  24. How to avoid using continue
    Create an if statement that addresses the instance
  25. Purpose of using methods
    To create actions that are reusable
  26. String variables can hold what type of values
    Char, Unicode
  27. Why use built in math functions
    • More accurate (Math.PI)
    • Truly random numbers (Math.rand())
    • Symbols not on keyboard (Math.sqrt())
  28. How to get the pattern of 5 asterisks, 4 asteriks... all the way to one
    • for (int i = 5; i > 0; i- -) {
    • for (int j = 1; j <= i; j++) {
    • sysout(“*”);
    • }
    • system.out.println();
    • }
Author
timmymorin
ID
352571
Card Set
Csci final
Description
Updated