APA 5th Edition Template

advertisement
- 1 -
Programming Portfolio
Programming Portfolio
A Paper
By
Llesh Miraj
Address: 2150 Wallace Ave 2G,
Bronx, NY 10462
Phone number: 718-822-6235
E-mail address: lm2555@columbia.edu
MSTU 5031: Programming II
Date: 05-08-2008
Submitted in partial fulfillment of the requirements for:
MSTU – 5031 Programming II
Teachers College Columbia University
New York, 2008
- 1 -
- 2 -
Programming Portfolio
Introduction
The purpose of this paper is to explain the basics of the object
oriented java programming language. The fundamental concepts
examined in this portfolio include objects and classes,
inheritance and encapsulation, abstraction and polymorphism. As
in the real world the myriad of objects is interrelated
throughout various types of interactions; in java objects exist
and interact among each other, further more encapsulation
emphasizes on internal interactions among members of an object,
as well as on defense system by “restricting access” and
“shielding” members of a class from code outside the class. This
relationship among methods and attributes of an object was not
inborn but evolved with the natural development of the java
language. This revolution in java cannot be understood without
the significant contribution of inheritance and polymorphism.
Attributes and behaviors not only interact with each other
inside a class but they can be transferred from parent classes
through inheritance and passed to child classes just like
parents pass their genetic code for blue eyes to their children.
Adaptation helps living things survive in different
environmental variables. This ability evolved in java with the
polymorphism. Behaviors adapt to different classes, this means
- 2 -
- 3 -
Programming Portfolio
that a method that runs on the data of a class will work on the
attributes of another class as well.
Objects and Classes:
All elements are made of atoms, which are made of protons
neutrons and electrons. The three particles are considered to be
“the basic building blocks of all matter” (United Streaming
video). It is the variance in the number of protons in an atom
that accounts for the diversity in the world around us. An atom
with only one proton is hydrogen (gas), an atom with two protons
is helium (gas), and an atom with 3 protons is lithium (alkali
metal).
Figure 1. Jefferson Lab (JLab).
The fact that things are different but all made out of the same
particles, forced the idea to build a class with attributes and
behaviors which characterize all atoms and use this class as a
- 3 -
- 4 -
Programming Portfolio
template or blueprint to build everything else. Since all atoms
have protons, neutrons, electrons and a name, these
characteristics will make up the attributes or properties of our
simplest Atom class. Two Atom classes are different from each
other if they have different number of protons; more exactly
Atom classes vary in the value of their data. The Atom class is
the template or the blueprint to make or create (instantiate)
atom objects. The key word new allocates memory for and builds
the atom object. We can create as many atom objects as we want,
but keeping in mind that there isn’t anything in nature that has
more then 92 protons in the atom.
Fig 2
- 4 -
- 5 -
Programming Portfolio
public class Atom
{
/**
* All data fields,atrributes are private in order to prevent code outside of this
* class to access them.
*/
private int protons;
// number of protons
private int neutrons; // number of neutrons; usually the same as protons.
private int electrons; // number of electrons ;usually the same as protons
private String name; // the name of the atom
/**
* Constructor: a special code used to initialize the attributtes of the class.
* The only way to instantiate an Atom object is to call new on the constructor.
* For example: new Atom(8, 8, 8,oxygen) will create an Atom object with
* 8 protons,8 neutrons, 8 electrons and name oxygen.
*/
public Atom(int protons,int neutrons,int electrons,String name)
{
this.protons = protons;
this.neutrons = neutrons;
this.electrons = electrons;
this.name = name;
}
public int getProtons()
{
return protons;
}
public int getNeutrons()
{
return neutrons;
}
public int getElectrons()
{
return electrons;
}
public String getName()
{
return name;
}
public void setProtons(int protons)
{
this.protons = protons;
}
public void setNeutrons(int neutrons)
{
this.neutrons = neutrons;
}
public void setElectrons(int electrons)
{
this.electrons = electrons;
}
public void setName(String name)
{
this.name = name;
}
// returns a string
public String toString()
{
return "" + " called "+ name + " and has \n" + protons + " protons " + ", " +
neutrons + " neutrons " + " and " + electrons + " electrons" ;
}
}//end of class
- 5 -
- 6 -
Programming Portfolio
public class Oxygen
{
public static void main(String[] args)
{
Atom atom = new Atom(8,8,8,"oxygen");
System.out.println("The Atom just
created is" + atom);
}//end main
}//end of class
Abstraction
Atoms join together to form new objects. Atoms join with atoms
of the same kind to form elements or they combine with different
atoms to form compounds. These are some behaviors the “Atom”
manifests. Here shows up a very important behavior of atoms:
reactivity. The number of electrons in the atom determines the
connection speed (reactivity) with other atoms.
All atoms of aluminum contain 13 protons, atoms with 26
protons are iron and atoms with 29 protons are cupper, gold
atoms have 79 protons. These are elements and consist of one
kind of atom. All the above elements are metals. Some elements
are nonmetals. Both terms “element” and “metal” are abstract
concepts. An element can be anyone of the 92 naturally occurring
elements; also metal can be any of the above mentioned metal
objects. The Element class has general characteristics of all
elements; it cannot instantiate objects, and stays just below
Atom class which is on top of all its subclasses. It is similar
to the Living things class, which contains all general,
- 6 -
- 7 -
Programming Portfolio
attributes and behaviors of all living things. This superclass
has also some abstract subclasses like animal and plant.
There are three major subclasses of element class: Metal,
Nonmetal and Metalloid (or semi-metal). Metal class is an
- 7 -
- 8 -
Programming Portfolio
abstract subclass; it incorporates the general characteristics
of all its subclasses. Based on their properties metals are
grouped further in subclasses with similar properties. Alkali
metals, Alkaline earth metals, Lanthanide, Actinide, and Transition
elements,all are concrete classes and able to build metal objects. For
instance gold is a transition metal; it’s an instance of concrete
subclass Transition which will also generate metal objects like
copper and silver. The Nonmetal class is unable to build
objects, and consists of three concrete subclasses called:
Halogen and NobleGas
and OtherNonmetal. OtherNonmetal class creates
(instantiates) nonmetal objects like diamond and oxygen. The
ability to create objects clears these subclasses from
abstraction and makes them concrete classes.
Depending on what properties and behaviors the program is
interested to study, Uranium could be considered as a
radioactive Element for most of the time, or as a naturally
occurring Metal at certain times, it is also possible to treat
Uranium as an Atom with the largest number of protons (92).
Inheritance
The Element is a type of atom and as such it is a subclass of
Atom, precislier Element extends Atom. In the class tree diagram
the abstract class Metal will transitively use the attributes
and a function of the parent class named Atom, obviously Metal
is a kind of Atom. It is appropriate to add some more physical
- 8 -
- 9 -
Programming Portfolio
and chemical properties to the attributers and functions
inherited from superclass Atom. This way Gold is a Metal and has
luster and it is yellow. Uranium is a radioactive Atom. Here the
code is inherited transitively from top to bottom. The code from
Atom class which resides on the most top of the class hierarchy
is used from the most bottom class Actinide to produce the
radioactive Metal Uranium. The code on its way from top to the
end of the class hierarchy looks like a river that starts as a
small stream on top of mountains and joins with many other
streams together to become a huge river when it runs-off.
Manufacturing an Element is a process that starts with the
simplest class Atom as a base class and adds some more features
to the Atom until the desired final product is reached. Thus the
process called inheritance allows for reusability of existing
code and provides the ability to add more to it.
Encapsulation
Actinide has a method called decay (). The code for the decay
method explains how decay happens by lossOfEnergy (). So Uranium
loses energy by emitting radiation in the form of particles or
electromagnetic waves (Wikipedia). Magnesium is an Alkaline
(sibling class of Actinide) not radioactive Metal and doesn’t
need to know how Uranium decays. Actinide is the only class that
instantiates radioactive metals, and as such remains the only
class with the decay () method. In this case the decay () method
- 9 -
- 10 -
Programming Portfolio
can be specified as private. The access modifier private will
shield this method from being accessed or altered by code
outside the Actinide class. While the Atom class acts as a new
Atom builder it hides it’s inside aggregates and shows only what
it does. Hiding what it has and showing what it does is
accomplished by specifying member variables as private. The
variables are declared private and can be accessed or changed
only through the use of accessor (getters) and mutator (setters)
methods. These methods are public and open to client code, but
the variables itself are restricted from direct access. This
kind of relationship between variables and methods of Atom class
allows the use of Atom from clients and protects its data from
unwanted changes.
Polymorphism
Polymorphism addresses ability of methods to work and function
when they are invoked on different environmental variables.
Considering a class as the environment where the methods run
then the polymorphic behavior is expressed in that the methods
exhibit the ability to adapt to changes in the environment, by
producing different behaviors when invoked on different
environmental variables. The same method accounts for different
results when called in different classes. In the class hierarchy
above it is appropriate to have a test method that operates on
different subclasses of Metal class. An abstract method named
- 10 -
- 11 -
Programming Portfolio
test () is defined in class named Metal. In Box 1 the class
named Alkaline extends the abstract class named Metal and
overrides (redefines) the method named test () , which it
inherits from its parent class named Metal (Richard G. Baldwin). Here
the behavior of method named test () is to display the
message:”Alkaline metal is denser then Alkali”. The parent
version of this method as defined in the parent class named
Metal is meant to do nothing, since it is an abstract method.
class Alkaline extends Metal
{
public void test()
{
System.out.println("Alkaline metal is denser (heavier) then Alkali.\n");
}//end method test()
}//end class Metal
//Box 1
In Box 2 the class named Transition extends Metal and as such
will redefine the test () method, defined in the class named
Metal. The behavior of the method named test () in this class is
to display the message:” Transition elements tend to have high
density.” This message prints out a property specific for
Transition elements and is different from the message displayed
from the same method defined in class named Alkaline. (Richard
G. Baldwin).
- 11 -
- 12 -
Programming Portfolio
class Transition extends Metal
{
public void test()
{
System.out.println("Transition elements tend to have high
density.\n");
}//end method test()
}//end class Transition
//Box 2
Box 3 contains the definition for the class named Alkali.
The
class named Alkali extends the class named Metal, and overrides
the method named test (), which it inherits from its parent
class named Metal. It also adds its own method named react ().
Here the test () method takes another form by displaying a new
message totally different from all messages described above.
class Alkali extends Metal
{
public void test()
{
System.out.println("The alkali metals have low-density.\n");
}//end method test()
public void react()
{
System.out.println("This group of metals is highly reactive ");
}//end method react()
}//end class Alkali()
//Box 3
In class named TestPolymorphysm (in Box 4) the method named test
() is invoked on a reference variable called myMetal, since
class Metal is abstract it cannot be instantiated.
- 12 -
- 13 -
Programming Portfolio
//Box 4
abstract class Atom extends Object {};
abstract class Metal extends Atom
{
abstract void test();
}//end class Metal
class Alkaline extends Metal
{
public void test()
{
System.out.println("Alkaline metal is denser (heavier) then
Alkali.\n");
}//end method test()
}//end class Metal
class Transition extends Metal
{
public void test()
{
System.out.println("Transition elements tend to have high density.\n");
}//end method test()
}//end class Transition
class Alkali extends Metal
{
public void test()
{
System.out.println("Alkali metals have low-density.\n");
}//end method test()
public void react()
{
System.out.println("Alkali metals are highly reactive.\n ");
}//end method react()
}//end class Alkali()
public class TestPolymorphysm
{
public static void main( String[] args)
{
Atom var = new Alkaline();
((Alkaline)var).test();
var = new Transition();
((Transition)var).test();
var = new Alkali();
((Alkali)var).test();
((Alkali)var).react();
//Atom and Metal can only refer to their
Metal myMetal = new Alkaline();
myMetal.test();
}//end main
}//end class TestPolymorphysm
- 13 -
subclasses//
- 14 -
Programming Portfolio
//Box 5
abstract class Animal
{
abstract void say();
}//end class Animal
class Hen extends Animal
{
public void say()
{
System.out.println ("In class Hen say equals cluck, cluck,
cluck");
}// end method say()
}//end of the Hen class
class Chick extends Hen
{
public void say()
{
System.out.println ("In class Chick say equals chip, chip,
chip.");
}//end method say()
}//end class Chick
class Cat extends Animal
{
public void say()
{
System.out.println ("In class Cat say equals meow, meow ");
}//end method say()
}//end class Cat
public class PolymorphicAnimal
{
public static void main(String[] args)
{
Animal any = new Hen();
((Hen)any).say();
any = new Chick();
((Chick)any).say();
any = new Cat();
((Cat)any).say();
}//end main
}//end class PolymorphicAnimal
References
JLab. Title of article. All about Atoms. Retrieved May 5, 2008,
From http://education.jlab.org/atomtour/fact2.html
- 14 -
- 15 -
Programming Portfolio
Author, Richard G. Baldwin. (December 31, 2006). Title of
article. The Essence of OOP using Java, Runtime Polymorphism
through Inheritance. Retrieved May 5, 2008, from
http://www.dickbaldwin.com/tocint.htm
http://www.dickbaldwin.com/java/JavaAP022.htm#6a
Author, Ph.D. David J. Eck. (Version 5.0, December 2006). Title
of book. Introduction to Programming Using Java, Fifth
Edition. Retrieved May 5, 2008, from
http://math.hws.edu/javanotes/contents-with-subsections.html
http://math.hws.edu/javanotes/c5/index.html
Author, John W. M. Russell. Title of article. Java Tutorials.
Retrieved May 5, 2008, from
http://home.cogeco.ca/~ve3ll/jatutor0.htm
- 15 -
Download