Anda di halaman 1dari 3

WEEK 1

WHAT ARE DATA STRUCTURES? WHAT ARE ALGORITHMS?


REVIEW OF JAVA AND OOP
ENCAPSULATION, INHERITANCE, POLYMORPHISM
CLASSES, METHOD, INTERFACES

DATA STRUCTURE
In computer science, a data structure is a particular way of storing and organizing data in
a computer so that it can be used efficiently. Different kinds of data structures are suited to
different kinds of applications and some are highly specialized to certain tasks. For example, B-
trees are particularly well-suited for implementation of database, while compiler
implementations usually use hash tables to look up identifiers. Data structures are used in
almost every program or software system. Specific data structures are essential ingredients of
many efficient algorithms and make possible the management of huge amounts of data, such as
large databases and internet indexing services. Some formal design methods and programming
languages emphasize data structures, rather than algorithms, as the key organizing factor in
software design.

ALGORITHMS
Objects interact with one another by performing actions (called methods). You, as a
programmer, need to design these actions by giving instruction for how action is carried out. The
hardest part of designing these actions are not figuring out how to express your solution in JAVA
(or whatever programming language you are using). The hardest part is coming up with a plan or
strategy that would allow an object to predict the direction of the stock market on a daily basis,
then you could sell this strategy for a huge amount of money. On the other hand, the
programmer who writes the java method who implements this strategy, while well paid, would
not get nearly the same amount of money as you get for designing the basic strategy. Of course
money paid is not necessary an indication of difficulty, but in this case it does pretty much
indicate the difference in difficulty. In less dramatic settings, such as completing a basic
programming assignment or designing a method for balancing you check book, the same rule
applies. The hard part is coming up with the strategy is often express as something called an
algorithm.
Algorithm is a set instruction for solving problem. To qualify as an algorithm, the
instructions must be expressed so completely and precisely that somebody could follow the
instruction without having to fill in any details or make details that are not fully specified in the
instruction.

JAVA
Java is a programming language originally by James Gosling at Sun Microsystem (which is
now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun
Microsystems’ Java flatform. The language derives much of its syntax from C and C++ but has
simpler object model and fewer low-level facilities. Java application are typically compiled to byte
code (class file) that can run on any Java Virtual Machine (JVM) regardless of computer
architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is
specifically designed to have a few implementation dependencies as possible. It is intended to
let application developers “write once, run anywhere”. Java is currently one of the most popular
programming languages in use, and is widely used from application software to web applications.

Object – Oriented Programming (OOP) Language

3 Main Design Principles of OOP

1. Encapsulation
Encapsulation sounds like it means putting thins into capsule, or to rephrase it, packing
things up. Encapsulation is the process of hiding all the details of how a piece of software was
written and telling only what is necessary to understand how the software is to be used.
When you purchased an electric fan, the user’s manual that was included to the package
is how to assemble and use effectively the electric fan not the way this appliance was developed.

2. Polymorphism
Polymorphism is the capability of an action or method to do different things based on the
object that it is acting upon. This is one of the basic principles of Object – Oriented Programming.
Overloading and overriding are two types of polymorphism.
Polymorphism allows multiple objects of different subclasses to be treated as objects of a
single superclass, while automatically selecting the proper methods to apply to a particular
object based on the subclass it belongs to.

3. Inheritance
Inheritance is a relationship between classes wherein one class is the superclass or the
parent class of another. It refers to the properties and behaviors received from an ancestor. It is
also known as a “is–a” relationship. Consider the following hierarchy.
Super
Hero

Flying Super Underwater Super


Hero Hero
Figure: Example of Inheritance

Super Hero is the superclass of Flying Super Hero and Underwater Super Hero classes.
Note theat Flying Super Hero “is–a ” Super Hero. Underwater Super Hero” is–a ” Super Hero as
well.

-Class
The class is the fundamental structure in Object – Oriented Programming. It can be
thought of as a template, a prototype or a blueprint of an object. It consists of two types of
members which are called fields (properties or attributes) and methods. Fields specify the data
types defined by the class, while methods specify the operations.

-Method
A method is a separate piece of code that can be called by a main program or any other
methods to perform some specific function.
The following are characteristics of methods:
*It can return one or no values.
*It may accept as many parameters if needs or no parameters at all. Parameters are also called
function arguments.
*After the method has finished execution, it goes back to the method that called it.
Now, why do we need to create methods? Why don’t we just place all the code inside one
big method? The heart of effective problem solving is in problem decomposition. We can do this
in Java by creating methods to solve a specific part of the problem. Taking a problem and
breaking it into small, manageable pieces is critical to writing large programs.

-Interfaces
An interface is a special kind of block containing method signature (and possibly
constants) only. Interfaces define the signature of a set of methods without the body. Interfaces
define a standard and public way of specifying the behavior of classes. They allow classes,
regardless of their location in the class hierarchy, to implement common behaviors. Note that
interfaces exhibit polymorphism as well, since program may call an interface method and the
proper version of that method will be executed depending on the type of object passed to the
interface method call.
We need to use interfaces if we want unrelated classes to implement similar methods.
Thru interfaces, we can actually capture similarities among unrelated classes without artificially
forcing a class relationship.
Another reason for using an object’s programming interface is to reveal an object’s
programming interface without revealing its classes. As we can see later on the section Interface
vs. Classes, we can actually use an interface as data type.
Finally, we need to model multiple inheritances which allow a class to have more than one
superclass. Multiple inheritances are not present in Java, but present in other object – oriented
languages like C++.