Professional Documents
Culture Documents
Comp251 - 01 OL JavaProgrammingBasics
Comp251 - 01 OL JavaProgrammingBasics
Comp251 - 01 OL JavaProgrammingBasics
Java Resources
Java API Specification http://download.oracle.com/javase/6/docs/api/ OReilly ONJava http://www.onjava.com/ Java Tutorial http://download.oracle.com/javase/tutorial/ Java Generics Sun Java Generics Tutorial Angelika Langer Generics FAQs UML Tutorial http://www128.ibm.com/developerworks/rational/library/content/ RationalEdge/sep04/bell/
Ch 01 Objectives
Java Refresher Java Generics Simple Problem Solving Methodology Designing for Larger OO Problems Unified Modeling Language (UML) Static Class Diagrams Design Patterns Readability & Style Testing and Debugging Introduce a Simple Method of Versioning Your Java Code
A Java Refresher
Open
MyMenu.java MyUtil.java
An Immutable Class
Open XYCoord.java What is an immutable class? When would you use one? What commonly used Java class is also immutable?
Java Generics
A generic type is a type with formal type parameters. A parameterized type is an instantiation of a generic type with actual type arguments.
Generics Examples
A Generic Type
interface Collection<E> { public void add (E x); public Iterator<E> iterator(); }
A Parameterized Type
List<String> list = new LinkedList<String>();
public Pair(X a, Y b) { this.a = a; this.b = b; } public X getFirst() { return a; } public Y getSecond() { return b; } public void setFirst(X a) { this.a = a; } public void setSecond(Y b) { this.b = b; } }
Practice
5.
State the problem clearly. Describe the input and output. Work it out by hand. Develop an algorithm. Test your solution.
1 Problem Statement
Write a clear and concise statement so as to avoid any misunderstanding EXAMPLE:
You need to determine what is needed to produce (calculate) the desired output. Usually its easiest to carefully examine the desired output and think backwards about what you will need to make that happen. Example:
We want to determine the distance between two points on a plane Therefore, we need to know the location of each of those points.
3 Hand Example
Now that we have a clear idea of what the input and the output will be we manually calculate the output using the pre-determined input. We may discover that other input is needed an so go back and revise the previous step. The distance between two points is the hypotenuse of a right triangle (see next slide). We can use the Pythagorean theorem to compute the required distance.
WARNING: you must NOT proceed to the next step until successful here.
(1, 3)
side1
(6, 1) side2
x
4 Develop an Algorithm
a)
b)
c)
Write out the steps you took in the hand example OPTIONAL: Formalize the steps using pseudocode Translate the pseudocode steps into a programming language.
3.
4.
Get values for the points Calculate the length of each side of the triangle Calculate the distance between the two points by squaring each side then finding the square root of the square of each side. Print the result (you didnt do this in the hand example but the need to do so is inferred)
4b - Pseudocode
OPTIONAL: not needed unless the programming language has not been determined. Pseudocode is covered in other courses including Comp125
(coded in C++)
// Exit program (pause so the program output // window doesn't close system("PAUSE"); return EXIT_SUCCESS;
}
5 - Testing
This is the same as the hand example. If it wasnt, review both hand example and code to determine what went wrong and correct.
More Complex Problem 1. State the problem clearly. 2. Analysis & Design
3. 4.
Describe input, computational needs, output Discover entities, their behaviour & relationships Work it out by hand. Draw UML, write the class specifications
An OO Analysis/Design Methodology
1. 2. 3. 4. 5. 6. 7. 8. 9.
Write a problem statement AND Restate the problem as a high-level (top-level) algorithm Describe inputs and outputs Identify the objects (look for nouns) Draw the class diagrams Add class associations Refine the (high-level) algorithm; primitive statements become method calls (add to the class diagrams) Add attributes as needed to hold state information Write the Class Specification (s)* NOTE:
All but steps 1&2 are iterative and/or parallel although you may even find that you will edit 1&2 as you complete the others You are NOT writing the code that goes in each method, rather you are clearly understand what each method will do (see class specification notes below) Complete ALL of this BEFORE you begin coding
Class Specification
Purpose statement Invariant statement Attributes Methods (document the task, precondition, modifies, post-condition)
While you are free to use abbreviated documentation for labs you must use the templates on the following slides for formal assignments.
Class Specification
Template for Class Documentation
(Put the following at the beginning of each .java file, remove bracketed text and add your own) /** * AUTHOR: * * CONTACT INFORMATION: * * CREATION DATE: * * VERSION HISTORY: (date and description of each update) * * PROBLEM STATEMENT: (concisely, rephrase the problem) * * PROGRAM DESCRIPTION: (describe your program here referring to * your class diagram as needed) * * CLASS PURPOSE: (a brief desc of the classs purpose) * * CLASS INVARIANT: (what will always be true about this class) */
Class Specification
Template for Method Documentation
(Put the following immediately BEFORE each method, including constructors, remove the bracketed text and add your own) /** * TASK: * * PRECONDITION: * * * MODIFIES: * * * * * POSTCONDITION: * * */
(describe what it does this will help you to complete the next three) (describe what must be true for this method to succeed, includes a description of the formal parameters) (list what will be modified that is external to the method; generally refers to this objects state information but will also include any objects in the formal parameter list that have been modified) (describe what will be true after the method finished, includes a description of the return value)
Responsibilities entities/actors each having different responsibilities Please make sure that Independence classes & methods youre be comfortable must very independent of others with UML and Class High Cohesion a unit of code (class or method) We will Specification. does just one thing (& does it well) use these extensively. Low Coupling a unit of code (esp a class) is not dependant on other units of code Behaviors each action must be well-understood (again, cohesion) Use UML (Unified Modeling Language) Static Class Diagrams to document
AComparator is an inner class of DataItem interface List DataItem -idNumber : int -description : string AComparator -name : string +DataItem() +compare(in dItem1 : DataItem, in dItem2 : DataItem) : int +getID() : int 0..* +getName() : string +getDescription() : string +compareTo(in dataItem2 : DataItem) : int +other methods . . .() 1 DataController -inventory : List 1 ArrayList 1
UML Tool
Get hooked up with MSDN and get MS VISIO. Here is an Open Source UML modeling tool thats somewhat easier to get going on than VISIO.
Code Design
Fundamental high level patterns. Functional Pattern Cohesion/Coupling Creational object creation Singleton Factory Method Structural ease design by identifying a simple way to realize relationships between entities. Adaptor Decorator Behavioural identify common communication patterns between objects. Iterator Observer Concurrency multithreading patterns read more here
Pseudo-Code
Pseudo-code can be used to describe what methods do once they are generically understood
Coding
Now we can start writing the code BUT REMEMBER, NOT until all the thought and documentation that goes into designing the solution is complete Why?
Review
Comp251_Commenting.docx (same as slide 23 and 24) Text: end of section 1.9.3 (pg 52 53)
Remember that the standards set out in the above file are mandatory for assignment submissions.
Testing
If possible each statement should be tested. Is this always practical? Special cases of input
Boundary testing
Debugging
Print statements Debugger
A Major revision
Followed by further refining and further major revisions. (Not Shown)