P-Coder Student Guide

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Geoffrey G Roy

School of Engineering Science

Murdoch University

 

 

 

 

 

Jan 2005


Contents

 

1      Introduction. 1

2      Overview.. 2

2.1       Starting P-Coder 2

2.2       Command Options. 3

2.3       Node Types. 4

3      Computational Concepts. 6

3.1       Sequence. 6

3.2       Iteration. 6

3.3       Selection. 7

3.4       Recursion. 8

4      Data and Variable Definitions. 10

5      Program Annotations. 12

6      Using the P-Coder Designer 13

7      Building a Simple Program.. 17

8      Adding Details to Nodes. 32

8.1       Direct Editing of Node Notes. 32

8.2       Using the Node Details Dialog. 33

8.3       Searching the Model 34

9      Using the Class View.. 36

10        Using the Module View.. 41

11        Using the Code View.. 42

12        Using the Object View.. 44

13        Guidelines for Constructing Models. 46

13.1     Getting Started. 46

13.2     Adding Method Nodes. 47

13.3     Adding Operational Nodes to Methods. 47

13.4     Adding Iteration Nodes. 48

13.5     Adding Selection Nodes. 49

13.6     Adding Switch/Case Nodes. 50

13.7     Adding Recursion Nodes. 51

13.8     Adding Try/Catch Nodes. 52

14        Summary. 53

 


1         Introduction

P-Coder is a support tool to assist in learning some of the basic principles in programming.  While it has a bias towards Java, it could equally be applied to most (good) programming languages.

The “P” in P-Coder stands for “Pseudo”.  Pseudocode is a commonly used word to describe how a program actually works before you write down all the instructions in the actual programming language.  It is thus an intermediate stage between the written/spoken descriptions of what the program is required to do, and the final code that you can compile then execute on the computer.

P-Coder is a tool to help you make the transition from a description of the computational task to achieving an operational program.  This intermediate step is quite important, and if it is done well many problems (that can crop up in programming) might be avoided and much time saved.  The main goals of using a pseudocode step is to make certain you better understand what the computational task is all about, and to design a solution to it that has a good chance of working.

P-Coder is a graphical design tool that help you visualize how your program should/can work and thus help you achieve a better understanding of how to do programming (well).  P-Coder provides support for a full range of the essential programming concepts that will enable you to do your first programming exercises.  P-Coder is not intended to be a professional programming tool – there are many others that are better designed for that purpose – but they can be difficult to learn to use.

This User Guide does not contain the full operational description of P-Coder, this is described elsewhere (in the Instructor Guide).


2         Overview

2.1      Starting P-Coder

P-Coder is started from the shortcut icon that you will find on the desktop, or in the Start menu, of your computer. It looks like this:

Figure 1: The P-Coder Icon

The Designer View is the primary user interface for the program design process; it looks like that shown in the following diagram. 

Figure 2: The main window of P-Coder

The P-Coder Designer consists of a graphical display area and a set of node icon buttons in on the left side.  Each of these corresponds to a node type that can be inserted into the program.  Note that a number of these buttons are disabled (for the moment) as these correspond to nodes that are automatically inserted into the model (e.g: Class Data, Then and Default nodes; as they are required for their respective parent nodes).

The text field at the bottom of the window will show messages as you use the program.

 

 

 

 

2.2      Command Options

Most commonly used operations are available from the toolbar across the top part of the window, these are:

 

Toolbar Icon

Operation

Opens a P-Coder model (xml) file

Saves the current model

Prints the current model

Open the Find tool

Opens the Module view with a selected P-Coder model

Opens the Code View

Opens the Class View

Opens the Object View

Terminates the program (bottom left of window)

 

These and other command options are available from the menu bar options, as follows:

 

Menu

Option

Action

File

Load

Loads a P-Coder model from disk

New ► Program

Creates a new P-Coder model from a Program node.

New ► Package

Creates a new P-Coder model from a Package node.

New ► Class

Creates a new P-Coder model from a Class node.

New ► Method

Creates a new P-Coder model from a Method node.

Import

Imports an existing Source file to create a skeleton model

Save

Saves the current model

Save As…

Saves the current model to a new file name

Page Setup

Show the page setup dialog to set page and printer properties.

Print

Prints the current model

Quit

Terminates the program

Node

Enable-Disable Class Bar

Enables-Disables the Class highlight bar

Open All

Opens all nodes in model

Open Selected

Opens those nodes that are currently selected

Close All

Closes all nodes in model

Close Selected

Closes those nodes that are currently selected

View Selector

Shows View Selector dialog

Reset All Nodes

De-selects all nodes

Delete Selected Nodes

Deletes all currently selected nodes.

View

Java Code View

Shows the Code View

Module View

Requests a module to load, then displays it in the Module View

Class View

Shows the Class View

Object View

Shows the Object View

Find

Search for text in model

Help

About

Shows the about dialog

Credits

Contributors to P-Coder

Enter userID

Show the userID dialog if you are required to enter or change your userID.

Enable/Disable Tool Tips

Enables/disables tool tips on tool bar icons.

 

Look & Feel ► Option

List of look and feel options for current installation (e.g. Windows, Motif, Metal)

 

2.3      Node Types

In P-Coder, the program design is composed of nodes, of different type, connected in a tree-like diagram.  Each node represents a particular computational step or process.

 

Node Icon

Formal Name

Description

Program

The root node that defines a complete program.

Package

Placed within a Program to contain sets of Classes.

Class

Defines a Class.

Class Data

Contains all Class fields (data items).

Method

To define a Method – contains the computational processes in the Class.

Local Data

Contains all local items of data, i.e. local to the current Method.

Sequence

Defines a computational step.

Iteration

Defines a set of computations that will be repeated a number of times

Selector

Defines a conditional branch in the computation, will be followed by a Then and (optionally) and Else node.

Then

Defines the computations to be performed if the Selector condition is true.

Else

Defines the computations to be performed if the Selector condition is false.

Switch

Defines a switch operation, used with Case and Default nodes.

Case

Defines the computations for a particular case of the Switch

Default

Defines the computations for all other cases of the Switch

Recursion

Defines a recursive computation.

Try

Adds s Try node where required to test for a failure condtion.

Catch

Defines what to do if a particular exception is caught

Finally

Defines what to do to after all Try and Catch operations are completed.

Comment

To contain commentary notes into the model and any place.

 

To help you learn these commands, there is an option on the Help menu to enable/disable tool tips on each of the tool bar icons.  With tool tips enabled, a short popup description of each icon will appear by just placing the cursor over the icon.


3         Computational Concepts

You will be introduced to the basic computational concepts elsewhere, but we will repeat them here in the style that they appear in P-Coder.

3.1      Sequence

Computational tasks normally occur in a well-defined sequence, usually following the order in which the instructions are defined.  Instructions must exist inside a container; in Java we call this a method[1].  Here is a sequence of instructions inside a method:

Figure 3: A Sequence description

When it is appropriate to execute the Method, the instructions are executed in the same sequence in which they are defined: First, then Second then Third.  This is the simplest form of programming.

3.2      Iteration

There are many situations where we need to repeat the execution of a set of instructions a number of times, sometimes a fixed number of times, sometimes the repetition continues until some conditions are achieved.

Here is a simple iteration:

Figure 4: An Iterator description using a For loop

This iteration requests that the operations “First step” and “Second step” be repeated like this:

First step
Second step
First step
Second step
….

…..
First step
Second step

for a total of 10 cycles (iterations) through the loop.  The iterator provides a convenient notation to describe what is required.  After all the required iterations are complete the next sequential statement (“next statement after iterator”) will be evaluated.

There are two other variants of the iterator, the “While” form like this:

Figure 5: An Iterator description using a While loop

In this case the iterator keeps on cycling through the sequence of statements while the condition (“number is greater than zero”) is satisfied.  If this condition becomes false (as we hope it will at some time – otherwise our program will just loop for ever!), then the iterator stops and the execution passes to the next sequential statement after the iterator.

The third form, Do/While, looks similar to the above except that the iterator test condition is applied after the statements included in the iterator are complete.  This difference is a bit subtle, but you will see the difference in due course.

3.3      Selection

Selection is a process that enables you to specify that only some program instructions should be evaluated, depending on some condition, like this:

Figure 6: A simple Selector

If “something is true” then “do this” is evaluated, otherwise nothing is done.  Here is a more general version:

Figure 7: A complete Selector

In this case if the condition fails, the “else” branch is taken and “do that” is evaluated.

These selection forms define the if-then-else constructs used in most programming languages.  There is another selector that can do much the same job, but it looks a little different and is better suited to programming tasks where there is a larger number of alternate paths of execution.  This is the switch/case construct:

Figure 8: A Switch/Case description

In this case there are (generally) several branches to the flow of execution each dependent “on the value of X” as defined in the Switch node.  Each Case node defines a condition for that branch to be followed.  The Default branch will be followed if none of the other cases are found to be true.

3.4      Recursion

Recursion is a more difficult concept to understand, but it is extremely powerful and often crops up in writing software.  Here is a simple example:

 

Figure 9: A Recursion description

In this case we need to combine the recursion node with a selection process for it to make sense.  It reads like this: “if some condition is true then stop the execution, else execute Method again”, where Method is the method in which the execution is taking place.  We will see some examples of recursion a little later.

A first time reader should now go to Section 6.  You will need to come back to Section 4 later on.


4         Data and Variable Definitions

Most programs require data to be defined, usually associated with one or more variables that allow the items of data to be referenced (referred to) in a clear and unambiguous way.  Most programming languages require every item of data to have a well-defined type (an int number, a double number, a String value, and so on).  We will come to data typing in a more detailed way later, but we will need to consider how we can describe these items of data in the P-Coder notation.

A major concern is the scope of the data (the variables if you like).  In Java we can have variables with many different scopes, in P-Coder we will think of just two types of scope: Class data and Method data.

Class data is defined for the class as a whole and is available (i.e. visible) to all methods within the class.  In the following diagram we have a class containing some items of data and two methods.  In P-Coder we will assume that every Class will have some items of data (there could be none, but that would be the exception), so every time we create a new Class node, a Class data node is automatically added as shown here:

Figure 10: Class Data and Methods

Inside this Class data node there may be some variable (or data) definitions.  We will come to these later in more detail, but they will consist of declarations like this:

int variableA = 5
double variableB = 1.99
String a_string

with some having initial values defined, and others being just a declaration of type and name.  These items of class data have a scope across the entire Class.  This means that the variables are visible from inside all methods in the class, and can be used (and changed) within any method within the Class.

Items of data (variables) can be defined in many places in a (Java) program, and inside a Method is not unusual.  A Local Data node may be added to a Method node, like this:

Figure 11: Local Data Nodes

The Local data nodes are there to contain variable definitions in the same way as Class Data nodes.  The only difference is that, this time, the variables have a scope only within the method itself (hence the term “local” data).  Local data nodes are optional.

 


5         Program Annotations

Whenever a program is being written is it advisable, if not essential, to annotate the code with comments.  These comments are intended to explain the operation of the program to yourself, and others, long after the program has been written and many of the assumptions forgotten and you have moved on to another job.  In complex software packages, comments are an essential part of the program documentation process.

Each node in a P-Coder model contains a line of text that adds explanations to the node.  While the node icon is indicative of its meaning (semantics), the following line of text provides away of providing a useful explanation for the reader (i.e. you and anyone else who need to understand the program)

P-Coder also provides a special node (Comment) to allow for even more commentary to be provided.  Comments do not form part of the executable elements of the code.

Figure 12: Comment Nodes


6         Using the P-Coder Designer

The primary user interface was described in Section 2.  In this section we describe how to create a program model in the Designer View.

The basic editing tasks are described in the following table.  Read these notes carefully as you will need to learn these operations so they become second nature.

 

Operation

Steps

Result

Add a new node

<left click> on the required node type in the button panel on left.

Cursor changes to traffic light, move it to required location and when green <left click> to place the node in the model. If the traffic-light cursor is not green the <left click> will terminate the add node operation.

Move an existing node (and its sub-tree) to a new location

<left click drag> on the required node in diagram

Cursor changes to traffic light, and a drag rectangle appears.  While holding left button down, drag the node to new location in the model and when traffic light becomes green release the mouse button.  If the traffic-light cursor is not green, releasing the mouse button will terminate the move operation.

Copy a node (and its sub-tree)

<right click> on node in diagram, and choose “Copy” from menu.

A copy of this node and its sub-tree is made and saved in the cut buffer.

Cut a node (and its sub-tree)

<right click> on required node and choose :”Cut” from the menu.

The selected node and its sub-tree is removed from the model (and placed in the cut buffer), if it is allowed to be cut.

Paste a node from the cut buffer

<right click> on a node and choose the paste location option (before, after, as child)

The node currently in the cut buffer is pasted to the chosen location (if valid).

To close node

<left click> on an open node

The tree below the node is rolled up.

To open a node

<left click on a closed node

The tree below the node is rolled out.

To mark a node as selected

<ctrl-left click> on node

The node is shown as selected (highlighted)

To reset a selected node

<ctrl-left clock> on a selected node

The node is de-selected and the highlighting is removed.

To open the details dialog for a node

<shift-left click> on node

The details dialog for the node is shown

To show the popup menu for a node

<right click> on the node

The popup menu is shown

 

The “traffic-light” cursor () is intended to show you where a node of a particular type can be located.  If the current location (as defined by the position of the cursor) is a valid place to locate the currently selected node (either as a new node, or an existing node being moved) then the cursor shows a green light ().  In some environments the “traffic-light” cursor will not appear correctly, standard system cursors are used instead.

The various P-Coder components are used to build complete programs, or parts of programs.  A program module can be any of:

Program:         consisting of one or more Packages

Package:         consisting of one or more Classes

Class:              consisting of one or more Methods

Method:          consisting of one of more executable statement blocks.

A usual way of starting is to begin with a new Program that, in P-Coder terms, looks like this:

Figure 13: Typical starting point for a P-Coder model

Some nodes are automatically added for convenience.  From here additional nodes can be added as required.

The placement of new (and copied/pasted) nodes into the model is governed by a set of rules that try to prevent errors being made.  When adding or moving a node the traffic light cursor will turn green if it is placed over a valid location for the type of node being added or moved.

The key to understanding the placement location is shown in the following diagram:

Figure 14: The Hot-Spots for selecting nodes

Each node is attached to a parent by a short horizontal connecting line at the left of the node icon.  Children are connected to a node by the line from the middle of the icon and going down.

If a node is to be added as a child of an existing node, then the cursor must be placed over that node in the “Child Hot Spot”.  If the node is to be added as a sibling to the node, i.e. to the same parent, then the placement positions are just to the left of the icon.  Just above the line places the new node before this node, while just below the line places the new node after the node.  These three options allow a tree of nodes to be easily built and edited.  The same hot spots are used when adding or moving nodes. 

Cut and paste operations are also possible, they happen directly on nodes that are selected by <right-clicking> on a node.  This action causes a popup menu to appear. 

Figure 15: The Node Pop-up Menu (<right click>)

This menu offers a number of options:

As the program gets larger, and more complex, it is useful to be able to view parts of the Designer View diagram and to hide other parts.  This allows you to focus attention on that part of the program you are currently working on.  Any node in the tree that has a sub-tree attached to it can be rolled-up or rolled-out as required.  There are various ways of doing this.  For example:

A rolled-up node is indicated by a small down arrow, while a rolled-out is indicated by a small up arrow.  Clicking on a node will thus cause the node to roll (up or out) in the direction of the arrow.

It is also possible to operated on groups of nodes:

The View Selector dialog provides a much more flexible way of displaying the program.

Figure 16: The View Selector Dialog

From this dialog, you can nominate actions (open and close, and no change from current status) for different types of nodes.  In the above dialog, all Package nodes will be opened, all Class nodes and all Method nodes will be closed.  From this dialog it is possible to set quite complex display strategies. 

Operations on Method nodes do not affect the display settings within the Method node. Operations on All Nodes (open and close) will affect the display of all nodes throughout the model.


7         Building a Simple Program

To show this works let us build a simple program.  The program is required to read in two numbers, compute their average and display the result.  The steps involved are displayed in the following diagrams.

Please remember that it is always important to save your work from time to time.  If you make a mistake, and do not know how to recover, you can then always go back to a known previous state.

To begin start up P-Coder, and then follow these steps (be careful to follow them exactly):

Step 1:             Create a New Program from the File|New|Program menu; a simple template is created.

Figure 17: Creating the new program

Step2:              Change the default text on the New Program node: <right-click> on the text field, the text editor is displayed with the cursor position shown by the (red) insertion cursor. Use the arrow keys (←, →) to move the insertion cursor, the <backspace> key to delete characters to the left of the cursor, and any other key to insert a character into the node note.  To complete the editing type an <enter> key.  To abort the editing type the <escape> key.  A more detailed description of these editing operations is given later.

Figure 18: Editing the node note

Step 3:             Change the note on the class node to “Average Class” using the same process.  We now have some useful descriptions of what the program is intended to do.

Figure 19: After editing the node note

Step 4:             Add a new Method node: click on the Method symbol () on the menu, the cursor changes to a traffic light ().  Move the cursor over the Class node; the traffic light will change to green (). Now <click> so that the new method node is added as a child of the Class node.

Figure 20: Adding a Method node

                        Most often Methods will contain data definitions, these are contained in Local Data nodes, add one of these now:

Figure 21: Adding the Local Data node to the Method

Step 5:             Note that the small up/down arrow symbol shows if a node has children and if it can be expanded to see them, or rolled up to hide them.  If you <left click> on the New Method node it will “roll up” like this.

Figure 22: Rolling up the Method

                        Now we see the small arrow symbol showing that there is more to the New Method node than is shown.  To expand (roll-out) the node; <left click> on it again.

Step 6:             In Java, the Method is where computations can take place, so it is here that we add the program instructions.  Add three sequence nodes () to the Method node by clicking the menu option, then dropping them onto the Method node.

Figure 23: Adding some Sequence nodes

Step 7:             Now we get to the actual algorithm required for the program – it is quite simple: read data, compute average, and write results.  Change the notes on the three sequence nodes to match, as well as the note on the Method node to something meaningful for the program.  We now have the pseudocode relatively complete.

Figure 24: Editing the Sequence nodes

From this point on, we will be introducing a number of programming concepts that you may not have seen before (if you have no prior experience).  For the moment just accept them as describe, their meaning will become clearer as you learn more about programming.

Step 8:             We can now start thinking about how to do these computations.  First we need to read in the values for two numbers – this will require two variables.  Let us name them “num1” and “num2” and we will take them to be of type “double” (i.e. that can be fractional decimal numbers).  We need to add these definitions to the Class Data node for the Average Class.

                        To examine the contents of the Class Data node (), or any node for that matter, we need to open the Node Details dialog.  This can be done in one of two ways:

·        <shift left click> on the node

·        <right click> on the node, and choose Node Details from the popup menu

The details dialog for the Class Data node will then appear. 

Figure 25: The Class Data Dialog

Add a definition for each variable by typing in the appropriate text into the top fields:

·        enter “double” into the Type field (no quotes)

·        enter “num1” into the Name field.

·        Enter “the first number” into the Comment field

·        <click> Add.

Repeat for “num2”, and also add another variable “aver” which will be used to hold the computed average value.

                        The dialog should now look like this:

 

Figure 26: Adding items of Class Data

 

If you make a mistake, <click> on the required entry, edit the entry in the top fields, then <click> Update.

The comments are there for you to add a short description of the purpose of each variable/data item.

Once complete, click Accept.  This data is now added to the Class Data node.

Note that every node in the Designer View has an associated set of details.  Each node has its own dialog (like this one) containing the fields required to fully define it.

Note that the Help button will provide, in a Help Dialog, some assistance in completing each Details Dialog.  Try it out. 

Step 9:             The method we have created needs to be given a formal name and some additional information.  Open the Node Details dialog for the Method node (<shift left click>).  In the dialog enter “void” in the Type field (our Method will not be returning any data), and “average” in the Name field (to give it a formal name).  Since there will be no data sent to the method, leave the arguments field empty.

Figure 27: The Method Node Dialog

Step 10:           Do the same for the Class node, to give the class a formal name “Average”.

Figure 28: The Class Node Dialog

                        In some cases, as here, there will be some fields that are left empty – you will learn about these in due course.

Step 11:           The pseudocode is now essentially complete, i.e. we have described all the essential steps to compute the average of the two numbers.  The next step is to complete the development of the code, so let us take a look at how the program code looks. 

From the main menu choose View|Java Code View, or choose the Code icon from the toolbar.  The Code View will appear.  Here we see the overall structure of the program as Java code.  Some of the actual code has been automatically generated, but some is missing still. 

We can see the class and method definitions, the local data declarations and the pseudocode notes included as comments (“/**<……>**/”).  Use the options at the bottom of the window to highlight/dim the code segments, as well as to hide the pseudocode notes.

Figure 29: The Code View with a partially complete program

 

Step 12:           To complete this program (and make it so we can execute it) we need to add two additional components: A “main” method that is necessary for the Java interpreter to create an Average object, and a special method, called a Constructor, to initialize the “Average” Class.

                        Please follow the following steps closely.

Step 13:           Add a new Method just before the “average” method, and call it “main method” like this (note that I have also added a Local Data node as well, this is optional and we will not use it here):

 

Figure 30: Adding the main Method

                        Within this new Method, add a Sequence node, and call it “create Average object”, like this:

Figure 31: Adding a Sequence node to the main Method

 

Step 14:           Open the Node Details dialog for the “main method” node, and add the following details:

Figure 32: The main Method Node Dialog

                        Add the details exactly as shown.  Note that the entry in the Arguments field is “String [] args”. This specification is required in all Java programs. Now <click> Accept.

Step 15:           Now open the Node Details dialog for the “create Average object” node, and add this information:

Figure 33: A Sequence Node Dialog

                        This instruction is a key one: it will create a new Object of type Average, and assign it to a variable called “av”.

                        This node is a Sequence type node; its code field will contain a complete segment of Java code.  Each Java statement must be terminated by a special character (a semi colon).  If you leave this character out, P-Coder will add one for you when the code is generated in the Code View.  Your instructor may configure P-Coder to give you a warning message if you forget to insert the ‘;’ at the end of each line of code.

                        Other node Details dialogs contain only partial code segments, so ‘;’ should not be added to terminate lines (they may, however, be required within the code segment).  P-Coder will normally decide to insert the semi-colons as and when needed.

Step 16:           The next step is to create the Constructor for the Average Class, so that when the “av” Object is created it knows what it is required to do.  Now add another Method just before the “main method” node, and add two Sequence nodes like this:

Figure 34: Completing the main Method

                        The two Sequence nodes are given names to describe their purpose, as follows:

·        “create JavaConsole”: since we need to read some numbers we need a way of doing this.  In this example we will use another Object that will be created from the JPEconsole Class[2].  JPEconsole is a simple Class for performing I/O in a Java program and is provided as a supplement to P-Coder.

·        “execute average method”: this will cause the “average” method to be evaluated.

Step 17:           We need to add some details to these new nodes as follows:

                        For the “constructor method”:

Figure 35: The constructor Method Node Dialog

                        Note that we check the Constructor checkbox and leave the Type field empty for a constructor method.

                        For the “create a JavaConsole” node:

Figure 36: Creating a JavaConsole object

                        We are saying here: create an object called “con” that is a JPEconsole.  This new variable (“con”) must be declared in the Class Data node, so now go back to the Class Data node and add it, like this:

Figure 37: Extending the Class Data Node

 

                        For the “execute average method” node: add the following instruction that will cause the “average” method to be evaluated.

 

Figure 38: The code to compute the average

Step 18:           We can now take a look at the Code View again; it will look like this:

Figure 39: The Code View, with most of the code created

                        We see the three Methods (the constructor: Average, the main method: main, and the method to compute the average: average).  We see that there are still some missing details in the “average” method, but everything is in place to complete the program.

 

Step 19:           Take the three sequence nodes in the “average” method in turn (in the Design View) and now add some code segments.  Open their Node Details dialogs then add the required code, as shown below.  We will use the JPEconsole Object (named “con”) to perform the I/O.

 

Figure 40: Adding the code details

                        You should try to follow the Java code to see how it reflects the desired task for each node.

 

Step 20:           From the Code View we now have a complete program with all the Java code in place.

Figure 41: The Complete Code View

                        We can now complete the task to execute the program.  The Save button will cause the Java source file to be saved (in your current working directory).  The Java source must be compiled with the Java compiler; this is done by <clicking> the Compile button.  If you have followed all the steps exactly this should be successful (you will get a message to confirm this). Then the Execute button can then be chosen.

                        The JavaConsole will first appear, like this, with the prompt message shown:

Figure 42: The program started using JavaConsole

                        Enter two numbers into the input field (at the bottom), either space or comma delimited, then <click> the Enter button.

Figure 43: Example execution of the program

                        Now we see the result of the computation.

                        The program is terminated by <clicking> the Close button.

                        You now have completed your first Java program using the P-Coder tool to design, code, compile and execute it.

 

 


8         Adding Details to Nodes

All the important information for the pseudocode description is contained in the notes written against each node in the P-Coder Designer View window.  When a node is first created it will be given a default note (one that is generally explanatory of its purpose).  The user must edit these notes so that the appropriate descriptions for the program being developed are made clear.  The notes can be edited directly in the Designer View.  As the design of the program proceeds, additional details need to be added to each node via the Node Details dialog.

8.1      Direct Editing of Node Notes

The editing functions are described in the flowing table:

 

Task

Operation

Result

Select a note for editing

<right-click> on note

The note is selected (grey background), and the insertion cursor is placed after the last character in the note.

Move cursor to left

Use the ← key.

Insertion cursor moves to left. Holding down the ←  key moves the cursor to the left in rapid steps.

Move the cursor to the right

Use the → key.

Insertion cursor moves to right. Holding down the → key moves the cursor to the right in rapid steps.

Insert a character

Type any standard key

Key typed is inserted at the cursor insertion point.

Delete a character

Use the <backspace> key

Character before insertion cursor is deleted.  Holding down the <backspace> or <delete> key deletes characters in rapid steps.

Delete whole note

Type <ctrl X>

The whole note is deleted, and saved in the text buffer.

Copy whole note

Type <ctrl C>

The whole note is copied to the text buffer.

Paste text from text buffer into note

Type <ctrl V>

The contents of the text buffer are inserted into the note at the insertion point.

Save changes made to note

Type <enter> key

The modified node note is saved and the note is de-selected.

Cancel any changes made to note

Type <escape> key

The modifications are ignored, the note is de-selected and the original text inserted.

To remove the fixed text parts, i.e. the text included in “[….]”.

Type <ctrl delete>

The protective “[  ]” characters are removed and the note is fully editable.

To move past the protective “[..]” characters.

Type ­ key

The insertion point jumps the “]” character.

 

For some nodes, it is appropriate to include some (semi) fixed parts to the note; these are included in “[…]”, followed by some other text.  It is normal to leave the fixed parts of the notes intact (and protected) while editing the remaining parts.  The idea is that the fixed part of the note should remain in place to help remind you about the functions of that node, even if you add additional text descriptions.

Normally editing of the text inside the “[..]” is prevented, but it can be done as follows.

A user may intentionally add “protected” text into a note by placing it within “[..]”, this will prevent accidental editing/deletion of important note information.  To remove the protective “[..]” characters, type <ctrl delete>.

The first time student should stop now.  You will need to come back and work through this Guide more thoroughly later.

8.2      Using the Node Details Dialog

The Node Details dialog provides access to all the data describing a node.  Some examples have been shown earlier.  The content of the dialog varies with the node type selected, but it has a typical appearance like the following two examples:

 

Figure 44: Example Node Dialogs

Generally we have:

In some installations the node note section can also be edited here.  In this case the “fixed” part of the note is placed in the left-hand field, and the remaining part of the note in the right hand field.  If you wish to edit the “fixed” part, then you must first enable it by clicking the check box (located between the two fields).

Figure 45: Editing the "fixed" text

The text entered then appears as “fixed” text inside “[….]” in the main Designer View window.

8.3      Searching the Model

It is possible to search the model for text strings from the View|Find menu or from the Search toolbar item.  This command will launch the Find dialog:

 

Figure 46: The Find Dialog

Type the required target text into the field, then choose the breadth of the search:

Just check the required checkboxes to define the breadth of the search.

The Find Next button finds the next occurrence in the model, and the Find All button locates all occurrences.  To start a new search, use the Reset button to set the search pointer back to the root node of the model.

When a match is found, the node is highlighted like this:

Figure 47: Found nodes highlighted

The Find dialog can be left open while editing the model in the Designer View.  It will need to be closed, then re-opened if a new model is loaded.

 


9         Using the Class View

To understand the Class View you will need to have been introduced to Object-Oriented (O-O) programming.  The Class View shows how the various classes you define in your program relate to each other.

The following diagrams demonstrate an example.  In this case we have a Package with a number of Classes that define the properties of different shapes.  At the “top” of this hierarchy we have the Shape class that provides the most general description.  Below this we have other shape types (Oval, Circle, Polygon, Rectangle, etc), in each case further specializing the shape type.  Here is the pseudocode (Designer) view:

Figure 48: The Shapes Model

And here is the Class view:

Figure 49: The Class View for the Shapes model

Note that when it is first displayed, only the list of packages is displayed on the left, you can to expand the required package by <left-clicking> on the expand (►) arrow which is located on the right side of the list at the Package node.  The Package list can be expanded/collapsed to show/hide the classes it contains by clicking on the small up/down arrow symbols in the Package node.

In the class view we see that some of the Classes “extend” the capability of a parent class.  For example, while the Polygon class, the Regular Polygon class and the Oval class have some identical properties (as defined in the parent Shape class), they have their own specialist properties to define their specific forms.  The arrows show the “extends” relationship between the classes.

The information contained in the class boxes has been entered in the Designer view in the respective Node Details dialogs for both the Class and Class Data nodes.  As well, the summaries of the Methods that are contained within each of the Classes are also included.

The class boxes have three sections:

The class boxes can be collapsed/expanded to show less/more detail from the popup menu which can be seen by <right-clicking> on the class box.  Class boxes can be displayed with three levels of detail:

Examples are shown in the following three figures.

Minimum detail:

Figure 50: The minimum display for the Class

Medium detail:

Figure 51: The medium view for the Class

Maximum detail:

Figure 52: The maximum view of the Class

 

The general placement of the Class boxes can (and must) be done by the user.  By default all class boxes will be located at the top-center of the window.  They can be moved about by <left-click-dragging> them to a better location.  The connecting links are automatically located, though sometimes you may have to move the boxes around to get a good placement of these links.

If the class box is not wide enough, some of the text inside it may be clipped.  You can change the width of any class box by <left-click-dragging> on the re-size handles that are located at the top-right of each class box.

The Class Viewer can show three different types of association between classes:

The P-Coder model can also be edited (to some extent) in the Class View using the options on the popup menu as shown in Figure 53.

 

Figure 53: The Popup Menu in Class View

The popup menu is activated by <right clicking> in the Class View.  If the <right click> is over an exiting Class all the menu options are available, if the <right click> is not over a Class only the Add/Edit Class option is active allowing a new Class to be added at the point clicked.. 

Items of Class Data can be added, edited and deleted.  Methods can also be added, edited and deleted (only the Method nodes, not the content of the Methods).  In each case a dialog is activated to allow you to fill in the required details, just like the ones shown in Section 8.2.

Changes made in the Class View will be reflected in the Designer View immediately so the two views will always refer to the same model.

Note that it is not possible to directly edit the associations between the Classes, these are always inferred from the definitions contained in the Class, Class Data and Method nodes.

 


10   Using the Module View

Most computer programs are written by re-using software components that have been previously written by yourself or by others.  You may be provided with some of these by your instructor.  In such a case you can load these (to view them) into the Module Viewer.  They appear just like a program in the Designer View, except that you cannot edit them.  The intention is that you can view it, study it and copy it (or parts of it), into your own program.

You may also be given program components for you to study where they demonstrate some key programming concept, style or methodology.  These can be conveniently viewed in the Module Viewer.


11   Using the Code View

Depending on the options specified by your instructor, the Code View can facilitate a number of tasks.  The Code View window is shown in the following figure.  Some things to note:

Figure 54: The Code View

Depending on your installation setup you may have some of the following options available:

Your instructor will provide guidance on how and when to use these features.  If they are not available the buttons and menu items will be disabled.

 

 


12   Using the Object View

As you become more familiar with O-O programming concepts you will need to understand more about how objects are created and what goes on inside them.  Of some importance is how Objects and Classes relate to each other.  Remember that a Class is a framework that is used to create Objects.  Several Objects can be created from a single Class, perhaps each having (some of) their own properties.  In P-Coder we have a way of creating and exploring Objects in some detail.

The Object View in P-Coder looks like this:

Figure 55: The Object View

Each class that is defined in your model appears on the left, as a wire-frame box.  Objects that have been created appear to the right (in each case).  In this example we have a Class called “Person” and two Objects (named “Fred” and “Susan”) that have been created from this Class.  “Fred” and “Susan” are thus objects of type “Person”.

The information on the right shows you all the internal information about the selected Class/Object.  It shows:

It may be possible for you to set/change the values of the items of Class Data as well as to evaluate the methods defined for the Object without formally executing the whole program.  For example you could send the required items of data to a method (as defined by the arguments), have the method “execute” and then see the return data, if any.  Such a capability is very useful for testing your program.

Once Objects are created in the Object View it is possible to look inside them with the Object Inspector as shown in the following figure.  In this case we have selected an item of Class Data from the Class Data field (a Vector type), then chosen Inspect.  The Object Inspector dialog then shows what is inside this item of data.  In this case it contains a Point, a TreeMap and a Hashtable (more complex data types).  Choosing the second of these, and Inspect again we see it contains a Point and a Rectangle.  Choosing the Point and Inspect again we see the details of the Point Object.

Figure 56: Inspecting data in the Object View

 

Once you have learned more about O-O concepts you will have an opportunity to explore these capabilities much further.  The Object View may not be initially available in your laboratory, depending on how your instructor has configured the installation.


13   Guidelines for Constructing Models

P-Coder operates with a set of rules that determine what nodes can be connected to other nodes.  There is a reasonably well-defined grammar, but this may be a little difficult for you to interpret directly.  In this section we will go through all the rules showing them graphically.  This section is intended to be an introduction only, not all the options are explained.

13.1Getting Started

The root of the model tree is usually a Program node like this:

Figure 57: Typical starting point for a new model

The Program node has a single Package node attached, together with a single Class node with a Class Data node attached.  This starting point is generated from the File|New>Program menu option.  It is possible to start off with a Package node at the root of the tree, or a Class node, or even a Method node as shown in the following three examples.

 

(a)

(b)

(c)

Figure 58: Other possible starting points

 

While each option is valid, there are some differences in the purpose/capabilities of the models built – you will need to get some guidance from your instructor.  For example models starting with a Method node cannot be compiled (and hence executed).  If you have no idea where to start, use the Program node as a starting point.

You will note that the Class node comes with a Class Data node attached.  This is always the case, and the Class Data node cannot be removed, there must always be one attached to every Class node.  It will always be the first node in the Class.

The first thing to understand is that:

13.2Adding Method Nodes

The following Figure shows a Class node with two added Method nodes.

Figure 59: Method Nodes added to a Class Node

In many situations a Local Data node is required to contain variable definitions that are, local to the Method.  A Local Data node is therefore often the first thing added to the Method node, as shown here.  The Local Data node can be deleted if required, and a new one can be added later if required.  The Local Data node must be the first node inside the Method.

13.3Adding Operational Nodes to Methods

Various node types are added to Methods perform actual computations.  The following example shows three sequence instructions

Figure 60: Sequence Nodes added to a Method Node

A Method can contain any of the possible computational elements:

13.4Adding Iteration Nodes

When an Iteration node is added, all the computations that will be placed in the resulting loop of computation will be placed in the model as children to the Iteration node as shown here.

Figure 61: Adding an Iterator Node

In this example only the two Sequence statements (“Sequence statement 1” and “Sequence statement 2” are contained within the Iterator node.  The statement following the Iteration node will be executed after the Iteration is complete.

13.5Adding Selection Nodes

A Selection node allows for some statements to be executed depending on some condition being true.  When first added it comes with a Then node attached, like this:

Figure 62: Adding a simple Selector Node

 

A Selection Node must have a single Then node that cannot be removed, or relocated.  By itself the combination of the Selection and Then nodes allow a set of instructions to be evaluated under some conditions.  It is often useful to be able to execute an alternative set of instructions if the Selection condition fails.  This is achieved by adding an Else node like this:

Figure 63: Adding a complete Selector Node

The required computations, within the Then and Else nodes, are performed by adding other nodes within these two branches of the tree.

A Selector node must only have one Then node; and may optionally have only one Else node.

It is permissible to chain If/Then nodes together as shown here:

Figure 64: Adding chained Selector Nodes

to create quite complex sets of Selection nodes.

13.6Adding Switch/Case Nodes

The Switch/Case construct is really just a Selection operation, but we include it separately as it looks a little different.  When first inserted the Switch node comes with a Default node like this:

Figure 65: Adding a Switch Node

The Switch node must have only one Default node, and it must be the last node connected to the Switch node.  To make the Switch node useful, Case nodes must be added like this:

Figure 66: Adding Case Nodes to the Switch Node

Any required computations are done within the required Case and Default nodes by adding them as shown.  A Switch node can only contain (directly as a child node) Case nodes and only a single Default node.

13.7Adding Recursion Nodes

Recursion nodes can be added wherever a Sequence node can be added.  It just looks like a single computational element, even though it is more complex.

Figure 67: Adding a Recursion Node

Remember that a recursion node must contain a reference to the Method in which it is contained.

13.8Adding Try/Catch Nodes

Try/Catch nodes are used to handle computations exceptions, i.e. where something may go wrong which requires some special handling.  The Try node comes with a single catch node attached, like this:

Figure 68: Adding a simple Try Node

A Try node must at least have a single Catch node (or a Finally node) attached to it.  Other Catch nodes can be added, and a Finally node can be added also – but this must be located as the last node.

Figure 69: Adding Catch and Finally Nodes

The computations required within each of these blocks are added as children to the respective Catch/Finally nodes.


14   Summary

P-Coder is not intended to be for the development of large software packages – it is intended for teaching purposes only.  Once you begin to understand the process of programming then it will be time to move on to other IDEs (Interactive Development Environments).  There are many of these that can be used professionally, and for large-scale projects.  For these, however, you will need to know what you are doing so that program design can be well managed and that you don’t make a real mess of the job by adopting a “hacker” approach.  Once you become a confident designer, then these more sophisticated tools will provide support for much more complex programming tasks.  You may still find it useful to come back to pseudocode (and P-Coder) as a means of designing particularly tricky algorithms and program elements.



[1] Formally, instructions occur within a block.  A method is a block and may contain sub-blocks in an hierarchical fashion.

[2] The JPE.jar file must be installed in the P-Coder install directory for this to work as indicated.