Skill-Building Exercises


Summary

Problem abstraction requires lots of programmer creativity and represents the Art in the Art of Programming. Your guiding mantra during problem abstraction is to Amplify the Essential — Eliminate the Irrelevant. Problem abstraction is performed in the analysis and design phase of the development cycle. The abstractions you choose to model a particular problem will directly influence a program’s design.

The end result of problem abstraction is the identification and creation of one or more new data types. The data types derived through problem abstraction are referred to as abstract data types (ADTs) or user-defined data types. User-defined data types are implemented as Java classes. These classes will interact with each other in some capacity to implement the complete problem solution.

A Unified Modeling Language (UML) class diagram is used to show the static relationship between classes that participate in a software design. The class diagram is used to by programmers to express and clarify design concepts to themselves, to other programmers, to management, and to clients.

A class is represented in UML by a rectangle. The rectangle can have three compartments. The uppermost compartment contains the class name, the middle compartment contains fields, and the bottom compartment contains the methods.

Generalization and specialization is indicated with lines tipped with hollow arrows. The arrow points from the specialized class to the generalized class. The generalized class is the base class and the specialized class is the derived or subclass. Generalizations specify “is a” relationships between base and subclasses.

Dependencies are indicated by dashed arrows pointing to the class being depended upon. Dependencies are one way to indicate “uses” relationships between classes.

Java classes have four types of members: 1) static fields, 2) non-static fields, 3) static methods, and 4) non-static methods. Static fields are shared between all class objects whereas each object has its very own copy of instance fields. Alternative names for static are class and class-wide. An alternative name for non-static is instance. Static methods can only directly manipulate static fields. Non-static methods can manipulate both static and non-static fields.

There are three access modifiers: public, private, and protected. All three access modifiers are used to control horizontal access. If no access is specified then default or package access is implied.

Horizontal access is the term used to describe how a client object uses the services of a server object. Public fields and methods published by a class are collectively referred to as its interface. Private fields and methods are said to be encapsulated within the class. Client code becomes dependent upon a class’s interface. Changes to a class’s interface can potentially break any client code that depends upon its interface.

Methods are named modules of executable program functionality. Methods contain program statements that, when grouped together, represent a basic level of code reuse. You access the functionality of a method by calling the method using its name in a program.

Methods should be well-named and maximally cohesive. A well-named, maximally-cohesive method will pull no surprises.

Method definitions have structure. Their behavior can be optionally modified with method modifiers, they can optionally specify a return result type or void, and they can have an optional parameter list.

Methods have a distinguishing characteristic known as a method signature. Methods with different names and parameter lists are said to have different signatures. Methods with different names and the same parameter list also have different signatures. Methods with the same name and different parameter lists have different signatures as well and are said to be overloaded (because they share the same name). Methods cannot have the same name and identical parameter lists. This will cause a compiler error.

Constructor methods are used to set up or build an object when it’s created in memory. Java will provide a default constructor but it may or may not provide the level of functionality required.

Abstract data types can be incrementally built and tested by iteratively applying the steps of the development cycle. Start with the class definition shell and then add fields and methods as required to fulfill the class’s design objectives.

Class functionality can be testing with the help of a test driver. A test driver is a small program that’s used to exercise the functionality of another program.

The Java assertion mechanism can be used to thoroughly test and debug code. Assertions are usually disabled upon completion of the testing phase.

Argument values are passed to methods by value. This is also referred to as pass by copy. The method parameters contain a copy of the argument values and any change to the parameter values only affect the copies, not the actual arguments. Any change to an object pointed to by a method parameter will remain in effect when the method call completes.

Methods can contain local variables whose scope is the body code block or the code block in which they are declared. Local variables are available for use after the point of their declaration up to the end of the code block. Method parameters are local variables that are available to the entire method body.

Anywhere an object of <type> is required a method that returns that <type> can be used in its place.

Static initializers can be used to perform complex initialization of static class fields. There can be more than one static initializer in a class and they can appear anywhere a field or method declaration can appear.

Data encapsulation can be unwittingly violated through the improper use of getter and setter methods. Let good class design be your guide in choosing exactly what, if any, internal pieces of a class to expose.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net