View
216
Download
0
Category
Preview:
Citation preview
Final Review
From ArrayLists to Arrays• The ArrayList: used to organize a list of objects
– It is a class in the Java API– the ArrayList class uses an array internally to manage
the list of objects
• Array: used to organize a list of objects that uses special syntax to access elements– Once initialized, cannot change size (does not
grow/shrink automatically)– Can store primitive values or objects
• Why use an array?– Time & space overhead
Copyright © 2012 Pearson Education, Inc.
From ArrayLists to Arrays• The ArrayList:
– Methods: add, remove, get, size
• Array: – No methods. Constant length.– Direct access to the contents of each element.
Both can use the for-each loop, or a for loop using an index over the elements in the collection
Copyright © 2012 Pearson Education, Inc.
Testing and Debugging• Testing
-To check if the method, the class, the program behaves as expected, both for good and bad inputs.
• Testing techniques– Manual Walkthroughs (for loop example – mmm)– Unit Testing:
• testing individual parts of an application• can be single methods, classes, or groups of classes
– Application Testing: test the complete application as a whole
– Regression Testing: test new version of application with passing test cases from previous version
– Print statements (primitive)
Testing and Debugging II• Debugging
-process of isolating the cause of a bug/error/failure & designing a way to fix it
• Debugging steps– Craft a test case (input) that exposes the bug– Refine that input to be the minimal input required to
cause the bug– If the fix is not obvious, manually walk thru the code,
writing down each execution step & keeping track of the values of each variable
– Once you fully understand the code, you should be able to spot the bug & suggest a fix
– Make the fix & rerun the test cases; none should fail now
Recursion• A recursive method is a method that invokes itself
• A recursive method must be structured to handle both the base case and the recursive case
• Each call to the method sets up a new execution environment, with new parameters and local variables
• As with any method call, when the method completes, control returns to the method that invoked it (which may be an earlier invocation of itself)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Object Oriented Design• The creation of software involves four basic
activities:– establishing the requirements
– creating a design
– implementing the code
– testing the implementation
• These activities are not strictly linear – they overlap and interact
• Software requirements specify the tasks that a program must accomplish
– what to do, not how to do it
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Object Oriented Design II• A software design specifies how a program
will accomplish its requirements• A software design specifies how the solution
can be broken down into manageable pieces and what each piece will do
• An object-oriented design determines which classes and objects are needed, and specifies how they will interact
• Low level design details include how individual methods will accomplish their tasks
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Identifying Classes and Objects
• The core activity of object-oriented design is determining the classes and objects that will make up the solution
• The classes may be part of a class library, reused from a previous project, or newly written
• One way to identify potential classes is to identify the objects discussed in the requirements
• Objects are generally nouns, and the services that an object provides are generally verbs
How can we achieve good design?• Cohesion: each code unit (method/class) should be
responsible for 1 and only 1 well-defined taskWhy we want high cohesion:
localize changes to one well-defined placeCohesive code is easier for others to understand, & modifyCohesive code is easier to reuse, thus reducing code duplication
Responsibility-driven design (RDD): store data where it’s predominantly manipulated
• Coupling: the degree to which 2 classes are inter-dependentWhen the implementation of a class changes, other classes should not be affected (when the interface changes, they might be)Why we avoid tight-coupling: more work to modify code; can’t just change 1 class, have to change all the tightly-coupled classesEncapsulation: what a class/method does is visible to the outside (to other classes) by not how it does it
Inheritance• Inheritance allows a software developer to
derive a new class from an existing one• The existing class is called the parent class, or
superclass, or base class• The derived class is called the child class or
subclass• As the name implies, the child inherits
characteristics of the parent• That is, the child class inherits the methods and
data defined by the parent class
Abstract classes
• Static type checking sometimes requires a particular method in the superclass
• But there is no obvious shared implementation for the method according to the subclasses.
• Define the method as abstract
Abstract classes and methods
• Abstract methods have abstract in the signature.
• Abstract methods have no body.• Abstract methods make the class abstract.• Abstract classes cannot be instantiated.• Concrete subclasses complete the
implementation.
Copyright © 2012 Pearson Education, Inc.
Interfaces
• All methods are abstract.• There are no constructors.• All methods are public.• All fields are public, static and final.• Strong separation of functionality from
implementation.
– Though parameter and return types are mandated.
• Clients interact independently of the implementation.
Copyright © 2012 Pearson Education, Inc.
Collections
• Collections can be implemented in many different ways
• Collections should be abstractions• That is, they should hide unneeded details• We want to separate the interface of the structure
from its underlying implementation• This helps manage complexity and makes it
possible to change the implementation without changing the interface
Copyright © 2012 Pearson Education, Inc.
Links
• Recall that an object reference is a variable that stores the address of an object
• A reference also can be called a pointer• Object references can be used to create links
between objects• This allows to create collections using linked or
double linked lists structures
Exceptions• We saw that a moment ago
Recommended