CSE/IT 213 - Review: New Mexico Tech
CSE/IT 213 - Review: New Mexico Tech
Procedural Programming
Attributes (data) and behaviors (methods = functions)
are normally separated
Object behaviors
Getters and setters support data hiding
Objects should not directly manipulate data within another object (encapsulation)
Provide controlled access to an objects data
e.g. private String name;
The
The
The
The
No class No object
Classes are blueprints, or templates, for making objects
Objects are instances of classes
Each object has its own copy of attributes and methods
Encapsulation
The internal representation, the attributes and behaviors, of an object is hidden from all other objects. Only
the details pertinent to its use (i.e., interfaces) are accessible to other objects.
Class interfaces define the fundamental means of communication between objects
Each class design specifies interfaces for instantiation
(e.g., constructors) and operation (e.g., public methods) of objects
Any behavior of an object is invoked by a message sent
using a provided interface
7
Inheritance
The abstraction of common attributes and behaviors
that allow the creation of a brand new class that will
inherit the attributes and methods of another class.
Common attributes and behaviors can be moved up to
a parent class (or superclass)
Superclass - contains all attributes and behaviors common to classes that inherit from it. Also known as a
parent class or generalization
Subclass - a specialization of the superclass with unique
attributes and behaviors. Inherits all attributes and behaviors from the superclass
8
Is-a relationships
A subclass is-a superclass.
Polymorphism
Each subclass may respond differently to the same message
Composition
We can create new objects by putting together other
objects
has-a relationship
9
UML
A graphical way to model class relationships
+ public methods
- private methods
10
Java
High level language
Java compiler produces bytecode - a machine language
for a virtual machine.
Why? portability. Bytecode should be able to run on
any machine without being recompiled.
Java Virtual Machine (JVM)
Interpreter that translates and executes the Java bytecode on an actual computer
11
Java Language
Know C know a a lot of Java
Flow-control, looping, arrays, variable declaration, data
types, operators similiar to C.
Use dot method to access the methods of an object (object.method(parameters)) similar to structure
member notation in C.
No pointers. You create references to objects with
the new keyword, but no memory management. The
garbage collector in java takes care of that.
12
13
Classpath
The class path tells the Java SDK tools and applications where to find third-party and user-defined classes.
14
Numerical Data
Same basic data types as C - chars, ints, floats and
operators
Can do math using the Math class (square roots, powers,
trig, logs, etc).
Math is declared final.
allowed.
15
Strings
No character arrays use a class called String
No need to worry about null terminator
There is a char type like C.
16
17
Console I/O
Use Scanner class to read in things from stdin.
To write to stdout use System.out.println("....");
18
Equality of Objects
The == operator tests for object identity, not equality.
That is, do the objects point to the same object.
To test if two objects are equal use equals method.
Enhanced for loop
iterate over collections or arrays
for (varDeclartion : iterable)
statements;
Arrays
Slightly different syntax, put the subscript braces after
the data type and before the variable name.
String[] s not String s[]
20
Object Construction
a way to initialize objects (init instance variables)
If no defined constructors in your class, the default constructor for the parent class is used. A default constructor is one with no parameters. Even when you dont
extend a class explicitly, implicitly all classes inherit the
Object class, the mother-of-all classes.
There is an important difference between fields (instance variables) and local variables.
You must always explicitly initialize local variables in a
method.
21
But if you dont initialize a field in a class, it is automatically initialized to a default (0, false, or null) depending
on its type.
Once you write a constructor than there is no longer
a default constructor provided for you. You must write
your own.
Can overload constructors
Overloading occurs if several methods have the same
name but different parameters.
Parameter Names
Coming up with parameter names for constructor and
methods can be difficult/tedious. Single character parameters are not the best.
Parameters variables shadow instance variables with the
same name. That is, for the scope of the method the
local variable hides the name of the instance variable.
this is the implicit parameter. this refers to the current
object.
this is used to give parameter names the same name
as instance variables
22
23
25
Abstract Classes
use the abstract keyword
Give method names but no body, i.e. no implementation of the method.
This is called an interface.
Interfaces are implemented by classes.
If a class implements an interface it has to create the
bodies of the methods.
Also, if you want to use interfaces in a polymorphic
manner, i.e. use them as return and parameter types,
can use objects of classes that implement the interface.
No requirement that they share any ancestry.
26
Sorting objects
static <T extends Comparable<? super T>>
void sort(List<T> list)
Sort is looking for objects of Type Comparable or its
subclass. (Just ignore <? super T> for the time being.
Comparable is an interface, how does it extend <T>? It
doesnt. In this context extends means implements and
<T extends Comparable> translates into T must be of a
type that implements Comparable.
The interface Comparable has only one method to implement.
28
int compareTo(T o)
Compares this object with the specified object for order.
Compares this object with the specified object for order.
Returns a negative integer, zero, or a positive integer as
this object is less than, equal to, or greater than the
specified object.
To sort objects have to add Comparable interface to the
class.
29
Exceptions
try-catch blocks
exceptions versus errors
30
File I/O
Wrapper classes File provides access to file Streams
operate at byte level Use other classes to read and write
data DataInputStream for binary data;
textttBufferedInputStream for text processing.
Can also write objects to a file called serialization.
31