Uploaded by Sean Pautrat

cs2110 notes

advertisement
Syntax:
Class Counter{
Int counts;
<- field (int is the static type) **by default an integer variable is set to 0
Void reset(){
This.counts = 0
}
Int getCount(){
Return this.counts;
}
}
Statically typed language:
Type: set of values
Invariant: statement that should always be true
Class invariant : relationship between fields for the class
Loop invariant: relationship between local variables
Static type variable enforces type invariants inherently
I.e: int max is an integer, the type enforces that always from declaration
void -methodheader-(){ ***void for the return type means it returns nothing
}
Constructors:
public class Counter{
Counter(){
Counts = 0;
}
}
A -final- variable can only be assigned once
Final int num = 0;
Immutable type: cannot change its state
String x; int x; …
Mutable type: can change their state
Counter *other classes
**Bug: anything that violates a specification, still can compile
Encapsualtion:
Public: anyone can access the methods/fields
Private: only the class implementation can access the methods/fields
*if class is public then all fields should be private
-remember private instance variables from ap
*helper methods should be private
Linked Data Structures:
Node<T>
-Data: T
-next: Node<T>
Download