Java Primer 1 OOP UML Java Structure Reading





















- Slides: 21

Java Primer 1 OOP UML Java Structure Reading the API

Object-Oriented Programming l Objects are “black boxes” that send and receive messages – l Hierarchy of functions Why? – – Modularity Objects can be created for similar items l Easy to handle lots of “items” when broken into functions

Units of OOP l Class: Defines an object – l 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 l Easy to one thing: e. x. Books – l 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 l Inheritance (an include) import java. io. * l Definition public class Example { int a; public Example (int b) { this. a = b; } }

Classes l Methods (Functions; “do work”) public int do. Addition (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 do. Addition (int a) { int result; result = this. a + a; return result; } }

Java - Hierarchy l l l 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 l l Only allowed 1 public class / file (file must have same name) Sub classes extend super classes – l public class This. Class() extends That. Class { 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 object 1 = new Example(a); a = object 1. do. Addition(200); } }

Interfaces l Used to guarantee formal structure of your class – Implemented with class definition class Example implements Interface. Example – Contains a list of methods interface Interface. Example { void do. Addition(int a); void do. Subtraction(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 l Consists of many separate diagram types – l l 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 l 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 - student. No : String - num. Courses : int + add. Course (int, String) : boolean + drop. Course (int, String) : boolean

Formal (Borrowed) Example

Formal (Borrowed) Example

Formal (Borrowed) Example

UML - Finish l 1 – n (1 to many), or 1…* – – l l l 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 l Variables – Public/private: private can be used in class only l – – Private variables use methods to access them out of class Lots of structures available through standard Java packages Can be changed with provided functions l l . to. String() Integer. parse. Int(other data type)

Java Structure l Accessing class data/functions – Methods and variables use same structure as packages; e. x. : object 1. do. Addition(). result Class – Method Variable If a variable is public, it can be accessed by the calling class; e. x. : Example object 1; int a; a = object 1. a;

Java Structure l Sun provides most of the standard (and advanced) classes you could need – We will use java. io. * and java. net. * l These provide Socket functions as well as I/O (display and keyboard) and much more

Java API Docs l 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/j 2 se/1. 5. 0/docs/api/index. html