Development Cycle First Iteration


Project Specification

Keeping both the project-approach strategy and development cycle in mind, let’s look now at a typical project specification given in table 3-3.

Table 3-3: Project Specification

IST 149 - Introduction To Java Programming

Project 1

Robot Rat

Objectives:

Demonstrate your ability to utilize the following language features:

 

Primitive, reference, and array data types

Two-dimensional arrays

Program flow-control structures

Class methods

Class attributes

Local method variables

Constructor methods

Console input and output

Java applications

Task:

You are in command of a robot rat! Write a Java application that will allow you to control the rat’s movements around a 20 x 20 grid floor.

The robot rat is equipped with a pen. The pen has two possible positions, up or down. When in the up position, the robot rat can move about the floor without leaving a mark.

If the pen is down the robot rat leaves a mark as it moves through each grid position. Moving the robot rat about the floor with the pen up or down at various locations will result in a pattern written upon the floor.

Hints:

- The robot rat can move in four directions: north, south, east, and west.

- Implement the floor as a two-dimensional array of boolean.

- Java provides the System.out.println() method that makes it easy to write text to the console, but getting text input from the console is not as straightforward. Look to the java.io package's InputStreamReader and BufferedReader classes for help.

At minimum, provide a text-based command menu with the following or similar command choices:

  

1. Pen Up

2. Pen Down

3. Turn Right

4. Turn Left

5. Move Forward

6. Print Floor

7. Exit

When menu choice 6 is selected to print the floor, the result might look something like this, assuming you chose '*’ to represent a marked area of the floor and '0’ to represent an unmarked area. You may use other pattern characters if desired.

.

 

*****000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

In this example the robot rat moved from the upper left-hand corner of the floor five spaces to the east with the pen down.

Analyzing The Project Specification

Now is a good time to step through the project-approach strategy and analyze the robot rat project using each strategy area as a guide starting with the project’s requirements.

Application Requirements Strategy Area

The robot-rat project seems clear enough but it omits a few details. It begins with a set of formally stated project objectives. It then states the task you are to execute, namely, to write a program that lets you control a robot rat. But what, exactly, is a robot rat? That’s a fair question whose answer requires a bit of abstract thinking. To clarify your understanding of the project’s requirements you decide to ask me a few questions, starting with, “Does the robot rat exist?”

If I answered the question by saying, “Well, obviously, the robot rat does not really exist!”, I would be insulting you. Why? Because if you are in fact wondering just what is a robot rat, then you are having difficulty abstracting the concept of a robot rat. I would be doing you a better service by saying, “The robot rat exists, but only as a collection of attributes that provide a limited description of the robot rat.” I would also add that by writing a program to control the robot rat’s movements around the floor you are actually modeling the concept of a robot rat. And since a model of something usually leaves out some level of detail, or contains some simplifying assumptions, I will also tell you that the robot rat does not have legs, fur, or a cute little nose.

Another valid requirements question might focus on exactly what is meant by the term Java application. That too is a good question. A Java application is a class that contains a main() method. You could write this program as a Java applet as well, although doing so is not a requirement for this project.

What about error checking? Again, good question. In the real world, making sure an application behaves well under extreme user conditions, and recovers gracefully in the event of some catastrophe, consumes the majority of the programming effort. One area in particular that requires extra measures to ensure everything goes well is array processing. As the robot rat is moved around the floor care must be taken to prevent the program from letting it go beyond the bounds of the floor array.

Something else to consider is how to process menu commands. Since the project only calls for simple console input and output, I recommend treating everything as a text string on input. If you need to convert a text string into another data type you can use the helper classes provided by the java.lang package. Otherwise, I want you to concentrate on learning how to use the fundamental language features listed in the project’s objectives section, so I promise not to try to break your program when I run it.

You may safely assume, for the purposes of this project, that the user is perfect, yet noting for the record that this is absolutely not the case in the real world!

Summarizing the requirements thus far:

  • You must write a program that models the concept of a robot rat and its movement upon a floor

  • The robot rat is an abstraction represented by a collection of attributes, (I discuss these attributes in greater detail in the problem domain section below.)

  • The floor is represented in the program as a two dimensional array of boolean

  • Use just enough error checking, focusing on staying within the array boundaries

  • Assume the user is perfect

  • Read user command input as a text string

  • Put all program functionality into one user-defined class. This class will be a Java application because it will contain a main() method

When you are sure you fully understand the project specification you can proceed to the problem domain strategy area.

Problem Domain Strategy Area

In this strategy area your objective is to learn as much as possible about what a robot rat is and how it works in order to gain insight into how to proceed with the project design. A good technique to use to help jump-start your creativity is to go through the project specification and look for relevant nouns and verbs or verb phrases. A first pass at this activity will yield two lists. The list of nouns will suggest possible application objects, data types, and object attributes. Nouns will also suggest possible names for class and instance fields (variables and constants) and method variables. The list of verbs will suggest possible object interactions and method names.

Nouns & Verbs

A first pass at reviewing the project specification yields the list of nouns and verbs shown in table 3-4.

Table 3-4: Robot Rat Nouns and Verbs

Nouns

Verbs

robot rat

move

floor

set pen up

pen

set pen down

pen position (up, down)

mark

mark

turn right

program

turn left

pattern

print floor

direction (north, south, east, west)

exit

menu

 

This list of nouns and verbs is a good starting point, and now that you have it, what should you do with it? Good question. As I mentioned above, each noun is a possible candidate for either a variable, a constant, or some other data type, data structure, object, or object attribute within the application. A few of the nouns will not be used. Others will have a direct relationship to a particular application element. Some nouns will look like they could be very useful but may not easily convert or map to any application element. Also, the noun list may not be complete. You may discover additional application objects and object interactions as the project’s analysis moves forward.

The verb list for this project example derives mostly from the suggested menu. Verbs normally map directly to potential method names. You will need to create these methods as you write your program. Each method you identify will belong to a particular object and may utilize some or all of the other objects, variables, constants, and data structures identified with the help of the noun list.

The noun list gleaned so far suggests that the robot rat project needs further analysis both to expand your understanding of the project’s requirements and to reveal additional attribute candidates. How do you proceed? I recommend taking a closer look at several nouns that are currently on the list, starting with robot rat. Just what is a robot rat from the attribute perspective? Since pictures are always helpful I suggest drawing a few. Here’s one for your consideration.

This picture suggests that a robot rat, as defined by the current noun list, consists of a pen that has two possible positions, and the rat’s direction. As described in the project specification and illustrated in figure 3-2, the pen can be either up or down. Regarding the robot rat’s direction, it can face one of four ways: north, south, east, or west. Can more attributes be derived? Perhaps another picture will yield more information. I recommend drawing a picture of the floor and run through several robot rat movement scenarios.

image from book
Figure 3-2: Robot Rat Viewed As Attributes

Figure 3-3 offers a lot of great information about the workings of a robot rat. The floor is represented by a collection of cells arranged by rows and columns. As the robot rat is moved about the floor its position can be determined by keeping track of its current row and column. These two nouns are good candidates to add to the list of relevant nouns and to the set of attributes that can be used to describe a robot rat. Before the robot rat can be moved, its current position on the floor must be determined, and upon completion of each move its current position must be updated. Armed with this information you should now have a better understanding of what attributes are required to represent a robot rat as figure 3-4 illustrates.

image from book
Figure 3-3: Robot Rat Floor Sketch

image from book
Figure 3-4: Complete Robot Rat Attributes

This seems to be a sufficient analysis of the problem at this point. You can return to this strategy area at any time should further analysis be required. It is now time to take a look at what language features must be understood to implement the solution.

Language Features Strategy Area

The purpose of the language features strategy area is two-fold: First, to derive a good design to a programming problem you must know what features the programming language supports and how it provides them. Second, you may be forced by a particular programming project to use language features you’ve never used before. It can be daunting to have lots of requirements thrown at you in one project. The complexities associated with learning the Java language, learning how to create Java projects, learning an integrated development environment, and learning the process of solving a problem with a computer can induce panic. Use the language features strategy area to overcome this problem and maintain a sense of forward momentum.

Apply this strategy area by making a list of all the language features you need to study before starting your design. As you study each language feature mark it off your list. Take notes about each language feature and how it can be applied to your particular problem.

Table 3-5 presents an example check-off list for the language features used in the robot rat project.

Table 3-5: Language Feature Study Check-Off List For Robot Rat Project

Check-Off

Feature

Considerations

 

Java Applications

How do you write a Java application? What’s the purpose of the main() method. What is the meaning of the keywords public, static, and void? What code should go into the main() method? How do you run a Java application?

 

classes

How do you declare and implement a class? What’s the structure of a class. How do you create and compile a Java source file?

 

primitive data types

What is a primitive data type? How many are there? What is each one used for? How do you use them in a program? What range of values can each data type cont ain? How do you declare and use primitive data type variables or constants in aprogram?

 

reference data types

What is a reference data type? How do you declare and use reference data types in a program? What’s the purpose of the new operator? What pre-existing Java classes can be used to help create the program?

 

array data types

What’s the purpose and use of an array? How do you declare an array reference and use it in a program? What special functionality do array objects have?

 

two-dimensional arrays

What is the difference between a two-dimensional array and a one-dimensional array? How do you declare and initialize a two-dimensional array? How do you access each element in a two-dimensional array?

 

fields

What is a field? How do you declare class and instance fields? What’s the difference between class fields and instance fields? What is the scope of a field?

 

methods

What is a method? What are they good for? How do you declare and call a method? What’s the difference between a static method and a non-static method? What are method parameters? How do you pass arguments to methods? How do you return values from methods?

 

local variables

What is a local variable? How does their use affect class or instance fields? How long does a local variable exist? What is the scope of a local variable?

 

constructor methods

What is the purpose of a constructor method? Can there be more than one constructor method? What makes constructor methods different from ordinary methods?

 

flow-control statements

What is a flow-control statement? How do you use if, if/else, while, do, for, and switch statements in a program? What’s the difference between for, while, and do? What’s the difference between if, if/else, and switch?

 

console I/O

What is console input and output? How do you print text to the console? How do you read text from the console and use it in your program?

Armed with your list of language features you can now study each one, marking it off as you go. When you come across a good code example that shows you how to use a particular language feature, copy it down or print it out and save it in a notebook for future reference.

Learning to program is a lot like learning to play a musical instrument. It takes observation and practice. You must put your trust in the masters and mimic their style. You may not at first fully understand why a particular piece of code works the way it does, or why they wrote it the way they did. But you will copy their style until you start to understand the underlying principles. Doing this builds confidence — slowly but surely. Soon you will have the skills required to set out on your own and write code with no help at all. In time, your programming skills will surpass those of your teachers.

After you have compiled and studied your list of language features you should just have a sense of what you can do with each feature and how to start the design process. More importantly, you will now know where to refer when you need to study a particular language feature in more depth. However, by no means will you have mastered the use of these features. So don’t feel discouraged if, having arrived at this point, you still feel a bit overwhelmed by all that you must know. I must emphasize here that to master the art of programming takes practice, practice, practice!

Once you have studied each required language feature, you are ready to move on to the design strategy area of the project-approach strategy.

Design Strategy Area

Before you can solve the robot rat problem you must derive a plan of attack! The plan will consist of two essential elements: a high-level software architecture diagram and an implementation approach.

High-Level Software-Architecture Diagram

A high-level software-architecture diagram is a picture of the software components needed to implement the solution and their relationship to each other. Creating the high-level software-architecture diagram for the robot rat project is easy as the application will contain only one class. Complex projects, on the other hand, usually require many different classes, and each of these classes may interact with the others in some way. For these types of projects software-architecture diagrams play a key role in helping software engineers understand how the application works.

The Unified Modeling Language (UML) is used industry-wide to model software architectures. The UML class diagram for the RobotRat class at this early stage of project’s design will look similar to figure 3-5.

image from book
Figure 3-5: RobotRat UML Class Diagram

As figure 3-5 illustrates, the RobotRat class will extend (inherit) the functionality provided by the java.lang.Object class. This is indicated by the hollow-pointed arrow pointing from RobotRat to Object. (In Java, all user-defined classes implicitly extend Object so you don’t have to do anything special to achieve this functionality.) The RobotRat class will have attributes (fields) and methods. Attributes are listed in the attribute box and methods are listed in the method box. The main() method is shown. The plus sign appearing to the left of the main() method indicates that it is a public method.

Implementation Approach

Before you begin coding you must have some idea of how you are going to translate the design into a finished project. Essentially, you must answer the following question: “Where do I start?” Getting started is ninety percent of the battle!

Generally speaking, when formulating an implementation approach you can proceed macro-to-micro, micro-to-macro, or a combination of both. (I realize this sounds like unorthodox terminology but bear with me.)

If you use the macro-to-micro approach, you build and test a code framework to which you incrementally add functionality that ultimately results in a finished project. If you use the micro-to-macro approach, you build and test small pieces of functionality first and then, bit-by-bit, combine them into a finished project.

More often than not you will use a combination of these approaches. Object-oriented design begs for macro-to-micro as a guiding approach but both approaches play well with each other as you will soon see. Java forces the issue somewhat by requiring that all methods and attributes belong to a class. (Unlike C or C++ where functions, variables, and constants can exist independently.)

When you start your design there will be many unknowns. For instance, you could attempt to specify all the methods required for the RobotRat class up front, but as you progress through development you will surely see the need for a method you didn’t initially envision.

The following general steps outline a viable implementation approach to the robot rat project:

  • Proceed from macro-to-micro by first creating and testing the RobotRat application class, devoid of any real functionality.

  • Add and test a menu display capability.

  • Add and test a menu-command processing framework by creating several empty methods that will serve as placeholders for future functionality. These methods are known as method stubs. (Method stubbing is a great programming trick!)

  • Once the menu-command processing framework is tested, you must implement each menu item’s functionality. This means that the stub methods created in the previous step must be completely implemented and tested. The robot rat project is complete when all required functionality has been implemented and successfully tested.

  • Develop the project iteratively. This means that you will execute the plan-code-test-integrate cycle many times on small pieces of the project until the project is complete.

Now that you have an overall implementation strategy you can proceed to the development cycle. The following sections walk you step-by-step through the iterative application of the development cycle.




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