Course material
|
Object-oriented design goals
abstraction: ignore details to focus attention on higher level of problem
object-focused: identify the objects in the real-world system & model each
with a class
modularization: design each class/method to be well-defined & self-contained,
so that they can be built/examined separately & reused easily
Java classes
using classes
loading a BlueJ project, creating an object, inspecting an object's state
calling a method, parameters, return values
defining/examining classes
fields: maintain the state of an object
access using this. prefix
can be static (shared by class) and/or final (unchangeable)
constructor(s): initialize the fields for a newly created object
methods: implement the behaviors of the method
accessor method returns a field value; mutator method changes field(s)
can define local variables for temporary values
must specify return type (void if no return)
class/constructor/methods are (usually) public; fields are (usually) private
comments (/** ... */ or // ..) are for documentation
Java statements
assignment statements
data types (String, int, double, char, boolean)
variables, expressions (+, -, *, *, %)
arithmetic assignments: +=, -=, *=, /=, ++, --
primitive types vs. object types (using new)
variable scope (field = entire class; parameter/local = that method only)
output statements
System.out.print, System.out.println
return statements
conditional statements
1-way: if; 2-way: if-else; multi-way: cascading if-else
comparison operators: ==, !=, <, >, <=, >= (danger: = vs. ==)
logical connectives: && (AND), || (OR), ! (NOT)
repetition statements
while: conditional repetition
for: counter-driven repetition (shorthand version of while loop)
for-each: for traversing a list (shorthand version of for loop)
method calls
internal call: this.METHOD(PARAMETERS)
external call: OBJECT.METHOD(PARAMETERS)
Java library classes
String
implicit call to new when assigning, immutable
methods: length, charAt, contains, indexOf, substring, toUpperCase,
toLowerCase, equals, compareTo, ...
Character
static methods: toUpperCase, toLowerCase, isLetter, isUpperCase, isLowerCase, ...
Integer
static method: parseInt
Random
methods: nextInt, nextDouble, ...
ArrayList
generic, must specify the type of object stored
wrapper classes (Integer, Double, ...) allow for storing primitives
methods: add, get, size, remove, contains, set, indexOf, toString, ...
traversal using for loop vs. for-each loop
Input/Output
Scanner class
used to read from keyboard (System.in) or a file (File object)
methods: hasNext, next, hasNextLine, nextLine, hasNextInt, nextInt, close, ...
FileWriter class
used to write to a file
methods: write, close
|