Transcript PPT

Java Primer 1
OOP
UML
Java Structure
Reading the API
Object-Oriented Programming

Objects are “black boxes” that send and
receive messages
–

Hierarchy of functions
Why?
–
–
Modularity
Objects can be created for similar items

Easy to handle lots of “items” when broken into functions
Units of OOP

Class: Defines an object
–

Provides a definition, data structures and methods
for an object
Method: Actual functions (or “work”) a class
can do
–
–
Generally, variables and data inside an object can’t
be directly accessed from an outer class
Methods are used manipulate data inside a class
Warehouse Example

Easy to one thing: e.x. Books
–

Enter call number, author, title
What if we have books AND magazines
–
–
Magazines have no call number, multiple authors
and a title with many issues attached
SOLUTION: have an object for each and have
another code piece (such as a class for user I/O)
call them
Classes

Inheritance (an include)
import java.io.*

Definition
public class Example {
int a;
public Example (int b) {
this.a = b;
}
}
Classes

Methods (Functions; “do work”)
public int doAddition (int a) {
int result;
result = this.a + a;
return result;
}
–
May contain void, int, String, etc. (any data structure
type)
Full Class Example
import java.io.*
public class Example {
int a;
//object definition
public Example (int b) {
this.a = b;
}
//method to add 2 numbers
public int doAddition (int a) {
int result;
result = this.a + a;
return result;
}
}
Java - Hierarchy



Package – Directory with similar classes in it
Class – Framework for an object that can be
created (instantiated)
Sub Class – Class that inherits functions from
the super class (general class one level above
it), but has its own specifics
–
e.x. java.net.Socket.SSLSocket
package
super
sub
class
Java - Hierarchy


Only allowed 1 public class / file (file must have
same name)
Sub classes extend super classes
–

public class ThisClass() extends ThatClass {
Interfaces connect seemingly unconnected
classes by defining a set of methods that they
must use
–
Instead of rewriting classes that don’t have shared
interests to work together, they can both implement
an interface
Classes using other classes
public class program {
public static int a;
public static void main (String[] args) {
a = 10;
Example object1 = new Example(a);
a = object1.doAddition(200);
}
}
Interfaces

Used to guarantee formal structure of your class
– Implemented with class definition
class Example implements InterfaceExample
– Contains a list of methods
interface InterfaceExample {
void doAddition(int a);
void doSubtraction(int b);
void multiply(int c);
void shift(int d);
}
–
Checked at compile time; your class MUST implement all methods
listed in the interface
UML – Unified Modeling Language

Consists of many separate diagram types
–


User, state, class, etc.
We will focus on Class Diagrams for the course
Class Diagrams depict classes (OOP), their
methods, class variables, and their
relationships to other classes.
UML – Class Diagrams

Each Diagram box has:
–
–
–
The class in the Top Level
Class-wide variables in the middle level (if any)
Methods in the lowest level
Registration
- studentNo : String
- numCourses : int
+ addCourse (int, String) : boolean
+ dropCourse (int, String) : boolean
Formal (Borrowed) Example
Formal (Borrowed) Example
Formal (Borrowed) Example
UML - Finish

1 – n (1 to many), or 1…*
–
–



Star can take the place of many 1 is lowest
boundary * is infinite
New UML spec, be specific about what you are
instantiating (1, 1…4, 1…*, etc.)
Triangles denote inheritance (super and sub
classes)
Regular arrows denote instantiation
Dashed lines represent implemented Interfaces
Java Structure

Variables
–
Public/private: private can be used in class only

–
–
Private variables use methods to access them out of class
Lots of structures available through standard Java
packages
Can be changed with provided functions


.toString()
Integer.parseInt(other data type)
Java Structure

Accessing class data/functions
–
Methods and variables use same structure as
packages; e.x.:
object1.doAddition().result
Class
–
Method
Variable
If a variable is public, it can be accessed by the
calling class; e.x.:
Example object1; int a;
a = object1.a;
Java Structure

Sun provides most of the standard (and
advanced) classes you could need
–
We will use java.io.* and java.net.*

These provide Socket functions as well as I/O (display and
keyboard) and much more
Java API Docs

This is incredibly helpful documentation for
understanding unused Java commands and for
finding info on the classes you will be calling
(e.g. Socket, etc.)
http://java.sun.com/j2se/1.5.0/docs/api/index.html