370 likes | 376 Views
Learn the purpose and benefits of UML design representations, with emphasis on use case, class, sequence, and collaboration diagrams. Practice Java coding concepts and explore class structures.
E N D
IST 311 – Object-Oriented Design & Software Steven Haynes IST 311 – Class 4 19 January 2006 shaynes@ist.psu.edu
Mid-Term Deliverables • Use Case diagrams • Derived from scenarios • Class diagram • Conceptual • Sequence diagrams TBD • Need use cases first • User Interface Design • Need use cases first • Due Thursday, March 3rd (Thursday before break)
Design Representations • Why learn the UML? • What is the purpose of design representation languages and techniques?
Design Representations • An aid to design cognition • A common language for communicating about a design (specifications) • A record of the design process and its outcomes (documentation)
UML Diagrams, Part 1. • Use Case diagram
UML Diagrams • Use Case diagram • Identify major services provided by a system to external actors (users and other systems) • Establish the boundaries of the system • Identify common functionality • Identify high-level alternate use scenarios • Capture requirements • Development project planning tasks • Communicate with the customer/user.
Use Cases • Actors • Use Cases • Include (Uses) Use Cases • Extend Use Cases • Annotations • Pre-conditions • Post-conditions • Constraints • Don’t use actor or use case generalization
Guidelines for Use Cases • Actors – specific user roles • Human actors on left • Non-human actors (systems) on right • Use Cases – verb-noun phrase • e.g., Verify Credit Card • Include (uses) link – included use case MUST be completed for the including use case to complete • Extend link – extending use case represents a variant of the extended use case
Guidelines for Use Cases • Use cases model system interactions. • Use case granularity THE big problem • Use annotations (notes) freely to document your assumptions. • Use cases are not data stores • Use cases are not data flow diagrams
UML Diagrams • Class diagram
UML Diagrams • Class diagram • Identify classes • Attributes • Operations • Identify class relationships • Identify packages • Describe a system’s static structure
UML Diagrams • Sequence diagram
UML Diagrams • Sequence diagram • Describe the sequence of steps required to realize a use case or use case scenario, which represent requirements • Describe interactions between objects/classes • Perspective is time oriented
UML Diagrams • Collaboration diagram
UML Diagrams • Collaboration diagram • Same as Sequence diagram except… • Perspective is structural or spatial
Practice Exploratory Learning! • Read Raposa chapters with a Java development environment at hand • Experiment with his or your own code fragments to ensure you understand a given point
Code Comments, Diagram Annotations • Always comment your code • Always annotate diagrams • Most points are lost on assignments and projects because of clarity issues. Comments and annotations help overcome these problems.
Ch. 1 The JVM Bytecodes Classes vs. Objects .java vs. .class files Command-line arguments The main method Ch. 2 Java keywords Java identifiers Primitive data types Declaring, initializing, assigning variables Java Strings Reference vs. primitive data Constants Operators Comments Ch. 3 Java control structures Boolean operators and logic Important from Chapters 1-3
Assumptions • You know what data types are and can select the appropriate type from those available in Java • You know what Java keywords are and can find and use them appropriately • You can declare, initialize, and use Java variables and constants
Assumptions Continued • You know what operators are and can use them • Logical: AND (&&), OR (||), EXCLUSIVE-OR (^) and NOT (!) • Arithmetic: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%) • Increment (e.g., i++, ++i), Decrement (e.g., k--, --k) • Assignment (=, +=, -=, *=, /=, %=) • Relational (==, <, >, <=, >=, !=) • You know how to use the different Java comment types
Classes and Objects • What’s the difference?
Classes and Objects • A class is a blueprint or template for creating objects and defines • An object’s attributes • An object’s behaviors (methods) • An object is an instance of a class • To create objects we instantiate a class
Java Strings • A string is a collection of character (char) data. Strings are important data structures in any programming language • A java.lang.String object is a sequence of characters plus a collection of methods for manipulating strings. • Unlike other Java objects, Strings have certain characteristics in common with the primitive data types. • For example, strings can have literals. A String literal is a sequence of zero or more characters contained in double quotes -- for example, “Socrates” and “” (empty string).
Java Strings • The + symbol is used as a binary concatenation operator for Strings. It has the effect of joining two strings together. • Primitive types are automatically promoted to strings when mixed with concatenation operators. • The number of characters in a string is its length. • The position of a character within a string is called its index. Strings are zero indexed -- the first character is at index 0. • The String.valueOf() methods are class methods that convert primitive types into String objects. • The indexOf() method searches from left to right within a String for either a character or a substring.
Java Strings • Methods for comparing strings: public boolean equals(Object anObject); // Overrides Object.equals() public boolean equalsIgnoreCase(String anotherString) public int compareTo(String anotherString) • Two strings are equal if they have the same letters in the same order: String s1 = "hello"; String s2 = "Hello"; s1.equals(s2) // false s1.equals("hello”); // true • Error: Using = = to compare two strings. For objects, o1 = = o2 means o1 and o2 are identical.
Java Strings • Java Strings cannot be modified. Whenever you assign a new value to a String, Java must create a new String object and discard the old one. • Objects of java.lang.StringBuffer class are strings that can be modified.
Java Strings • A StringTokenizer breaks a string into tokens separated by delimiters, which by default value are the whitespace characters: StringTokenizer sTokenizer = new StringTokenizer("This is an English sentence."); This is an English sentence.
Control Structures • Sequence --- The statements in a program are executed in sequential order unless their flow is interrupted by one of the following control structures. • Invoke or Call a Method --- Invoking a method transfers control temporarily to a named method. Control returns to the point of invocation when the method is completed. • Decision/Selection--- The if, if/else, and switch statements are branching statements that allow choice by forking of the control path into two or more alternatives. • Repetition --- The for, while, and do-while statements are looping statements that allow the program to repeat a sequence of statements.
Control Structures • No matter how large or small a program is, its flow of control can be built as a combination of these four structures. • Note that each structure has one entry and one exit.
Repetition • Repetition structure: a control structure that repeats a statement or a sequence of statements. • A counting loop can be used if you know in advance how many iterations are needed. The for statement is used for counting loops. • A while structure should be used if the loop body may be skipped entirely. The while statement is used. • A do-while structure should be used only if a loop requires one or more iterations. The do-while-statement should be used.
Loop Elements • The loop variable, which is used to specify the loop entry condition, must be initialized to an appropriate value and updated on each iteration. • A loop's bound, which may be a count, a sentinel, or, more generally, a conditional bound, must be correctly specified in the loop-entry expression, and progress toward it must be made in the updater. • An infinite loop may result if either the initializer, loop-entry expression, or updater expression is not correctly specified.
Homework Assignment • This is an individual assignment. • Read Raposa Chapter 4. • Using Eclipse, write a Java class for each of the classes you designed for homework #2. • Write another class (a program), the main class with the main() method, that creates an instance of each of these classes, then calls one method in each to output the method name. • Due at the start of class Tuesday, 1/24. • Hand in hard copies of the object definitions (classes) and of the source code.