Worth:20 points
All source code needs to be documented. As you probably have already seen it can be difficult to understand what is happening in a piece of code just by looking at it. Comments and good programming style can make it easier to understand the structure and purpose of the code we write. There are three parts to creating a readable and understandable design along with its associated code. The three parts are the object model, programming style and the comments, each contributes to creating a program that others can modify and use.
First is the object model which is done prior to concrete coding
to manifest the design and get ideas down on paper. To make this model, program designers have
agreed on a standard form of representation called the Unified Modeling Language (UML).
The object model provides
a visual reference to the relationships of the classes to each other and
a shorthand view of the classes. During this design phase of building the
object model, you should be playing with the ideas and relationships of
the classes and not be set on the first design that pops into your head.
A good design at the start, created by "playing with" the object model, will
save coding time and reduce bugs. A good design also makes the overall
program ideas and relationships more understandable by others. Often the
initial object model is not adequate to fully solve the problem and is changed
during the code writing phase of the project. This is part of the normal
process of creating a program and as you make changes to the code being
written, you should reflect those changes in the object model. There are
four principle things that will appear in an object model, these are classes,
inheritance relationships, associations and aggregations. Classes are the main tool we use to organize the code and provide the
structure to our programs. As such they form a principle and central role
in object models and are represented by a square, that is divided into three
parts. The first part is the name of the class, the second is the
variables and the third is the methods of the class. In the process of design,
you must make decisions as to what classes should be in your programs and
hence in the object model. A simple way to think about this is to make big
concepts or nouns into classes and smaller related things into
variables of the related classes. Instance variables should represent some
state of an object or its present situation. Methods are the verbs or actions
that effect our classes ( nouns) and the instance variables of the associated
class. Verbalize what something is and what actions are to be preformed
on it and that should help in seeing what should be a class, an instance
variable of a class and what actions or verbs will become methods of the
class. There is no clear cut way to determine exactly what should be an
instance variable and what should be a class, generally if it is a complex
problem with lots of detail - the tendency will be to create classes to handle
the details. For example, I could model the gas tank in a car class as simply
a double and when the car is moved around the gas is subtracted from the
tank. When the value of the gas tank equals zero I am out of gas. But take
the situation in which I am modeling a rear-end crash of a car, suddenly
the complexity level jumps and a separate car and a separate gas tank class
may be required to correctly model the behavior of a crash. Inheritance is one of the techniques we use to allow us to reuse code others
have written and/or modify something that is very close to fit what we need.
This must be used with caution since inheritance should only be used to
express an IS-A relationship, that an object is also something else. Generally
this means moving from a general concept to one that is more specific to
what is in our problem domain. Take for example the class motor vehicles
(general concept) being extended to make a Mercedes ( a less general motor
vehicle ) class. The Mercedes is still a motor vehicle, so by using inheritance
we gain the general features of a motor vehicle and then modify only
the parts that are specific to our motor vehicle. We have already been doing
this in our programs by inheriting Applet into our programs, which we then
make a specific kind of Applet that is our program. Generally you will not
have a lot of inheritance in the programs we write in this course. *** Classes ***

public class A
{
public int x;
private double y;
public void myMethod( int z )
{
}
private int myOtherMethod( )
{
return 0;
}
}
*** Inheritance ***

public class A
{
public int x,y;
private double z;
public void display( )
{
}
private double add(int a1, int a2 )
{
return 0;
}
}
public class B extends A
{
public int x1;
private double z1;
public void display( )
{
}
}
Associations are the most common connections generally in our object
models. An association is A-USE relationship between two or more classes.
In some way two classes are related to each other within our program, normally
an association is labeled with a short meaningful description ( 1 or 2 words
) of what the relationship is. Often this label will be something like "model/view"
, "prints", "mouse controller". Frequently the best
way to figure out what labels should be on the association is to look at
the verbs, actions or methods that interact between the two classes. Associations
are thought of as being fairly loose connections between classes. Because
of this they are frequently changed during the code development phase of
projects and are the most updated part of most object diagrams.
Whether a class uses an explicit instantiation of a Listener or if it implements it internally, the reader should be clear that an Interface/Listener has been implemented.
Since class A says that it
Aggregation is a special kind of association. Aggregation is the composition
of a class from other classes and is a PART-OF relationship between classes.
Meaning that one class contains another as part of its definition, the aggregate
classes are part of a bigger item but is not the item itself or the same
kind of item. An example of this is a car which is made up of a number of
items grouped together like the engine, tires, seats, but each of those
items alone is not the car but when put together as a group make up a car.
This is a much stronger relationship than that of an association because
the use of the classes is in relation to its containment within another
class. If you are unsure about whether something is an aggregate or just
an association, make it an association. The code between an association
and an aggregation may look very similar, this has more to do conceptually
with the way we think about how the classes are used. For more, see this page
The Free Software Foundation ( GNU ) has one of the best programming
style guides and you should take some time and review these guidelines.
The guidelines they provide are for C programs but much of what they say
is also useful when developing in other languages like java.
http://www.fsf.org/prep/standards_toc.html
Some simple programming style guidelines for this course...
Examples..
Don't do something like this.
x++ = y + x++ + 4 - y-- ;
Use it pretty much only like this.
x++;
y--;
Comments provide a way to communicate directly though the source code
what your intentions, feelings and reasoning are for a certain solution. At
a later date and time you, or another programmer, may need to return to the
program you wrote. Most likely you won't remember these details at a later
date and/or a new programmer will have no reference to what the code does.
Well-written comments will clarify and enlighten the source code and no
comments are way better than ones that are wrong or misleading. Good comments
are a kindness that other programmers will greatly appreciate when they encounter
your code. Poor or misleading comments will earn the hate and scorn
of your fellow programmers.
Here is the applet to see running - though you shouldn't need to. Here is the applet to see running - though you shouldn't need to.
The html for adding the actual image to a web page looks like so,
<img src="filename"><br> or you can put a link to each of your lab's images
Create an object model of the applet and post it with the documented
and commented source code. Everything should be in a directory called lab5
under your csci directory (in the public_html directory).
You must create the applet, a web page to display applet, a link to the
source code, the javadoc and an object model to get full credit.
Web Page Submission Required Contents:
*** Association ***

public class A
{
public B myb;
public void paint( )
{
myb =new B( );
myb.display( );
}
}
public class B
{
public int x, y;
public void display( )
{
}
}
*** Interfaces ***
implements the Interface ActionListener, and the Interface specifies the method of actionPerformed(ActionEvent e), then this
method must be
implemented in the class. Note however, that even though the code is inside of class A, for the Object Model diagram the method is specified in the Interface box since that is where it is specified in the API.
Notice also that Class A has a Button as an instance variable. Since this user did not write the code for the Button, but
rather it was available from the Java API, the class only shows its name but not its variables or methods.

public class A implements ActionListener
{
public Button myButton;
public void paint()
{
g.drawString("Hello", 50, 50);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pushed");
}
}
*** Aggregation ***

public class A
{
public int x,y;
private double z;
private C myClass;
public void display( )
{
}
private double add(int a1, int a2 )
{
myClass=new C();
myClass.x1=a1+a2;
return 0;
}
}
public class C
{
public int x1;
private double z1;
public void display( )
{
}
}
*** Programming Style ***
Programming style is about putting your code in a format that is readable
as well as applying conventions on how certain features of the language are used.
Often it is possible to get a program to compile and run but that does not mean
all is "done". Most programs must also be modified at some point.
This is called maintenance. Maintenance is a major issue since most programs
must be changed to meet the evolving needs of the users or the systems that
they use. Languages often give us programmers many different ways to achieve
the same task and programming styles provide a way to simplify the ways
in which things are done. Companies working on large products will often
have a documented style that all programmers working on a project will adhere
too, this keeps the code in a uniform manner and makes it easier for a new
programmer to examine the work of previous programmers. Ideally the code
from two different programmers following the same coding style and guidelines
should look extremely similar.
style 1 "Classic C style "
public class A {
int x;
}
style 2 "Pascal style"
public class A
{
int x;
}
Also state whether they are instance or class variables/methods. This is done with the word static to designate a class variable/method.
*** Comments ***
*** Assignment ***
<a href="filename.gif"> OM.gif </a >
to the .gif file (in the same directory)
init() and paint(Graphics g) are called by the Applet class
when it is instantiated by the browser. Other methods in the Applet are not automatically called.
Make sure to remember to invoke the methods that you write (other than init() and paint(Graphics g)).
See methods notes for a reminder on using and invoking multiple methods...(and a very similar class on using
and instance variable. )
Submission:
Go to the
Lab Assignments: drop box link and then to lab 5 and follow directions for submission.
Clearly you need to provide us the link to your web page, or we will not know
where to find your page for grading.
In the URL you submit, you should have a web page with
Poorly documented source code will also lose
points.