Java Written Exam 3 Flashcards
checked exceptions
must be caught or declared
unchecked exceptons
don’t have to be caught or declared
class invarient
is an assertion about an object’s state that is true throughout the lifetime of the object
eg no bankAccount object’s balance can be negative
precondition
Something you assume to be true when the method is called
postcondition
something you promise to be true when your method calls
how are pre/post conditions documented?
as comments
common exceptions
illegalArgumentException, ArithetricException, IllegalStateException, YourCustomeExceptions
(and FileNotFound exceptions I would add)
junit test for exceptions
assertThrows, assertDoesNotThrow
syntax junit exceptions
”() -> <expression>" , This is a "lambda" - an anonymous, inline function definition</expression>
token based processing
practice of processing input token by token (ie one word at a time or one number at a time)
token
unit of user input, separated by a delimited
scanner token based methods
next(), nextInt(), nextFloat()
line based scanner methods
nextLine()
printing out to file syntax
PrintStream <name> = new PrintStream (new File ("results.txt");</name>
printStream details
if the given file does not exist, it is created.
If the given file already exists it is overwritten
Output appears to file, not console
Do not open the same file for both reading (Scanner) and writing (PrintStream) at the same time
printStream exceptions
You might not have permission to write to the directory
You might be locked because another file is using it
need a throws clause
generic
Generic class is a type in Java that is written to be a placeholder for another type
<T>
T
</T>