Object Oriented design requires identification of objects, their relationships and communications. Use of nouns and verbs found in specifications in determining objects, their state and methods is one of the techniques employed in this process. It is critical to design clean and coherent classes of objects that encapsulate a state and a set of methods that operate on that state. Hiding the details from the other components of a system is critical here. Objects must act as black boxes wherever we use them. A class must be highly cohesive, all of its pieces, be it, the elements that constitute the state of its objects, or its methods must fit together perfectly--they must make sense. Classes must be loosely coupled, the communication between objects must be clear and minimal.
Probably, the single most important difference between the traditional top-down approach to design and object oriented design is in the level of software reuse. A good Object designer looks for existing classes of objects. He/she employs inheritance to extend classes to make them meet his/her needs, never reinventing the wheel. Java Package Documentation provide a good example of object classification and frameworks. You will use a small percentage of Java classes, but browsing through them is helpful.
Testing of all parts with drivers and stubs must occur as the classes are being developed. But, integrating the various components and testing their interaction is critical here. The sensitive components of a system may need to go through a rigorous mathematical proof to ensure their correctness.
Let users use it. They may have already played with a prototype, but this is for real. Todays industry seems to advocate alpha and beta releases. Although there are some advantages to this, broad-based releases of incomplete with most likely incorrect components does not make sense to me! But, its the way things are today.
Make corrections, make additions, and may be even redesign some parts.
There are few term and concepts that we need to review to be sure we all speak the same language. I won't discuss the primitives in Java, but concepts and terms involved in using classes and objects.
int i;
i=25;
.
.
.
String s1;
String s2;
s1.concat(s2);
In the 1st couple of instructions, a simple integer variable i is declared and it is set to 25, no problem. In the second set of instructions, two String object variables are declared and there seems to be an intent to concatenate s2 to s1. The problem is that s1 and s2 are not created yet; they have not been instantiated with actual objects of class String.
String s1= new String('He');
String s2= new String('llo');
these two statements construct two objects of class String and instantiate
them with variables s1 and s2. Now, if we execute s1.concat(s2);
we change s1 to include the content of s2 as well.
The problem arises when we decide to use a method like concat to concat, say, the content of our int variable i to s1. The two different paths that one could take in a language like java.
It always makes sense to consider the 2nd option first. In this
example, we will make use of the static method toString that converts
an int to a String. toString is in the Integer
class in Java API. Here is way you express this:
s1.concat(Integer.toString(i));
Consider the following code segment:
String s1= new String('He');
String s2;
s2=s1;
What do you expect the instruction s2=s1
to do?
the 3rd answer is correct. Assume that our variable i still
contains 25. Now, consider these two instructions following s2=s1
:
s1.concat(Integer.toString(i));
s2.concat(Integer.toString(i));
the object that both s1 and s2 now reference will contain He2525. Thsi may make you uncomfortable, but it is how a language like Java works. Now,
what if you really wanted a duplicate of s1 in s2, what do you do then? s2= new String(s1);
will accomplish that.
You will Develop your first HTML file (web page) for this course as part of Lab#1. In this lab you will also learn a few things about operating effectively in a workstation and getting an applet to work.
An applet is a small embeddable Java program. In our course, we will set up HTML files that will run our applets. What you will write are subclasses of the Applet class. The Applet class is a subclass of Panel which in turn is a subclass of Container, which in turn is a subclass of Component, which in turn is a subclass of the Object class. Component implements an interface named ImageObserver. Interfaces provide specification for behavior that are adopted and implemented by classes.
Why list the class hierarchy leading to the Applet class? Knowing the class hierarchy in an object oriented framework is not always necessary, but at times useful. When considering the big picture, as discussed earlier, learning to become a class user is as important as learning to develop algorithms. Traversing Java API for the right class or method is an important part of programming with Java and it is best that you get used to it now.
For the most part, when developing applets, you will have examples to guide you on what methods to write. However, if you decide to find out more about any of those methods, you will most likely look much deeper than just the Applet class for its specification in Java API. The functionality provided for applets is not fully specified at that level, much of it is defined in the classes leading to it. For example, if you are interested in knowing what the specification for the paint(Graphics) method is, you would have to look at the Component class. But, how do you track that method down? First of all, there are several ways to do it, but here I'll describe just one way. Start from the Java's Packages page and click the link for Java 2 Platform API Specification. From there, take the path to java.applet. From there, follow the link to Applet. Once you look through the Applet class and notice that the paint method is not there, you can move up to the Panel class and continue this process until you locate paint() in Component.
Java is rich with classes that enable you to do graphics and many other neat things. For most students, the most appealing part of Java is its applets and the fact that it can be transported across the internet and run on any platform. We will begin by looking at a Hello World example. I wrote a couple of simpler Applets first, if you like to check them out too: First Hello World and Second Hello World.
This is a detailed discussion of the Hello and Goodbye Applet.
public class helloAndGoodbye extends Applet implements ActionListener{
means that the class name is
helloAndGoodbye and it is a subclass of the Applet class. it also means that it implements the interface ActionListener.
The variable declarations at the top of this class work the same way as they do with other classes in Java--they are available in all methods of helloAndGoodbye.
protected Font hello_font;
is the declaration
of hello_font which will later get constructed in
the init
method. This is the statement that constructs hello_font:
hello_font=new Font("Helvetica",Font.BOLD,48);
. Note that
the arguments for the constructor are the Font name, its style, and size.
In the following pair of statements, we first set the font to
hello_font for our applet's Graphics object
(all applets have one), then we draw the string "HELLO WORLD" using that font.
g.setFont(hello_font);
g.drawString("HELLO WORLD",40,110);
protected Button switch_;
is the declaration
of switch_ which will also get constructed in the init
method. The following sequence of statements construct switch_ and
set its front and background colors:
switch_ = new Button("Switch");
switch_.setForeground(Color.black);
switch_.setBackground(Color.lightGray);
switch_.addActionListener(this);
requires a bit more
understanding than you have in OO at this point, but here is a breif attempt for now.
this refers to the applet being run by java runtime which in this case
is an instance of helloAndGoodbye. In this statement we are identifying the
applet as the listener object for this button.
The statement that follows these this.add(switch_);
is
designed to include the button in the drawing area associated with the
applet.
The paint method is invoked once when the applet starts, but it is also called when a repaint() is issued, notice that we issue a repaint() from the actionPerformed method. The parameter for paint is a Graphics object. Graphics class provides the description of the methods invoked in paint.
The actionPerformed method is the most interesting one used here. It handles button-click event initiated by the user; it is invoked when our button is clicked.